#!/bin/bash

# This script has been auto-generated by Jirafeau but you can still edit options below.

# Config begin
proxy='' # Or set JIRAFEAU_PROXY.
url='' # Or set JIRAFEAU_URL.
time='month' # Or set JIRAFEAU_TIME.
one_time='' # Or set JIRAFEAU_ONE_TIME.
upload_password='' # Or set JIRAFEAU_UPLOAD_PASSWD
# Config end

if [ -n "${JIRAFEAU_PROXY:-}" ]; then
    proxy="$JIRAFEAU_PROXY"
fi

if [ -n "${JIRAFEAU_URL:-}" ]; then
    url="$JIRAFEAU_URL"
fi

if [ -z "$url" ]; then
    echo "Please set url in script parameters or export JIRAFEAU_URL"
fi

if [ -n "${JIRAFEAU_TIME:-}" ]; then
    time="$JIRAFEAU_TIME"
fi

if [ -n "${JIRAFEAU_ONE_TIME:-}" ]; then
    one_time='1'
fi

if [ -n "${JIRAFEAU_UPLOAD_PASSWD:-}" ]; then
    upload_password="$JIRAFEAU_UPLOAD_PASSWD"
fi

if [ -z "${2:-}" ]; then
    echo "Jirafeau Bash Script 4.7.1"
    echo "--------------------------"
    echo "Usage:"
    echo "    $0 OPTIONS"
    echo
    echo "Options:"
    echo "    $0 send FILE [PASSWORD]"
    echo "    $0 get URL [PASSWORD]"
    echo "    $0 delete URL"
    echo
    echo "Global variables to export:"
    echo "    JIRAFEAU_PROXY: Domain and port of proxy server, eg. »proxyserver.example.com:3128«"
    echo "    JIRAFEAU_URL : URI to Jirafeau installation with trailing slash, eg. »https://example.com/jirafeau/«"
    echo "    JIRAFEAU_TIME : expiration time, eg. »minute«, »hour«, »day«, »week«, fortnight, »month«, »quarter«, »year« or »none«"
    echo "    JIRAFEAU_ONE_TIME : self-destroy after first download, eg. »1« to enable or »« (empty) to disable"
    echo "    JIRAFEAU_UPLOAD_PASSWD : upload password"

    exit 0
fi

proxy_args=()
if [ -n "$proxy" ]; then
    proxy_args=(-x "$proxy")
fi

options=()
if [ -n "$one_time" ]; then
    options+=(-F one_time_download=1)
fi

if [ -n "$upload_password" ]; then
    options+=(-F "upload_password=$upload_password")
fi

password=''
if [ -n "${3:-}" ]; then
    password="$3"
    options+=(-F "key=$password")
fi

apipage='script.php'
downloadpage='f.php'

if [ "$1" == "send" ]; then
    if [ ! -f "$2" ]; then
        echo "File \"$2\" does not exist."
        exit
    fi

    # Get result
    res=$(curl -X POST --http1.0 "${proxy_args[@]}" "${options[@]}" \
                  -F "time=$time" \
                  -F "file=@$2" \
                  "${url}${apipage}")

    if [[ "$res" == Error* ]]; then
        echo "Error while uploading."
        echo "$res"
        exit
    fi

    # Not using head or tail to minimise command dependencies
    code=$(cnt=0; echo "$res" | while read -r l; do
        if [[ "$cnt" == "0" ]]; then
            echo "$l"
        fi
        cnt=$(( cnt + 1 ))
        done)
    del_code=$(cnt=0; echo "$res" | while read -r l; do
        if [[ "$cnt" == "1" ]]; then
            echo "$l"
        fi
        cnt=$(( cnt + 1 ))
        done)
    key_code=$(cnt=0; echo "$res" | while read -r l; do
        if [[ "$cnt" == "2" ]]; then
            echo "$l"
        fi
        cnt=$(( cnt + 1 ))
        done)

    echo
    echo "Download page:"
    if [[ $key_code ]]; then
        echo "    ${url}${downloadpage}?h=$code&k=$key_code"
    else
        echo "    ${url}${downloadpage}?h=$code"
    fi
    echo "Direct download:"
    if [[ $key_code ]]; then
        echo "    ${url}${downloadpage}?h=$code&k=$key_code&d=1"
    else
        echo "    ${url}${downloadpage}?h=$code&d=1"
    fi
    echo "Delete link:"
    echo "    ${url}${downloadpage}?h=$code&d=$del_code"
    echo
    echo "Download via API:"
    if [[ $key_code ]]; then
        echo "    ${0} get ${url}${apipage}?h=$code&k=$key_code [PASSWORD]"
    else
        echo "    ${0} get ${url}${apipage}?h=$code [PASSWORD]"
    fi
    echo "Delete via API:"
    echo "    ${0} delete \"${url}${downloadpage}?h=$code&d=$del_code\""

elif [ "$1" == "get" ]; then
    if [ -z "$password" ]; then
        curl "${proxy_args[@]}" -OJ "$2"
    else
        curl "${proxy_args[@]}" -OJ -X POST -F "key=$password" "$2"
    fi
elif [ "$1" == "delete" ]; then
    curl "${proxy_args[@]}" "$2" --data-raw "do_delete=1%2F" | grep "div class" | sed -e "s/<[^>]\+>//g"
fi
