#!/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="3029687654"
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
            BaiduPCS-Go su "$bduid"
            time BaiduPCS-Go d "$line"
            mv "/futabruh/tempbitte/nonu2/3029687654_wym7532/$filename" ./
            time BaiduPCS-Go sumfile "$filename" 
            time sha512sum "$filename" 
            
            mv "$filename" "$dir.$filename"
            time bash ~/discord/singlefilediscorddrive.sh "$dir.$filename"
            time bash ~/guilded_dir.v3.1.sh "$dir.$filename" "$discordwebhook"
            
            rm "$filename" -rf
            let currentlines++
            echo -e "\e[32mfile\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 > /dev/null 2> /dev/null
    bash ~/discord/singlefilediscorddrive.sh currentprogress.rar > /dev/null 2> /dev/null
    rm currentprogress.rar -f

    IFS=$OLD_IFS
}

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

downloadantics "$1"

exit 0
