#!/bin/bash

GID="$1";
FileNum="$2";
File="$3";
MaxSize="15728640";
Thread="3";  #默认3线程，自行修改，服务器配置不好的话，不建议太多
Block="20";  #默认分块20m，自行修改
RemoteDIR="aria2";  #上传到Onedrive的路径，默认为根目录，如果要上传到MOERATS目录，""里面请填成MOERATS
LocalDIR="/root/Download/aria2/";  #Aria2下载目录，记得最后面加上/
Uploader="/usr/local/oneindex/OneDriveUploader";  #上传的程序完整路径，默认为本文安装的目录
Config="/usr/local/oneindex/auth.json";  #初始化生成的配置auth.json绝对路径，参考第3步骤生成的路径
time=$(date "+%Y%m%d%H%M-")
filename=$(basename $3)
rename=$(date "+%Y%m%d%H%M-")$(basename $3)
urlEncoderename=`echo -n "$rename" | xxd -ps | tr -d '\n' | sed -r 's/(..)/%\1/g'`

if [[ -z $(echo "$FileNum" |grep -o '[0-9]*' |head -n1) ]]; then FileNum='0'; fi
if [[ "$FileNum" -le '0' ]]; then exit 0; fi
if [[ "$#" != '3' ]]; then exit 0; fi

function LoadFile(){
  if [[ ! -e "${Uploader}" ]]; then return; fi
  IFS_BAK=$IFS
  IFS=$'\n'
  tmpFile="$(echo "${File/#$LocalDIR}" |cut -f1 -d'/')"
  FileLoad="${LocalDIR}${tmpFile}"
  if [[ ! -e "${FileLoad}" ]]; then return; fi
  ItemSize=$(du -s "${FileLoad}" |cut -f1 |grep -o '[0-9]*' |head -n1)
  if [[ -z "$ItemSize" ]]; then return; fi
  if [[ "$ItemSize" -ge "$MaxSize" ]]; then
    echo -ne "\033[33m${FileLoad} \033[0mtoo large to spik.\n";
    return;
  fi
  ${Uploader} -c "${Config}" -t "${Thread}" -b "${Block}" -s "${FileLoad}" -n "${time}${filename}"  -r "${RemoteDIR}" -skip
  if [[ $? == '0' ]]; then
    rm -rf "${FileLoad}";
	curl -X GET https://tgbot.bananazone.cc/sendMessage/2p3dk2ejgkdtkni9u?text=$urlEncoderename%E4%B8%8A%E4%BC%A0OneDrive%E6%88%90%E5%8A%9F
  fi
  IFS=$IFS_BAK
}
curl -X GET https://tgbot.bananazone.cc/sendMessage/2p3dk2ejgkdtkni9u?text=$urlEncoderename%E4%B8%8B%E8%BD%BD%E6%88%90%E5%8A%9F
#rm -f "$3.aria2"
LoadFile;