#!/bin/bash

taskdir=`php /unas/apps/wanyoucloudserver/v3/components/func/getTaskDir.php`
# 输出结果
#echo "WebdavTempDir: $taskdir"

#ID=1cfefb5a368a56f25cea48bcdbe81dd7
#conf_file="/unas/run/wanyoucloud/mv/$ID/conf"
#progress_file="/unas/run/wanyoucloud/mv/$ID/progress"
#done_file="/unas/run/wanyoucloud/mv/$ID/done"
#size_file="/unas/run/wanyoucloud/mv/$ID/size"
#processing_file="/unas/run/wanyoucloud/mv/$ID/processing"

params_str=$1
#params_str=admin-e29d0366220e221319a0ed1e969fea76e0d5e326dbd21c9318a1a5683dd49d7b
# 使用 cut 命令分割字符串
USER=$(echo "$params_str" | cut -d'-' -f1)
ID=$(echo "$params_str" | cut -d'-' -f2)
# 输出每个部分
#echo "Part 1: $USER"
#echo "Part 2: $ID"

conf_file="$taskdir/$USER/$ID/conf"
progress_file="$taskdir/$USER/$ID/progress"
done_file="$taskdir/$USER/$ID/done"
size_file="$taskdir/$USER/$ID/size"
processing_file="$taskdir/$USER/$ID/processing"
errorperm_file="$taskdir/$USER/$ID/errorperm"

if [ ! -f "$conf_file" ]; then
  exit 0;
fi

exec > $progress_file
. $conf_file

# 检查 done_file 的内容
if [ -f "$done_file" ]; then
    done_code=$(cat "$done_file")
    if [ "$done_code" -eq 4 ]; then
        echo "done_file contains 4, stopping the move operation."
        exit 0;
    fi
fi

# 检查并创建目标目录
if [ ! -d "$TARGETDIR" ]; then
    sudo -E -u $USER mkdir -p "$TARGETDIR"
    if [ $? -ne 0 ]; then
        echo "Failed to create target directory: $TARGETDIR"
        exit 1
    fi
fi

#SOURCE_FS=`stat -fc %i "$SOURCE"`
#DIST_FS=`stat -fc %i "$TARGETDIR"`
#if [ $SOURCE_FS == $DIST_FS ];then
#    SIZES=(0)
#else
    SIZES=(`du -bsc "${SOURCE[@]}"|awk '{print $1}'`)
#fi
TOTAL_SIZE=${SIZES[-1]}
SIZE_DONE=0
DONE_FLAG=0

move(){
    source_file=$1
    target_file=$2

    SOURCE_FS=`stat -fc %i "$source_file"`
    DIST_FS=`stat -fc %i "$TARGETDIR"`

    if [ $SOURCE_FS == $DIST_FS ];then
        if [ -d "$target_file" ]; then
            if [ $USER == "admin" ];then
                rsync -aI --info=progress2 --chown=admin:Administrators --remove-source-files "$source_file/" "$target_file" && find "$source_file" -type d -empty -delete
                if [ $? -ne 0 ]; then
                    DONE_FLAG=4
                fi
            else
                sudo -E -u $USER rsync -aI --info=progress2 --remove-source-files "$source_file/" "$target_file" && sudo -u $USER find "$source_file" -type d -empty -delete
                if [ $? -ne 0 ]; then
                    DONE_FLAG=4
                fi
            fi
        else
            if [ $USER == "admin" ];then
                rmdir "$target_file" 2>/dev/null ; mv "$source_file" "$target_file"
                if [ $? -ne 0 ]; then
                    DONE_FLAG=4
                fi
            else
                sudo -E -u $USER rmdir "$target_file" 2>/dev/null ; sudo -u $USER mv "$source_file" "$target_file"
                if [ $? -ne 0 ]; then
                    DONE_FLAG=4
                fi
            fi
        fi
    else
        if [ -d "$source_file" ];then
            if [ $USER == "admin" ];then
                rsync -aI --info=progress2 --chown=admin:Administrators --remove-source-files "$source_file/" "$target_file" && find "$source_file" -type d -empty -delete
                if [ $? -ne 0 ]; then
                    DONE_FLAG=4
                fi
            else
                sudo -E -u $USER rsync -aI --info=progress2 --remove-source-files "$source_file/" "$target_file" && sudo -u $USER find "$source_file" -type d -empty -delete
                if [ $? -ne 0 ]; then
                    DONE_FLAG=4
                fi
            fi
        else
            if [ $USER == "admin" ];then
                rsync -aI --info=progress2 --chown=admin:Administrators --remove-source-files "$source_file" "$target_file"
                if [ $? -ne 0 ]; then
                    DONE_FLAG=4
                fi
            else
                sudo -E -u $USER rsync -aI --info=progress2 --remove-source-files "$source_file" "$target_file"
                if [ $? -ne 0 ]; then
                    DONE_FLAG=4
                fi
            fi
        fi
    fi
}

echo $SIZE_DONE $TOTAL_SIZE > $size_file

for i in "${!SOURCE[@]}";
do
    echo ${SIZES[$i]} "${SOURCE[$i]}" -\> "${TARGET[$i]}" > $processing_file
    move "${SOURCE[$i]}" "${TARGET[$i]}"
    SIZE_DONE=$((SIZE_DONE + ${SIZES[$i]:-0}))
    echo $SIZE_DONE $TOTAL_SIZE > $size_file
done

code=$?

echo $code > $done_file

if [ $DONE_FLAG -eq 4 ]; then
  echo $DONE_FLAG > $errorperm_file
  php /unas/apps/wanyoucloudserver/v3/components/func/updateTask.php -a "$ID" -b 2 # 0已完成，1进行中，2失败，3错误，4权限不足
else
  if [ $code -eq 0 ] && [ $DONE_FLAG -eq 0 ]; then
    php /unas/apps/wanyoucloudserver/v3/components/func/updateTask.php -a "$ID" -b 0
  else
    php /unas/apps/wanyoucloudserver/v3/components/func/updateTask.php -a "$ID" -b 3
  fi
fi

rm $progress_file $size_file $processing_file

exit $code