set -l dir (status dirname)

function initBackupConfig -V dir -d "Fetches the configuration by prompting the user to "
    set -l disabled VALHALLA_BACKUP_DISABLED

    if [ -z "$$disabled" ]
        if [ -z "$VALHALLA_BACKUP_DIR" ]
            argparse -i "action=" -- $argv
            set -l index
            set -l confirm fish "$dir/../../lib/modules/partition/confirm.fish"

            switch "$_flag_action"
                case backup
                    set index 1
                case restore
                    set index 2
                case '*'
                    set index 3
            end

            if [ "$_flag_action" != restore ] || $confirm "Do you wish to restore a backup?"
                set -l keyVar VALHALLA_BACKUP_SERVER_KEY

                set -l keyPath ~root/.config/port-valhalla/valhalla
                set -l server (string repeat --count 3 (printf "%s\n" "Please specify the host name of the SSH server: " | string collect -N))
                set -l port (string repeat --count 3 (printf "%s\n" "Please specify the port of the SSH server (default 22): " | string collect -N))
                set -l user (string repeat --count 3 (printf "%s\n" "Please specify the name of the user to log in to the SSH server: " | string collect -N))


                set -l ssh \
                    "Do you wish to store the backup on an SSH server?" \
                    "Do you wish to restore the backup from an SSH server?" \
                    "Do you wish to backup or restore files on an SSH server?"

                set -l path \
                    "Please specify the path to the directory to save the backup to: " \
                    "Please specify the path to the directory to load the backup from: " \
                    "Please specify the path to the backup directory: "

                if $confirm $ssh[$index] n
                    read -gxP $server[$index] VALHALLA_BACKUP_SERVER
                    read -gxP $port[$index] VALHALLA_BACKUP_SERVER_PORT
                    read -gxP $user[$index] VALHALLA_BACKUP_SERVER_USER
                end

                if [ -n "$VALHALLA_BACKUP_SERVER" ]
                    set -l sshArgs

                    if [ -z "$$keyVar" ]
                        set -gx "$keyVar" "$keyPath"
                        sudo mkdir -p (dirname "$keyPath")
                        sudo ssh-keygen -f "$$keyVar" -N ""
                    end

                    if [ -n "$VALHALLA_BACKUP_SERVER_PORT" ]
                        set -a sshArgs -p "$VALHALLA_BACKUP_SERVER_PORT"
                    end

                    echo
                    echo "$(tput setaf 3)==== WARNING ====$(tput sgr0)"
                    echo "For a seamless experience, please make sure that you are able to establish an unattended ssh connection using key authentication."
                    echo
                    echo "Your public key is:"
                    sudo cat "$VALHALLA_BACKUP_SERVER_KEY.pub"
                    echo
                    echo "$(tput bold)This command should succeed without user interaction:$(tput sgr0)"
                    echo "sudo ssh -o PasswordAuthentication=no -i $(string escape -- "$VALHALLA_BACKUP_SERVER_KEY") $sshArgs $VALHALLA_BACKUP_SERVER true"
                    read -P "Press enter once you're done: "
                    echo
                end

                read -gxP $path[$index] VALHALLA_BACKUP_DIR
            else
                set -gx "$disabled" 1
            end
        end
    end
end

function backupFiles
    if [ -n "$VALHALLA_BACKUP_DIR" ]
        argparse -i "base-directory=" -- $argv
        set -l tarArgs
        set -l path (getArchivePath $argv)
        set -l dir (dirname "$path")
        argparse -i "u/user=" "n/name=" -- $argv

        if runRestorationCommand test ! -d "$dir"
            runRestorationCommand mkdir -p "$dir"
        end

        if [ -n "$_flag_base_directory" ]
            set -a argv --base-directory "$_flag_base_directory"
            set -a tarArgs -C "$_flag_base_directory"
        else
            set -a tarArgs -P
        end

        sudo tar $tarArgs -cvz (fd $argv) | createArchive "$path"
    end
end

function restoreFiles
    if [ -n "$VALHALLA_BACKUP_DIR" ]
        set -l path (getArchivePath $argv)
        set -l tarArgs
        set -l sudoArgs
        argparse -i "user=" "base-directory=" -- $argv

        if [ -n "$_flag_base_directory" ]
            set -a tarArgs -C "$_flag_base_directory"
        else
            set -a tarArgs -P
        end

        if [ -n "$_flag_user" ]
            set -a sudoArgs -u "$_flag_user"
        end

        if runRestorationCommand test -f "$path"
            runRestorationCommand cat "$path" | sudo $sudoArgs tar $tarArgs -xvz
        end
    end
end

function createArchive -a path
    runRestorationCommand sh -c "tee $(string escape -- "$path") >/dev/null"
end

function runRestorationCommand
    if [ -z "$VALHALLA_BACKUP_SERVER" ]
        $argv
    else
        set -l args
        set -l host "$VALHALLA_BACKUP_SERVER"

        if [ -n "$VALHALLA_BACKUP_SERVER_USER" ]
            set host "$VALHALLA_BACKUP_SERVER_USER@$host"
        end

        if [ -n "$VALHALLA_BACKUP_SERVER_PORT" ]
            set -a args -p $VALHALLA_BACKUP_SERVER_PORT
        end

        sudo ssh -i $(string escape -- "$VALHALLA_BACKUP_SERVER_KEY") $args "$host" (string escape -- $argv)
    end
end

function getArchivePath
    argparse -i "u/user=" "n/name=" -- $argv
    set -l path "$VALHALLA_BACKUP_DIR"

    if [ -n "$_flag_user" ]
        set -a path Users "$_flag_user"
    else
        set -a path System
    end

    set -a path "$_flag_name.tar.gz"
    realpath -m (string join / $path)
end