#!/bin/sh
#
# module :
# publication (rsync) of dirwww content on dirpublish given in argument
#
# If the host of publication is cerbere.locean-ipsl.upmc.fr, update_web is
# launched.
#
# update
# $Id$
# fplod 2007-09-28T09:30:43Z aedon.locean-ipsl.upmc.fr (Darwin)
# parametrisation and translation
# smasson 2007-06-07T16:43:42Z arete.locean-ipsl.upmc.fr (Darwin)
# can give the answer with input parameters
# fplod 2007-04-26T11:51:42Z aedon.locean-ipsl.upmc.fr (Darwin)
#
set -o posix
command=$(basename ${0} .sh)
log_date=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
log=/tmp/${command}.${log_date}
#
usage=" Usage : ${command} -w dirwww -p dirpublish"
#
minargcount=4
#echo " narg ${#}"
if [ ${#} -lt ${minargcount} ]
then
  echo "eee : not enought arguments"
  echo "${usage}"
  exit 1
fi
#
while [ ! -z "${1}" ]
do
 case ${1} in
 -w)
  dirwww=${2}
  shift
 ;;
 -p)
  dirpublish=${2}
  shift
 ;;
 esac
 shift # next flag
done
#
set -u
#
# ++ check directories
#
answer=${1:-" "}
case ${answer} in
    y|Y|n|N)
	;;
    *)
	echo "Do you want to install on ${dirpublish}  (y|[n]) ?"
	read answer
	;;
esac
case ${answer} in
 y|Y)
 # copy of ${dirwww} on $dirpublish
 echo "iii : update of ${dirpublish}"
 rsync -av -e ssh ${dirwww}/ ${dirpublish}
 # detect if in dirpublish following this pattern [USER@]HOST:SRC, HOST
 # is cerbere.locean-ipsl.upmc.fr. If so, update_web is launched
 userhost=${dirpublish%%:*}
 host=${userhost##*@}
 if [ "${host}" = "cerbere.locean-ipsl.upmc.fr" ]
 then
  ssh ${userhost} /usr/local_linux/bin/update_web
 fi
 ;;
 *)
 echo "no update of ${dirpublish}"
 ;;
esac
#
# normal exit
exit 0
