#!/bin/bash

readonly HTTP_BASE="https://florrie.ed1.club"
readonly SSH_BASE="florrie@ed1.club"
readonly REMOTE_BASE="public_html"

private=false
unlisted=false
delete=false
list=false

OPTIND=1
while getopts ":dhlpu" opt; do
    case $opt in
        u)
            unlisted=true
            ;;
        p)
            private=true
            ;;
        d)
            delete=true
            ;;
        l)
            list=true
            ;;
        h)
            echo "-p: Act on a private file. (If not specified, on a public file.)"
            echo "-u: Act on an unlisted file. (If not specified, on a listed file.)"
            echo "-d: Delete the stated file. (If not specified, upload a file."
            echo "-l: List all files."
            exit 0
            ;;
        \?)
            echo "Invalid option: -$OPTARG" >&2
            exit 1
            ;;
    esac
done

i="$OPTIND"

if [ -n "${!i}" -a "$list" = true ]; then
    >&2 echo "Didn't expect filename."
    exit 1
fi

if [ -z "${!i}" -a "$list" = false ]; then
    >&2 echo "Expected filename."
    exit 1
fi

if [ "$private" = true ]; then
    if [ "$unlisted" = true ]; then sshdir="private/unlisted"
    else sshdir="private"; fi
else
    if [ "$unlisted" = true ]; then sshdir="stuff/unlisted"
    else sshdir="stuff"; fi
fi

if [ "$list" = true ]; then
    ssh -t "$SSH_BASE" << EOF

find "$REMOTE_BASE/stuff" "$REMOTE_BASE/private" -type f -printf '%Ts\t%p\n' | sort -n | cut -f2 |
    while read -r -d $'\n' x; do
        if [ -n "\$x" ]; then
            date="\$(stat -c '%Y' "\$x")"
            datefmt="\$(date -d "@\$date" +'%F %T')"
            file="\${x#"$REMOTE_BASE"}"

            private=false
            [[ "\$file" == /private/* ]] && private=true

            unlisted=false
            [[ "\$file" == */unlisted/* ]] && unlisted=true

            filename=\${file#"/private/"}
            filename=\${filename#"/stuff/"}
            filename=\${filename#"unlisted/"}

            if [ "\$private" = true ]; then
                printf "\033[31m[Private"
            else
                printf "\033[32m[Public"
            fi
            if [ "\$unlisted" = true ]; then
                printf -- "-\033[22mUnlisted"
            fi
            printf "\033[22m] "

            printf "\033[1m"
            printf "\$filename"
            printf '\r\033[40C '
            printf "\033[22m"
            printf "\$datefmt"
            printf "   "
            printf "\033[2m"
            printf "$HTTP_BASE\$file"
            printf "\033[0m"
            printf "\n"
        fi
    done

EOF
    exit 0
fi

filename="${!i}"; let "i++"

if [ -z "${!i}" ]; then
    targetname="$(basename "$filename")"
else
    targetname="${!i}"
    settargetname=true
fi

targetpath="$sshdir/$targetname"

if [ "$delete" = true ]; then
    ssh "$SSH_BASE" "rm -i \"$REMOTE_BASE/$targetpath\""
    if [ "$?" = 1 ]; then
        printf "\033[31mThat file doesn't exist! "
    else
        printf "\033[31m"
        printf "Deleted "
    fi
else
    rsync -sav "$filename" "$SSH_BASE:$REMOTE_BASE/$targetpath"
    printf "\033[36m"
    printf "Uploaded "
fi

printf "\033[1m"
printf "$(basename "$filename")"
printf "\033[22m"

if [ "$settargetname" = true ]; then
    printf " as "
    printf "\033[1m"
    printf "$(basename "$targetname")"
    printf "\033[22m"
fi

printf ".\033[0m"

if [ "$private" = true ]; then
    printf " \033[31;1m[Private"
else
    printf " \033[32;1m[Public"
fi
if [ "$unlisted" = true ]; then
    printf -- "-\033[22;2mUnlisted"
fi
printf "\033[22;1m]"
printf "\033[0m"

printf " -- "

if [ "$private" = true ]; then
    if [ "$unlisted" = true ]; then printf "$HTTP_BASE/private/unlisted"
    else printf "$HTTP_BASE/private"; fi
else
    if [ "$unlisted" = true ]; then printf "$HTTP_BASE/stuff/unlisted"
    else printf "$HTTP_BASE/stuff"; fi
fi

printf "/$targetname"

# Cleanup
printf "\033[0m"
printf "\n"
