#! /usr/bin/env nix-shell
#! nix-shell -i fish -p fish discordchatexporter-cli

read -gP "Insert Token: " token
read -gP "Insert an after date: " dateInput
set afterDate (date "+%F %H:%M:%S" --date="$dateInput")
set beforeDate (date "+%F %H:%M:%S")

# set guildIDs 828901086722785281 659186066724749312 \
#     618655243554521089 111912780377985024
# set guildNames 'The fattest pussy club' CreativeCringeCorner \
#     'Bound Art With Lore and Less Minors' 'Webcomic Land'
set dmIDs 808310797004308500 539621978504560640 810121293818363934 816078347972116491
set dmNames Hurricane DeftBeck Tomayto Weenie
set baseDir $HOME/Temp/Discord
mkdir -p $baseDir

function guildsAll
    for guildID in $guildIDs
        discordchatexporter-cli exportguild --parallel 5 \
            -t $token -g $guildID --before $beforeDate \
            -p 10mb -o "$baseDir/$guildID - $beforeDate"/
    end
end

function guildsUpdate
    for guildID in $guildIDs
        discordchatexporter-cli exportguild --parallel 5 \
            -t $token -g $guildID --after $afterDate --before $beforeDate \
            -p 10mb -o "$baseDir/$guildID - $beforeDate"/
    end
end

function dmAll
    for dmID in $dmIDs
        discordchatexporter-cli export --parallel 5 \
            -t $token -c $dmID --before $beforeDate \
            -p 10mb -o "$baseDir/$dmID - $beforeDate"
    end
end

function dmUpdate
    #echo $afterDate
    for dmID in $dmIDs
        discordchatexporter-cli export --parallel 5 \
            -t $token -c $dmID --after $afterDate --before $beforeDate \
            -p 10mb -o "$baseDir/$dmID - $beforeDate"
    end
end

function renameDirs
    set i 1
    set names $guildNames $dmNames
    for ID in $guildIDs $dmIDs
        set foundDir (find $baseDir -type d -name "$ID*")
        mv $foundDir $baseDir/(string replace $ID $names[$i] (basename $foundDir))
        set i (math $i + 1)
    end
    for dir in (find $baseDir -maxdepth 1 -type d -not -path $baseDir)
        set fileName (basename $dir).tar.xz
        cd $baseDir
        set baseName (basename $dir)
        tar cJvf $baseDir/$fileName $baseName/
        trash-put $dir
    end
end

switch $argv[1]
    case -G
        guildsAll
        renameDirs
    case -g
        guildsUpdate
        renameDirs
    case -D
        dmAll
        renameDirs
    case -d
        dmUpdate
        renameDirs
    case -u
        guildsUpdate
        dmUpdate
        renameDirs
end
