#!/bin/ksh
#
#
UNIX2WEBBIN=/usr/local/bin/unix2webd
UNIX2WEBCONFDIR=/etc/unix2web
#
#
if [ ! -f "$2" ]
 then
  if [ ! -d $UNIX2WEBCONFDIR ]
   then
    echo "$UNIX2WEBCONFDIR nicht vorhanden."
    exit 0
  elif [ `ls $UNIX2WEBCONFDIR/*.conf 2>/dev/null | /usr/bin/wc -l` -eq 0 ]
   then
    echo "Es sind keine Services definiert."
    exit 0
  fi
fi
#
start()
{ echo
  echo "Config File: $1"
  unset SWITCHUSER U2WPORT U2WHOME U2WUSER U2WPARS
  . "$1"
  if [ ! -z "$SWITCHUSER" ]
   then
    unset U2WUSER
  fi
  if [ "$U2WPORT" -gt 0 -a "$U2WHOME" != "" ]
   then
    if [ ! -z "$U2WUSER" ]
     then
      U2WUSER="-u $U2WUSER"
    fi
    echo "$UNIX2WEBBIN -D -p $U2WPORT -h $U2WHOME $U2WUSER $U2WPARS"
    if [ -z "$SWITCHUSER" ]
     then
      eval $UNIX2WEBBIN -D -p $U2WPORT -h \"$U2WHOME\" $U2WUSER $U2WPARS
    else
      su -c "$UNIX2WEBBIN -D -p $U2WPORT -U -h \"$U2WHOME\" $U2WUSER $U2WPARS" $SWITCHUSER
    fi
  fi
}

stp()
{ echo
  echo "Config File: $2"
  unset U2WPORT U2WHOME
  . "$2"
  if [ "$U2WPORT" -gt 0 -a "$U2WHOME" != "" ]
   then
    PID=`/bin/ps -ef | /usr/bin/grep "unix2webd -D -p $U2WPORT" | \
        /usr/bin/grep -v grep | /usr/bin/awk '$3 == "1" { print $2 }'`
    if [ ! -z "$PID" ]
     then
      kill $PID
      /usr/bin/sleep 1
    fi
    if echo "$1" | /usr/bin/grep -q "kill"
     then
      PIDS=`/bin/ps -ef | /usr/bin/grep "unix2webd -D -p $U2WPORT" | \
            /usr/bin/grep -v grep | /usr/bin/awk '{ print $2 }'`
      for PID in $PIDS
       do
        kill $PID
      done
      /usr/bin/sleep 1
    fi
    if echo "$1" | /usr/bin/grep -q "kill9"
     then
      PIDS=`/bin/ps -ef | /usr/bin/grep "unix2webd -D -p $U2WPORT" | \
            /usr/bin/grep -v grep | /usr/bin/awk '{ print $2 }'`
      for PID in $PIDS
       do
        kill -9 $PID
      done
    fi
  fi
}

restart()
{ if [ "$1" = "restart" ]
   then
    unset WAITCONNECTION U2WPORT
    . "$2"
    if [ "$U2WPORT" != "" -a "$WAITCONNECTION" != "" ]
     then
      while [ `netstat -n | awk '$4 ~ "'$U2WPORT'"' | /usr/bin/wc -l` -gt 0 ]
       do
        /usr/bin/sleep 3
      done
    fi
  fi
  stp $1 "$2"
  start "$2"
}

case "$1" in
  start)
        if [ -z "$2" ]
         then
          echo "unix2web: ..."
          for i in `ls $UNIX2WEBCONFDIR/*.conf`
           do
            start "$i"
          done
          echo started
        else
          if [ -f "$2" ]
           then
            start "$2"
          elif [ -f "$UNIX2WEBCONFDIR/$2.conf" ]
           then
            start "$UNIX2WEBCONFDIR/$2.conf"
          else
            echo "Kann Konfigurationsdatei $2 nicht finden"
          fi
        fi
        ;;
  stop*)
        if [ -z "$2" ]
         then
          echo "unix2web: ..."
          for i in `ls $UNIX2WEBCONFDIR/*.conf`
           do
            stp $1 "$i"
          done
          echo stopped
        else
          if [ -f "$2" ]
           then
            stp $1 "$2"
          elif [ -f "$UNIX2WEBCONFDIR/$2.conf" ]
           then
            stp $1 "$UNIX2WEBCONFDIR/$2.conf"
          else
            echo "Kann Konfigurationsdatei $2 nicht finden"
          fi
        fi
        ;;
  restart*)
        if [ -z "$2" ]
         then
          echo "unix2web: ..."
          for i in `ls $UNIX2WEBCONFDIR/*.conf`
           do
            restart $1 "$i"
          done
          echo restarted
        else
          if [ -f "$2" ]
           then
            restart $1 "$2"
          elif [ -f "$UNIX2WEBCONFDIR/$2.conf" ]
           then
            restart $1 "$UNIX2WEBCONFDIR/$2.conf"
          else
            echo "Kann Konfigurationsdatei $2 nicht finden"
          fi
        fi
        ;;
   *)
        echo "Usage: $0 start|stop|stopkill|stopkill9|restart|restartnowait|restartkill|restartkill9 [<name>]"
        exit 1
        ;;
esac
