#!?SHELL_EXECUTION?
#?USE_ZSH?set -o SH_WORD_SPLIT

#########################################################################
# COPYRIGHT (C) 2003         EDF R&D              WWW.CODE-ASTER.ORG    #
#                                                                       #
# 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 2 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, WRITE TO : EDF R&D CODE_ASTER,       #
#    1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.        #
#########################################################################

# $Id: as_serv 2103 2006-09-07 15:04:33Z courtois $
# as_serv : call an astk services

#########################################################################
# configuration
ASTER_ROOT=?ASTER_ROOT?
export ASTER_ROOT

REPASTK=$ASTER_ROOT/ASTK/ASTK_SERV
SBIN=$ASTER_ROOT/ASTK/ASTK_SERV/bin
SLIB=$ASTER_ROOT/ASTK/ASTK_SERV/lib
SSHA=$ASTER_ROOT/ASTK/ASTK_SERV/share
SCON=$ASTER_ROOT/ASTK/ASTK_SERV/conf
export REPASTK
export SBIN
export SLIB
export SSHA
export SCON

ASTER_VERSION="set_later"
if [ -f $SCON/aster_profile.sh ]; then
   . $SCON/aster_profile.sh
fi

#########################################################################
# arguments
err=-1
if [ $# -ge 1 ]; then
   serv=$1
   shift
   err=0
fi

if [ $err -lt 0 ]; then
   echo "usage : as_serv service [args...]"
   exit 1
fi

# set defaults for remote commands
RSH_PROTO=RSH
RCP_PROTO=RCP

largs=""
if [ $# -gt 0 ]; then
   while true
   do
      case $1 in
         --remote_shell_protocol)
            RSH_PROTO=$2
            shift; shift ;; # shift of 2
         --remote_copy_protocol)
            RCP_PROTO=$2
            shift; shift ;; # shift of 2
         *)
            largs="$largs $1"
            shift ;;
      esac
      if [ $# -le 0 ]; then break; fi # no more argument : exit
   done
fi
export RSH_PROTO
export RCP_PROTO

RSH_CMD=rsh
case $RSH_PROTO in
   SSH)
      RSH_CMD=ssh ;;
   *)
      RSH_CMD=rsh ;;
esac
export RSH_CMD

RCP_CMD=rcp
case $RCP_PROTO in
   SCP)
      RCP_CMD=scp ;;
   RSYNC)
      RCP_CMD=rsync ;;
   *)
      RCP_CMD=rcp ;;
esac
export RCP_CMD

#########################################################################
trouv=0
# old services called now by as_run
case $serv in
   as_info)
      serv=as_run
      largs="--info $largs"
      trouv=1 ;;
   as_actu)
      serv=as_run
      largs="--actu $largs"
      trouv=1 ;;
   as_edit)
      serv=as_run
      largs="--edit $largs"
      trouv=1 ;;
   as_del)
      serv=as_run
      SIGNAL='KILL'
      larg2=""
      for argu in $largs ; do
         if [ $argu = "-KILL" ]; then
            SIGNAL='KILL'
         else
            if [ $argu = "-USR1" ]; then
               SIGNAL='USR1'
            else
               larg2="$larg2 $argu"
            fi
         fi
      done
      largs="--del --signal=$SIGNAL $larg2"
      trouv=1 ;;
   as_tail)
      serv=as_run
      largs="--tail $largs"
      trouv=1 ;;
   # only available on the AGLA machine
   as_rex_creer)
      serv=as_run
      largs="--create_issue $largs"
      trouv=1 ;;
   as_rex_consult)
      echo "The service is not available any more."
      exit 99;;
   as_rex_modfic)
      echo "The service is not available any more."
      exit 99;;
   as_rex_suppr)
      echo "The service is not available any more."
      exit 99;;
esac

# services called themself
if [ $trouv -eq 0 ]; then
   for val in as_run as_exec as_exec_special tool_stanley as_mail; do
      if [ $val = $serv ]; then
         trouv=1
      fi
   done
fi
# unknown service
if [ $trouv -eq 0 ]; then
   echo "Unknown service : $serv"
   iret=1
else
# call service
   $SBIN/$serv $largs
   iret=$?
fi

echo "EXIT_CODE=$iret"
exit $iret

