#!/bin/bash
# TLWWW: /etc/init.d/textlib-www lenyege
#

#:::::: Adatbazis nevenek kitalalasa
. /usr/share/tlwww/tlwsetpar

#::::::: Fix beallitasok ::::::
PATH="/sbin:/bin:/usr/sbin:/usr/bin"

#:::::::: functions :::::::::::

# -- inditas --
tlwww_start()
{
  echo -e "\n\n\n\n*** textlib-www$par $1 $2 $3 $4 $5 : `date`" >> $TLWWWLOG

  if [ ! -x $TLWDIR/$SCRIPTNAME ]; then
     echo "Nincs futtathato $TLWDIR/$SCRIPTNAME" >> $TLWWWLOG
     exit 5
  fi
  if [ ! -x $TLWDIR/$PROGNAME ]; then
     echo "Nincs futtathato $TLWDIR/$PROGNAME" >> $TLWWWLOG
     exit 5
  fi

  # Elozetes ellenorzes - biztos nem fut? Pid nem volt, dehat...
  # Ha tobb tlwww megy egyszerre, akkor ez bekavar!
  #  sleep 1
  #  CheckProgRun  

  echo -n "TextLib     WWW$par: inditas ..."

  /bin/bash $TLWDIR/$SCRIPTNAME $par $1 $2 $3 $4 2>> $TLWWWLOG &

  # nezzuk, elindult-e a program
  cnt=0
  PidRun $TLWWWPID
  while [ $cnt -lt 5 -a ! -f $TLWWWPID ]; do
      cnt=$[$cnt+1]
      sleep 1
      PidRun $TLWWWPID
  done
  if [ $cnt -ge 5 ]; then
      echo " nem indult el"
      exit 1
  else
      echo " OK"
      touch $TLLOCKFILE 2> /dev/null
      chgrp textlib $TLLOCKFILE 2> /dev/null
      chmod g+rw $TLLOCKFILE 2> /dev/null
  fi
}

# -- leallitas --
tlwww_stop()
{
   if [ ! -x $TLWDIR/g-tlkill.exe ]; then
       echo "Nincs futtathato $TLWDIR/g-tlkill.exe" >> $TLWWWLOG
       exit 5
   fi

   echo -n "TextLib     WWW$par: leallitas ..."

   # Van pid file, csak akkor hivodik meg...
   $TLWDIR/g-tlkill.exe `cat $TLWWWPID`

   # sikerult-e leallitani?
   cnt=0
   PidRun $TLWWWPID
   while [ $cnt -lt 8 -a -f $TLWWWPID ]; do
      cnt=$[$cnt+1]
      sleep 1
      PidRun $TLWWWPID
   done

   # Ha nem allt le, probaljuk a sima kill: ABRT es KILL
   PidRun $TLWWWPID
   if [ -f $TLWWWPID ]; then
      echo -n " ABRT"
      kill -ABRT `cat $TLWWWPID` 2> /dev/null
      sleep 1
      PidRun $TLWWWPID
      if [ -f $TLWWWPID ]; then
        echo -n " KILL"
        kill -KILL `cat $TLWWWPID` 2> /dev/null
        sleep 1
      fi
   fi

   # Na most mar leallt?
   PidRun $TLWWWPID
   if [ -f $TLWWWPID ]; then
      echo " leallitasa sikertelen."
      exit 1
   else
      echo " OK"
      rm -f $TLLOCKFILE 2> /dev/null
   fi

   # Utana azert egy ellenorzes, hatha csak pid torlodott???
   sleep 1
   CheckProgRun
}

# -- info ---
print_log()
{
   ## Mutassuk meg a TLWWWLOG veget - ha mar van
   if [ -s $TLWWWLOG ]; then
       tail -n 15 $TLWWWLOG
   elif [ -s $TLWWWLOG.0 ]; then
        tail -n 15 $TLWWWLOG.0
   elif [ -s $TLWWWLOG.1 ]; then
        tail -n 15 $TLWWWLOG.1
   else
       echo "Nincs $TLWWWLOG - A TLWWW$par meg nem futott"
   fi
}

