#!/bin/sh
#  *********************************************************************
# ***********************************************************************
# ****                                                               ****
# ***                 STARTUP for SAPup                               ***
# ***                                                                 ***
# ***           Copyright (c) 2012-2015 SAP-AG                        ***
# ****                                                               ****
# ***********************************************************************
#  *********************************************************************
rootdir=`dirname $0`

if [ `uname` = 'OS400' ]; then
  PWDCMD='/QOpenSys/usr/bin/pwd -L'
else
  PWDCMD=/bin/pwd
fi

if [ -z "$rootdir" -o "$rootdir" = "." ] ; then
  rootdir=`$PWDCMD`
else
  absdir=`expr "X$rootdir" : "X/"`
  if [ "$absdir" -eq 0 ] ; then
    rootdir="`$PWDCMD`/$rootdir"
  fi
fi

configha=no
restartha=yes
configtype=upgrade

cmdopts=
gtopt=gt=httpchannel
for arg
do
  case "$arg" in
  upgrade)
    cmdopts="$cmdopts runmode=upgrade"
    configtype=upgrade
    ;;
  confighostagent)
    configha=yes
    ;;
  RESTARTSHA=no)
    restartha=no
    ;;
  restartsha=no)
    restartha=no
    ;;

  quiet)
    gtopt=gt=batch
    ;;
  scroll)
    gtopt=gt=scroll
    ;;
  gt=*)
    gtopt="$arg"
    ;;

  configinput=*)
    cmdopts="$cmdopts $arg"
    ;;
  batchinput=*)
    cmdopts="$cmdopts $arg"
    ;;

  *)
    cmdopts="$cmdopts $arg"
    ;;
  esac
done

# ************************************************************************
if [ "$configha" = "yes" ] ; then
  if [ "$configtype" = "upgrade" ] ; then
      configsum=1
      configobserver=1
  else
      configsum=1
      configobserver=1
  fi

  #  Check for wrong permission of our folder (i. e. belongs to "root"
  if [ `find $rootdir -user 0 -print | wc -l` -gt 0 ] ; then
      find $rootdir -user 0 -print
      echo "ERROR: SUM folder belongs to root or has root files (see above)!"
      exit 11
  fi

  #  Adapt rootdir automatically to 0755 - needed by Hostagent Permission Policy (PL47)
  chmod og-w $rootdir $rootdir/SUMSTART

  #  Adapt automatically to where we are. Protect against "/usr/sap/put" or so :)
  scriptname=`basename $0`
  sid=`echo $rootdir | sed -n 's/\/usr\/sap\/\([A-Z][A-Z0-9][A-Z0-9]\)\/.*/\1/p'`
  testdir=/usr/sap/$sid/SYS/profile
  if [ -d $testdir ] ; then
    sumdir=`echo $rootdir | sed 's!/usr/sap/[A-Z][A-Z0-9][A-Z0-9]/!/usr/sap/\$[SID:#required]/!'`
  else
    sid=""
    sumdir=$rootdir
  fi

  hostctrldir=/usr/sap/hostctrl/exe
  opdir=$hostctrldir/operations.d
  descdir=$hostctrldir/descriptors.d

  #create and configure non-existing Hostagent directories
  if [ `uname` = 'OS400' ]; then
    usergroup=qsecofr:R3GROUP
  else
    usergroup=sapadm:sapsys
  fi

  if [ ! -d $opdir ] ; then
    mkdir $opdir
    chown $usergroup $opdir
    chmod 0750       $opdir
  fi

  if [ ! -d $descdir ] ; then
    mkdir $descdir
    chown $usergroup $descdir
    chmod 0750       $descdir
  fi

  #create Hostagent configuration
  # -------------------------------------------------------------------------------------------
  if [ $configsum ] ; then
      test -r $opdir/sumabap.conf && mv $opdir/sumabap.conf $opdir/sumabap.conf.old
      cat <<EOF >$opdir/sumabap.conf
