#!/bin/bash

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

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"

#ID=$1
id_dir="$taskdir/$USER/$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

SIZES=(`du -bsc "${SOURCE[@]}"|awk '{print $1}'`)
TOTAL_SIZE=${SIZES[-1]}
SIZE_DONE=0
DONE_FLAG=0

echo $SIZE_DONE $TOTAL_SIZE > $size_file

# 自动重命名函数
auto_rename() {
    local dst=$1
    local base=$(basename "$dst")
    local dir=$(dirname "$dst")
    local ext=""
    local i=1

    # 如果是文件夹，则不需要处理扩展名
    if [ -d "$dst" ]; then
        while [ -e "$dir/$base" ]; do
            base="${base}_${i}"
            ((i++))
        done
        echo "$dir/$base"
    else
        # 如果是文件，则处理扩展名
        ext="${dst##*.}"
        base="${base%.*}"
        while [ -e "$dir/$base.$ext" ]; do
            base="${base}_${i}"
            ((i++))
        done
        echo "$dir/$base.$ext"
    fi
}

for i in "${!SOURCE[@]}"; do
    source="${SOURCE[$i]}"
    target="${TARGET[$i]}"

    if [ -d "$source" ] && [ -d "$target" ]; then
        if [ $USER == "admin" ]; then
            rsync -a --remove-source-files "${source}/" "$target"
        else
            sudo -u "$USER" rsync -a --remove-source-files "${source}/" "$target"
        fi

        if [ $? -ne 0 ]; then
            DONE_FLAG=4
        fi
        find "$source" -type d -empty -delete > /dev/null 2>&1 || :

    else
        if [ -e "$target" ]; then
            target=$(auto_rename "$target")
        fi

        if [ $USER == "admin" ]; then
            mv "$source" "$target"
            if [ $? -ne 0 ]; then
                DONE_FLAG=4
            fi
        else
            sudo -u "$USER" mv "$source" "$target"
            if [ $? -ne 0 ]; then
                DONE_FLAG=4
            fi
        fi
    fi
done

echo $SIZE_DONE $TOTAL_SIZE > $size_file

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权限不足
fi

rm $progress_file $size_file $processing_file

# 如果操作成功（code为0），则删除id_dir
if [ $code -eq 0 ] && [ $DONE_FLAG -eq 0 ]; then
  rm -rf "$id_dir"
  php /unas/apps/wanyoucloudserver/v3/components/func/removeTask.php -a "$ID"
fi

exit $code