print_tlgw_log()
{
   ## Mutassuk meg a TLWWWLOG veget - ha mar van!
   if [ -f $TLGWLOG ]; then
       tail -n 24 $TLGWLOG
   else
       echo "Nincs $TLGWLOG - A TLWWW$par meg nem futott"
   fi
}

print_pid()
{
  unset X
  ls /var/run/tlwww*/*.pid 2> /dev/null > /dev/null
  if [ $? == 0 ]; then
    xxx=$(ls /var/run/tlwww*/*.pid)
    xxx=${xxx//\/var\/run\/}
    xxx=${xxx//textlib\.}
    xxx=${xxx//textlib\/}
    xxx=${xxx//\.pid/}
    X="$X $xxx"
  fi
  ls /home/textlib/.textlib/tlwww*.pid 2> /dev/null > /dev/null
  if [ $? == 0 ]; then
    xxx=$(ls /home/textlib/.textlib/tlwww*.pid)
    xxx=${xxx//\/home\/textlib\/\.textlib\/}
    xxx=${xxx//\.pid/}
    X="$X $xxx"
  fi
  if [ "$X" != "" ]; then
    echo $X
  else
    echo "Nem fut egyetlen TLWWW sem"
  fi
}

#--- ---
print_ver()
{
   unset LOGFILE
   if [ -s $TLWWWLOG ]; then
        LOGFILE=$TLWWWLOG
   elif [ -s $TLWWWLOG.0 ]; then
        LOGFILE=$TLWWWLOG.0
   elif [ -s $TLWWWLOG.1 ]; then
        LOGFILE=$TLWWWLOG.1
   fi
   if [ "$LOGFILE" != "" ]; then
       grep 'WWW Szerver V' $LOGFILE | tail -n 1
       grep 'WWW V' $LOGFILE | tail -n 1
   fi
}

#:::::::: Start :::::::::::::::

touch $TLWWWLOG 2> /dev/null
# Ez sikerul minden textlib group tagnak.
chgrp textlib $TLWWWLOG 2> /dev/null
# Ez csak a root-nak sikerul
chmod g+rw $TLWWWLOG 2> /dev/null
touch $TLGWLOG 2> /dev/null
chgrp textlib $TLGWLOG 2> /dev/null
chmod g+rw $TLGWLOG 2> /dev/null

PidRun $TLWWWPID
case "$1" in
    start)
        if [ -f $TLWWWPID ]; then
            echo "TextLib     WWW$par: fut"
            exit 0
        fi
        if [ "$2" == "info" ]; then
            tlwww_start $3 $4 $5 $6
            sleep 1
            print_log
        else
            tlwww_start $2 $3 $4 $5
        fi;;


     restart|force-reload)
        if [ ! -f $TLWWWPID ]; then
            echo "TextLib     WWW$par: nem futott."
        else
          tlwww_stop
          sleep 1
        fi
        if [ "$2" == "info" ]; then
            tlwww_start $3 $4 $5 $6
            sleep 1
            print_log
        else
            tlwww_start $2 $3 $4 $5
        fi;;


    reload)
        ;;


    stop)
        if [ -f $TLWWWPID ]; then
           tlwww_stop
        else
           echo "TextLib     WWW$par: nem fut."
           CheckProgRun
           rm -f $TLLOCKFILE 2> /dev/null
        fi
        if [ "$2" == "info" ]; then
           print_log
        fi;;

    stat|status)
        if [ -f $TLWWWPID ]; then
           echo "TextLib     WWW$par: fut."
        else
           echo "TextLib     WWW$par: nem fut."
           rm -f $TLLOCKFILE 2> /dev/null
        fi;;

    info)
        if [ -f $TLWWWPID ]; then
           echo "TextLib     WWW$par: fut."
        else
           echo "TextLib     WWW$par: nem fut."
           rm -f $TLLOCKFILE 2> /dev/null
        fi
        print_log
        ;;

    list)
        X=`ls -d /var/log/tlwww*`
        X=${X//\/var\/log\//}
        echo $X
        ;;

    pid|run)
        print_pid
        ;;

    backup|naplo)
        ;;

    tlgw)
        print_tlgw_log
        ;;

    ver)
        print_ver
        ;;

    *)
        echo "tlwvezer [start] [stop] [restart] [info] [stat]"
        exit 1
        ;;

esac
exit 0

