#!/bin/bash
#
# 2014/09/09 Gabriel Moreau <Gabriel Moreau(A)univ-grenoble-alpes.fr> - Initial release
# 2017/06/20 Gabriel Moreau - Trokata Integration

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
export LANG=C


function usage() {
   cat <<END_USAGE
NAME
   $(basename $0) - fast and robust deploy tool

SYNOPSIS
   $0 -a arch -n soft_name -r release_number [-o os_name] [-d] [-v] [-h]

OPTIONS
   -a  arch : 32_bit / 64_bit
   -n  name of the soft, example: matlab
   -r  release number of the soft, example: 2.3.1
   -o  os distribution name
   -d  delete (mirror) mode
   -v  verbose
   -h  help

AUTHOR
   Gabriel Moreau

COPYRIGHT
   Copyright (C) 2014-2018, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
   Licence : GNU GPL version 2 or later
END_USAGE
   }

# Default values
rsync_cmd='/usr/bin/ionice -c 3 /usr/bin/nice -18 /usr/bin/rsync'
rsync_opt="--contimeout=3 --timeout=3 --bwlimit=5000 --rsync-path='/usr/bin/ionice -c 3 /usr/bin/nice -18 /usr/bin/rsync'"
rsync_share='legilnx40::opt/${os_arch}'
dest_dir='/opt'

if [ -e "/etc/softsyncrc" ]
then
   . "/etc/softsyncrc"
fi

# get options
if [ $# -eq 0 ]; then usage; exit 1; fi 
while getopts "a:n:r:o:dvh" options
do
   case ${options} in
      a)
         if echo ${OPTARG} | egrep -q '^[[:digit:]]+'
         then
            export os_arch=${OPTARG}
         else
            usage
            exit 2
         fi
         ;;

      n)
         if echo ${OPTARG} | egrep -q '[[:alpha:]]'
         then
            export soft_name=${OPTARG}
         else
            usage
            exit 2
         fi
         ;;

      r)
         if echo ${OPTARG} | egrep -q '[[:digit:]]'
         then
            export soft_version=${OPTARG}
         else
            usage
            exit 2
         fi
         ;;

      o)
         if echo ${OPTARG} | egrep -q '[[:alpha:]]'
         then
            export os_name=${OPTARG}
         else
            usage
            exit 2
         fi
         ;;

      d)
         export rsync_opt="${rsync_opt} --delete"
         ;;

      v)
         export verbose='yes'
         ;;

      h|*)
         usage
         exit 3
         ;;
   esac
done


soft_version_server=${soft_version}
if [ -n "${os_name}" ]
then
   soft_version_server="${soft_version}..dist.${os_name}"
fi

rsync_error='2> /dev/null'
if [ "${verbose}" == "yes" ]
then
   rsync_error=
fi

[ -d "${dest_dir}/${soft_name}" ] || mkdir -p "${dest_dir}/${soft_name}"
if eval ${rsync_cmd} -avn ${rsync_opt} ${rsync_share}/${soft_name}/.zz-end.${soft_version_server} \
      ${dest_dir}/${soft_name}/.zz-end.${soft_version} \
      ${sync_error} | /bin/grep -q ^\.zz-end
then
   rm -f ${dest_dir}/${soft_name}/.zz-end.${soft_version}
   [ "${verbose}" == "yes" ] && \
      eval echo ${rsync_cmd} -avz ${rsync_opt} ${rsync_share}/${soft_name}/${soft_version_server}/ ${dest_dir}/${soft_name}/${soft_version}/
   if eval ${rsync_cmd} -avz ${rsync_opt} ${rsync_share}/${soft_name}/${soft_version_server}/ ${dest_dir}/${soft_name}/${soft_version}/
   then
      chmod ugo+rX ${dest_dir}/${soft_name}/
      eval ${rsync_cmd} -aq ${rsync_opt} ${rsync_share}/${soft_name}/.zz-end.${soft_version_server} ${dest_dir}/${soft_name}/.zz-end.${soft_version}
   fi
fi
