#! /bin/bash
# poubelle - a command line pastebin-like which upload files through ssh
# Copyright (C) 2008-2013 Olivier Schwander <olivier.schwander@chadok.info>
# Copyright (C) 2009 Fabien Givors <fabien.givors@chezlefab.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

VERSION=0.2

[ -z "$XDG_CONFIG_HOME" ] && XDG_CONFIG_HOME=$HOME/.config
CONFIG_FILES="$XDG_CONFIG_HOME/poubellerc $HOME/.poubellerc"

SSH_HOST=
BASE_DIR=
BASE_URL=
HTTPD_GROUP=www-data
PYGMENTIZE_OPTIONS=

usage () {
    echo "poubelle - a command line pastebin-like which upload files through ssh"
    echo "$0 [-h] [-c CONFIG] [file1 [file2 ... [fileN]]]"
    exit $1
}

version () {
    echo "poubelle $VERSION"
    echo "Copyright (C) 2008-2013 Olivier Schwander <olivier.schwander@chadok.info>"
    echo "Copyright (C) 2009 Fabien Givors <fabien.givors@chezlefab.net>"
    echo "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>."
    echo "This is free software: you are free to change and redistribute it."
    echo "There is NO WARRANTY, to the extent permitted by law."
    exit 0
}

show_url () {
    if [ $count -eq 1 ]; then
        echo $1 | xclip
    fi
    echo $1
}

while getopts "hc:l:v" flag; do
    [ "$flag" = "h" ] && usage 0
    [ "$flag" = "c" ] && CONFIG_FILES=$OPTARG
    [ "$flag" = "l" ] && PYGMENTIZE_LEXER="-l $OPTARG"
    [ "$flag" = "v" ] && version
done
shift $(($OPTIND-1))

for config in $CONFIG_FILES; do
    if [ -r $config ]; then
        echo "Found configuration in $config"
        . $config
        break
    fi
done

for var in SSH_HOST BASE_DIR BASE_URL; do
    if [ -z "${!var}" ]; then
        echo "The configuration variable $var cannot be empty !"
        usage 2
    fi
done

tmpdir=$(mktemp -d poubelle.XXXXXXXXXX)

rand=$(dd if=/dev/urandom count=64 bs=1 2>/dev/null | sha1sum | cut -c1-40)
dir=$BASE_DIR/$rand
url=$BASE_URL/$rand

count=$#
if [ $count -eq 0 ]; then
    echo "Enter your text here, end with ^D"
    if [ -z "$PYGMENTIZE_LEXER" ]; then
        PYGMENTIZE_LEXER="-l text"
    fi
    set $tmpdir/index
    cat >$1
    count=1
fi

if [ $count -ne 1 ]; then
    echo $url
    echo -n $url | xclip
fi

ssh $SSH_HOST mkdir -p $dir &&
ssh $SSH_HOST touch $BASE_DIR/index.html &&
while [ -n "$1" ]; do
    name=$(basename $1)
    raw=$url/$name

    if [ -f "$1" ]; then
        htmlfile="$tmpdir/${name}.html"
        html=${raw}.html

        if pygmentize -O full -f html $PYGMENTIZE_OPTIONS $PYGMENTIZE_LEXER -o "$htmlfile" "$1"; then
            show_url $html

            scp -r "$htmlfile" $SSH_HOST:$dir
            rm "$htmlfile"
        else
            show_url $raw
        fi
    fi

    scp -r "$1" $SSH_HOST:$dir
    shift
done &&
ssh $SSH_HOST chgrp $HTTPD_GROUP -R $dir &&
ssh $SSH_HOST chmod g+rX -R $dir

rm -r $tmpdir

