#!/bin/bash
set -a
shopt -s extglob 
shopt -s nullglob

USAGE="USAGE: ${0##*/} [options] [INSTALL-LOGIN@]INSTALL-HOST:INSTALL-DIR
For Freetown version (4.2.x)
install a TWiki distrib into the directory INSTALL-DIR on host INSTALL-HOST
under the login INSTALL-LOGIN (defaults to current one).
This script must be called from a working dir of a CVS or SVN distrib
exemple: ./utils/twiki-deploy-remote webcore@wiki.ilog.fr:/www/wiki
options:
   -s server-name   defaults to INSTALL-HOST
   -i               force rebuild of config files
   -u url-dir       the base URL of the wiki will be http://server-name/url-dir
		    defaults to the last component of INSTALL-DIR
   -e admin_email   email admin. defaults to $USER@`dnsdomainname`
   -t tempdir       temporary directory used on the remote machine to perform
                    the actual installation via ./utils/twiki-deploy-local
		    defaults to INSTALL-DIR.INSTALL-LOGIN.setup
-s, -u, -e are only needed on first invocation or with -i
The script copies the needed files onto tempdir, then performs in it a 
./utils/twiki-deploy-local [options] INSTALL-DIR
"

############################################################ Utils
trace () { echo "== $*"; tracein=false;}
tracem () { echo "   -- $*"; tracein=false;}
tracen () { if ! $tracein; then echo -n "  ";fi; echo -n " $*"; tracein=true;}
tracend () { if $tracein;then echo; fi; tracein=false;}
err () { echo "***ERROR: $*"; exit 1; }
warn () { echo "+++Warning: $*"; }
do_rsync () { rsync  -aHSx --delete --force --cvs-exclude -z "$@"; }

################################################################ Options
admin_email="$USER@`dnsdomainname`"
unset url_dir # starts with /
unset src dir tempdir init_opt host_arg url_arg email_arg
export tracein=false init=false
tfd=${TWIKI_FREETOWN_DIST:-$HOME/.twiki_freetown_dist}
RSYNC_RSH="ssh -x"

while test -z "${1##-*}" -a -n "$1";do case $1 in
  -s) host="$2";shift;;
  -u) if test -n "$2"; then url_dir="$2"; else url_dir=/; fi; shift;;
  -i) init=true; init_opt=" -i";;
  -e) admin_email="$2";shift;;
  -t) tempdir="${2%/}";shift;;
  *) echo "Unkown option: $1";err "$USAGE";;
  esac;shift
done

if test "$#" != 1; then  
  echo "Error: one argument required, not $# ($@)"; err "$USAGE"; exit 1
fi
lhd="${1%%/}"
inst_login=$USER
case "$lhd" in *@*:*) inst_login="${lhd%%@*}"; lhd="${lhd#*@}";; esac
case "$lhd" in
 *:*) inst_host="${lhd%%:*}"; inst_dir="${lhd#*:}";;
 *) echo "$USAGE"; exit 1;;
esac
dest="$inst_login@$inst_host"
if test -z "$tempdir"; then tempdir="$inst_dir.$inst_login.setup"; fi

################################################################ Copy
# copy needed parts of source, main, plugins to remote site
trace Remote copy to $dest:$tempdir
if ! ssh -x $dest "mkdir -p $tempdir/{src,MAIN,twikiplugins}"; then err no dest tempdir; fi
do_rsync . $dest:$tempdir/src/.
./utils/twiki-versions | ssh -x $dest "cat >$tempdir/src/TWIKI_VERSION"

do_rsync $tfd/MAIN/. $dest:$tempdir/MAIN/.
plugins=""
for i in plugins/*; do
  if test -d $tfd/twikiplugins/${i##*/}; then 
    plugins="$plugins $tfd/twikiplugins/${i##*/}"
  fi
done
do_rsync $plugins $dest:$tempdir/twikiplugins

################################################################ Remote deploy
trace Remote deploy
if [ -n "$host" ]; then host_arg="-s $host"; fi
if [ -n "$url_dir" ]; then url_arg="-u $url_dir"; fi
if [ -n "$admin_email" ]; then email_arg="-e $admin_email"; fi
ssh -x $dest "export TWIKI_FREETOWN_DIST='$tempdir';cd $tempdir/src; ./utils/twiki-deploy-local -r $host_arg $url_arg $init_opt $email_arg $inst_dir"