Name:sumabap
Authorization:\$[SID:#required#tolower]adm
Command:$sumdir/$scriptname
Username: \$[SID:#required#tolower]adm
Workdir: $sumdir/
ResultConverter: flat
EOF

      test -r $descdir/sumabap.lmsldesc && mv $descdir/sumabap.lmsldesc $descdir/sumabap.lmsldesc.old
      cat <<EOF >$descdir/sumabap.lmsldesc
Codebase:sumabap
ConnectionPolicy: SAPHostChannel:_lmsl_sumabap_\$[SID:#required]
Operation: sumabap
URISuffix: SID
WebRoot: $sumdir/doc
CsrfProtection: all
ForceHTTPS: true
ProxyRouting[exclude|hard]: /slui,/slui_ext,/pic,/guide,/eval,/analysis
EOF

      chown 0:0  $opdir/sumabap.conf $descdir/sumabap.lmsldesc
      chmod 0644 $opdir/sumabap.conf $descdir/sumabap.lmsldesc
  fi

  # -------------------------------------------------------------------------------------------
  if [ $configobserver ] ; then
      test -r $opdir/sumobserver.conf && mv $opdir/sumobserver.conf $opdir/sumobserver.conf.old
      cat <<EOF >$opdir/sumobserver.conf
Name:sumobserver
Authorization:\$[SID:#required#tolower]obs
Command:ls
Username: \$[SID:#required#tolower]obs
Workdir: $sumdir/doc
ResultConverter: flat
EOF

      test -r $descdir/sumobserver.lmsldesc && mv $descdir/sumobserver.lmsldesc $descdir/sumobserver.lmsldesc.old
      cat <<EOF >$descdir/sumobserver.lmsldesc
Codebase:sumobserver
ConnectionPolicy: SAPHostChannel:_lmsl_sumobserver_\$[SID:#required]
Operation: sumobserver
URISuffix: SID
WebRoot: $sumdir/doc
CsrfProtection: all
ForceHTTPS: true
ProxyRouting[exclude|hard]: /monitor,/slui,/pic,/analysis
EOF

      chown 0:0  $opdir/sumobserver.conf $descdir/sumobserver.lmsldesc
      chmod 0644 $opdir/sumobserver.conf $descdir/sumobserver.lmsldesc
  fi

  # -------------------------------------------------------------------------------------------
  if [ "$restartha" = "yes" ] ; then
      echo "**** Restarting SAP Host Agent ****"
      $hostctrldir/saphostexec -restart
      echo "**** Host Agent has been restarted ****"
      echo "**** Host Agent configured, start the UI from the browser now ****"
      echo "**** You are using SAP Host Agent version: ****"
      $hostctrldir/saphostexec -version | grep 'kernel release'
      $hostctrldir/saphostexec -version | grep 'kernel make variant'
      $hostctrldir/saphostexec -version | grep 'patch number'
      echo "**** Make sure that you have the latest SAP Host Agent version. See SAP Note 2219592 for more information ****"
  fi

  hostname=$HOST
  if [ -z "$HOST" ] ; then
    hostname=`hostname`
  fi

  test -z "$hostname" && hostname="<HOSTNAME>"
  test -z "$sid" && sid="<SID>"

  if [ $configsum ] ; then
      echo "**** SUM ABAP: https://$hostname:1129/lmsl/sumabap/$sid/slui ****"
  fi

  if [ $configobserver ] ; then
      echo "**** SUM Observer: https://$hostname:1129/lmsl/sumobserver/$sid/monitor/index.html ****"
  fi
  exit 0
fi

# ************************************************************************
# check for user sidadm
if [ `uname` = 'OS400' ] ; then
  user=`id -u -n`
else
  user=`whoami`
fi
if [ -z "`echo $user | sed -n 's/\(.*adm\)/\1/p'`" ] ; then
  echo "ERROR: Your user name is $user but you must be <sid>adm! Only for parameter confighostagent you need to use the root user."
  exit 5
fi

# ************************************************************************
#  Check maximum allowed length for root directory
if [ `echo $rootdir | wc -c` -gt 30 ] ; then
  echo "ERROR: Root directory ${rootdir} exceeds allowed length of 25, please use another one!"
  exit 12
fi

# ************************************************************************
if [ `uname` = 'AIX' ]; then
    #  improve forking for large 'SAPup' processes
    LDR_CNTRL="DATAPSIZE=64K@TEXTPSIZE=64K@STACKPSIZE=64K"
    export LDR_CNTRL
    #VMM_CNTRL="vmm_fork_policy=COR"
    #export VMM_CNTRL
fi

# ************************************************************************
SAPUPEXE=${rootdir}/bin/SAPup

if [ ! -r "$SAPUPEXE" ] ; then
  echo "ERROR: Missing executable $SAPUPEXE!"
  exit 13
fi

exec $SAPUPEXE "rootdir=${rootdir}" $gtopt $cmdopts
