#!/usr/bin/env bash

source $HOME/.config/teiler/config
if [[ "$img_ul" == "s3" || "$img_ul" == "scp" ]]; then
    source $HOME/.config/teiler/uploader/$img_ul
fi
if [[ "$vid_ul" == "s3" || "$vid_ul" == "scp" ]]; then
    source $HOME/.config/teiler/uploader/$vid_ul
fi
if [[ "$paste_ul" == "s3" || "$paste_ul" == "scp" ]]; then
    source $HOME/.config/teiler/uploader/$paste_ul
fi

# define uploaders
# images
if [[ $img_ul == "fb" ]]; then
    imageUpload () {
        if [[ $(echo "$files" | wc -w) -gt 1 ]]; then
            cd "${img_path}"; fb -m ${@}; x_clip; notify-send -a "teiler" "Image Uploaded" "$(xclip -o)"
        else
            cd "${img_path}"; fb ${@}; x_clip; notify-send -a "teiler" "Image Uploaded" "$(xclip -o)";
        fi
    }

elif [[ $img_ul == "scp" ]]; then
    imageUpload () { for x in $files; do scpUpload "Image" "${img_path}" "$x" "${scp_host}" "${scp_path_img}" "${http_img}"; done; }
elif [[ $img_ul == "imgur" ]]; then
    imageUpload () { cd "${img_path}"; for x in $files; do imgurbash2 "${x}"; x_clip; notify-send -a "teiler" "Image Uploaded" "$(xclip -o)"; done; }
elif [[ $img_ul == "s3" ]]; then
    imageUpload () { cd "${img_path}"; s3cmd --no-progress put "${1}" "s3://${s3_bucket}/${s3_path_img}/"; notify-send -a "teiler" "Image Uploaded" "${s3_http_img}/${1}"; echo -n "${s3_http_img}/${1}" | xclip; x_clip; }
else
    imageUpload () { echo "No image uploader set. check example config"; }
fi


# Videos
if [[ $vid_ul == "fb" ]]; then
    videoUpload () { cd "${vid_path}"; fb "${1}"; x_clip; notify-send -a "teiler" "Video Uploaded" "$(xclip -o)"; }
elif [[ $vid_ul == "scp" ]]; then
    videoUpload () { scpUpload "Video" "${vid_path}" "$1" "${scp_host}" "${scp_path_vid}" "${http_vid}"; }
elif [[ $vid_ul == "s3" ]]; then
    videoUpload () { cd "${vid_path}"; s3cmd --no-progress put "${1}" "s3://${s3_bucket}/${s3_path_vid}/"; notify-send -a "teiler" "Video Uploaded" "${s3_http_vid}/${1}"; echo -n "${s3_http_vid}/${1}" | xclip; x_clip; }
else
    videoUpload () { echo "No video uploader set. Check example config"; }
fi

# text
if [[ $paste_ul == "fb" ]]; then
    clipUpload () { cd "${paste_path}"; fb "${1}"; x_clip; notify-send -a "teiler" "Paste Uploaded" "$(xclip -o)"; }
elif [[ $paste_ul == "scp" ]]; then
    clipUpload () { scpUpload "Paste" "${paste_path}" "$1" "${scp_host}" "${scp_path_paste}" "${http_paste}"; }
elif [[ $paste_ul == "ix" ]]; then
    clipUpload () { xclip -o 2>&1 | curl -F 'f:1=<-' ix.io | tr -d "\n" | xclip; x_clip; notify-send -a "teiler" "Paste Uploaded" "$(xclip -o)"; }
else
    clipUpload () { echo "No text uploader set. Check example config"; }
fi

scpUpload() {
    fileType="$1";
    path="$2";
    filename="$3";
    scp_host="$4";
    scp_path="$5";
    http="$6";
    cd "${path}";
    scpResult="$(scp "$filename" "${scp_host}:/${scp_path}/${filename}" 2>&1)";
    if [[ "$?" == 0 ]]; then
        notify-send -a "teiler" "${fileType} Uploaded" "${http}/${filename}";
        echo -n "${http}/${filename}" | xclip;
        x_clip;
    else
        notify-send -a "teiler" "${fileType} upload failed" "$scpResult";
    fi;
}

x_clip () {
    if [[ $img_ul == "imgur" ]]; then
        (xclip -o -selection clipboard) | xclip
    else
        (xclip -o) | xclip -selection clipboard
    fi
}

if [[ $1 == "--upload" ]]; then
    files=$(echo "$@" | cut -d ' ' -f3-)
    if [[ $2 == "image" ]]; then imageUpload "${files}"
    elif [[ $2 == "video" ]]; then videoUpload "${files}"
    elif [[ $2 == "text" ]]; then clipUpload "${files}"
    fi
fi
