#!/usr/bin/env bash

# Usage:
#  uploadalpha [release] [username]

# default values
USERNAME=${2:-$LOGNAME}
DEFAULT_RELEASE=`cat ALPHA`
RELEASE=${1:-$DEFAULT_RELEASE}
HOST="$USERNAME@tiddlywiki.com"
DIR="/var/www/www.tiddlywiki.com/htdocs"
OWNER="www-data:www-data"
PERM="664"

# setPermissions()
# Usage:
#  setPermissions file
function setPermissions() {
	COMMANDS="$COMMANDS sudo chown $OWNER $1;"
	COMMANDS="$COMMANDS sudo chmod $PERM $1;"
}

# upload files to temporary folder
echo
echo "uploading files"
echo
FILES="tiddlywiki.$RELEASE.html tiddlywiki_compressed.$RELEASE.html tiddlywiki_externaljs.$RELEASE.html tiddlywiki_externaljs_tiddlyspace.$RELEASE.html twcore.$RELEASE.js jquery/jquery.js jquery/plugins/jQuery.twStylesheet.js"
scp $FILES "$HOST:./tmp/"

# transfer files to their respective folders
echo
echo "transferring files"
echo
stty -echo
COMMANDS="ssh $HOST"
# Empty
COMMANDS="$COMMANDS sudo cp ./tmp/tiddlywiki.$RELEASE.html $DIR/alpha/empty.html;"
setPermissions "$DIR/alpha/empty.html"
# Compressed
COMMANDS="$COMMANDS sudo mv ./tmp/tiddlywiki_compressed.$RELEASE.html $DIR/alpha/tiddlywiki_compressed.html;"
setPermissions "$DIR/alpha/tiddlywiki_compressed.html"
COMMANDS="$COMMANDS sudo mv ./tmp/tiddlywiki_externaljs.$RELEASE.html $DIR/alpha/tiddlywiki_externaljs.html;"
setPermissions "$DIR/alpha/tiddlywiki_externaljs.html"
COMMANDS="$COMMANDS sudo mv ./tmp/tiddlywiki_externaljs_tiddlyspace.$RELEASE.html $DIR/alpha/tiddlywiki_externaljs_tiddlyspace.html;"
setPermissions "$DIR/alpha/tiddlywiki_externaljs_tiddlyspace.html"
COMMANDS="$COMMANDS sudo mv ./tmp/twcore.$RELEASE.js $DIR/alpha/twcore.js;"
setPermissions "$DIR/alpha/twcore.js"
COMMANDS="$COMMANDS sudo mv ./tmp/jquery.js $DIR/alpha/jquery.js;"
setPermissions "$DIR/alpha/jquery.js"
COMMANDS="$COMMANDS sudo mv ./tmp/jQuery.twStylesheet.js $DIR/alpha/jQuery.twStylesheet.js;"
setPermissions "$DIR/alpha/jQuery.twStylesheet.js"
# execute
$COMMANDS
stty echo