#!/bin/bash
# copyrekt die deutsche Orthopädiespezialist 2022
# onedrive to dudisk / discord drive / guild drive tri-direction reupload script, github actions ver. (convinced
# use this github actions to run: https://github.com/idinahuichan/ruantics, remember to fork into your own github account, and enable actions:thonkeqing:
# and use `generic antics v114514` to run, put oneindex file link (or probably ANY http file i guess) in `List Link: `, and put this script in "mysterious bash script"

# constants
bduid="669476744"
discordwebhook="https://discord.com/api/webhooks/937726948082352178/3DVGQDJXu5tmN8fB_gToNuZT6GJqUnusyTB61Si50rBVW71yuxniVJVnvGx6r7gPHtHO"

function onedrivereupload(){
    for line in `cat list`
    do
        if [ `echo "$line" | grep -c "^#"` -eq 0 ] # not comment
        then
            currentdir=`pwd`
            filename="${line##*/}"
            dir="${line%/*}"
            dir="${dir##*/}"
            # probably we need to add a loop here, to detect unfinish downloads and retry it
            time aria2c --dir='temp' --out "$filename" -k 1M -x 128 -s 128 -j 128 -R -c --auto-file-renaming=false "$line"
            for file in `ls temp`
            do
                BaiduPCS-Go su "$bduid"
                time BaiduPCS-Go sumfile "temp/$file" 
                time sha512sum "temp/$file" 
                
                if [ "$dudisk" ]
                then
                    dudiskargs=`curl http://144.172.69.22/nonu2/temp/dudiskargs`
                    ddtime=`echo "$dudiskargs" | cut -f1 -d'|'`
                    ddprocesses=`echo "$dudiskargs" | cut -f2 -d'|'`
                    BaiduPCS-Go su "3029687654"
                    time timeout "$ddtime" BaiduPCS-Go u --norapid -p "$ddprocesses" "temp/$file" "/$dir"
                    time timeout 40s BaiduPCS-Go u --norapid -p "$ddprocesses" "temp/$file" "/$dir"
                    time timeout 60s BaiduPCS-Go u --norapid -p "$ddprocesses" "temp/$file" "/$dir"
                    
                    rm "~/.config/BaiduPCS-Go/pcs_uploading.json" -f
                    dudiskargs=`curl http://144.172.69.22/nonu2/temp/dudiskargs`
                    ddtime=`echo "$dudiskargs" | cut -f1 -d'|'`
                    ddprocesses=`echo "$dudiskargs" | cut -f2 -d'|'`
                    time timeout "$ddtime" BaiduPCS-Go u --norapid -p "$ddprocesses" "temp/$file" "/$dir"
                    time timeout 40s BaiduPCS-Go u --norapid -p "$ddprocesses" "temp/$file" "/$dir"
                    time timeout 60s BaiduPCS-Go u --norapid -p "$ddprocesses" "temp/$file" "/$dir"
                fi
                
                if [ "$dualdrive" ]
                then
                    mv "temp/$file" "temp/$dir.$file"
                    cd "$currentdir/temp"
                    time bash ~/discord/singlefilediscorddrive.sh "$dir.$file"
                    time bash ~/guilded_dir.v3.1.sh "$dir.$file" "$discordwebhook"
                    cat results.txt >> "$currentdir/results.txt"
                    cd "$currentdir"
                fi
            done
            rm temp -rf
            let currentlines++
            echo -e "\e[36mfile \e[32m$dir.$filename\e[36m finished uploading in \e[32m`date`, \e[36m$currentlines\e[32m/$totallines\e[0m"
        fi
    done
}

function downloadantics() { # $1 = listfile
    OLD_IFS=$IFS
    IFS=$'\n'
    [ -f list ] || curl "$1" > list
    totallines=`cat list | wc -l`
    currentlines=0
    
    # logantics3 &
    onedrivereupload >> log 2>&1
    # sleep 4 # to make sure the very last version of log file was uploaded into real time logging server at least once
    
    # sed -i 's/^temp\///g' results.txt
    rar a -ep1 -htb -m5 -ma5 -rr5 -ts -tsp -ol currentprogress.rar log* rarlog* rarlog2* list results.txt ~/localdb.discorddrive.txt ~/localdb.guildeddrive.txt > /dev/null 2> /dev/null
    bash ~/discord/singlefilediscorddrive.sh currentprogress.rar > /dev/null 2> /dev/null
    bash ~/guilded_dir.v3.1.sh currentprogress.rar "$discordwebhook" > /dev/null 2> /dev/null
    rm currentprogress.rar log* list -f

    IFS=$OLD_IFS
}

# [ "$1" = "rarantics" ] && discorddrive

if [ "$1" = "--dudiskonly" ]
then
    dudisk="AYAYA! AYAYA!! AYAYA!!! "
    downloadantics "$2"
elif [ "$1" = "--dualdriveonly" ]
then
    dualdrive="AYAYA! AYAYA!! AYAYA!!! "
    downloadantics "$2"
else
    dudisk="AYAYA! AYAYA!! AYAYA!!! "
    dualdrive="AYAYA! AYAYA!! AYAYA!!! "
    downloadantics "$1"
fi

exit 0
