#!/bin/bash

# SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
#
# SPDX-License-Identifier: GPL-3.0-or-later


if [ -z $dry_run ]; then dry_run=0; fi

# migrate for a dir entry
migrate_for_dentry()
{
    dir_path="$1"
    short_name="$2"
    long_name="$3"

    if [ "$short_name" == "$long_name" ]; then return 0; fi

    short_path="$dir_path/$short_name"
    long_path="$dir_path/$long_name"

    if [ -L "$short_path" ]
    then
        target=`readlink "$short_path"`
        if [ $dry_run -eq 0 ]
        then
            ln -s "$target" "$long_path" >/dev/null 2>&1
        else
            echo symlink "$long_path" to "$target"
        fi
        # no rm $short_path
    else
        if [ $dry_run -eq 0 ]
        then
            mv -n "$short_path" "$long_path" >/dev/null 2>&1
        else
            echo move "$short_path" to "$long_path"
        fi
    fi
}

# migrate for a dir
migrate_for_dir()
{
    dir_path="$1"
    data_file_path="$dir_path/.longname"

    first_line=1
    short_name=""
    while IFS= read -r line; do
        # check data file version
        if [ $first_line -eq 1 ]
        then
            if [ "$line" != "version=1.0" ]
            then
                echo "dfm_WARNING: .longname version not support, $data_file_path"
                break
            fi
            first_line=0
            continue
        fi

        # rename short name to long name
        if [ -z "$short_name" ]
        then
            short_name="$line"
        else
            long_name="$line"
            migrate_for_dentry "$dir_path" "$short_name" "$long_name"
            short_name=""
        fi
    done < "$data_file_path"

    if [ $dry_run -eq 0 ]
    then
        mv "$data_file_path" "${data_file_path}~" >/dev/null 2>&1
    fi
}

# migrate for a mount point
migrate_for_mount_point()
{
    mount_point="$1"

    if [ $dry_run -eq 1 ]
    then
        echo "================ $mount_point ================"
    fi

    # process the directories under the mount point in a bottom-up order
    # the fs type where the directory is located is ulnfs, and there is a .longname file in the dir
    find "$mount_point" -depth -type d -fstype ulnfs -exec test -f '{}'/.longname \; -print0 |
        while IFS= read -r -d '' data_file_dir; do
            migrate_for_dir "$data_file_dir"
        done
}

# migrate for all mount point
migrate_for_mount_points()
{
    default_paths=`dde-dconfig --get -a org.deepin.dde.file-manager -r org.deepin.dde.file-manager -k  dfm.mount.dlnfs.defaults`

    formats='"[ " ]'
    for path in $default_paths
    do
        contains=$(echo $formats | grep -F "${path}")
        if [ "$contains" != "" ]; then
            continue
        fi

        # remove quotes and commas
        path=${path//'"'/}
        path=${path//','/}
        abs_path=$path

        env_path=`echo $path | grep -o '\$[^/]*'` # env_path = $HOME

        if [ "$env_path" != "" ]; then
            env_var=${env_path//'$'/}  # env_var = HOME
            abs_env_path=`echo ${!env_var}` # abs_env_path = /home/$USER
            abs_path=${path/$env_path/$abs_env_path}  #path = /home/xxxx/xxxx
        fi

        if [ ! -d $abs_path ]; then
            echo "dfm_WARNING: $abs_path do not exist"
            continue
        fi

        migrate_for_mount_point $abs_path
    done
}

migrate_filename()
{
    # check if we finished the migrate
    if [ -f $HOME/.config/deepin/migrate_filename_from_dlnfs_to_ulnfs_finished ]
    then
        echo "file name migration has been done"
        return 0
    fi

    # check if we should do a migration
    ulnfs_enable=`dde-dconfig --get -a org.deepin.dde.file-manager -r org.deepin.dde.file-manager -k dfm.mount.ulnfs`
    if [[ "$ulnfs_enable" != "true" ]]; then return 0; fi

    migrate_for_mount_points

    touch $HOME/.config/deepin/migrate_filename_from_dlnfs_to_ulnfs_finished
    echo "complete file name migration"
}

if [ "$test_dir" ]
then
    migrate_for_mount_point "$test_dir"
else
    migrate_filename
fi

exit 0
