#!/bin/sh
# /etc/rc.d/init.d/toughrad
#
# toughradius service script 
# https://github.com/talkincode/ToughRADIUS
#
# chkconfig: 345 91 05
# description: toughrad service
# processname: toughrad

# Source init functions
if [ -f /etc/rc.d/init.d/functions ]
then
    . /etc/rc.d/init.d/functions
fi

appdir=/opt/toughradius
rundir=/var/toughradius

usage () 
{
        cat <<EOF
Usage: $0 [OPTIONS]
  start              start toughradius and mysql (if mysql not running),not support docker
  stop               stop toughradius 
  restart            restart toughradius, not support docker
  status             show toughradius run status
  docker             start toughradius in docker container    
  upgrade            update toughradius version and restart
  startdb            start mysqld_safe
  stopdb             stop mysqld_safe
  backupdb           backup toughradius database and send to ftp
  tracedb            trace mysqld log
  tracerad           trace toughradius radius log
  traceadmin         trace admin console log
  tracecustomer      trace customer console log
     
All other options are passed to the toughrad program.

EOF
        exit 1
}

# not support docker
start()
{
    if [ -x /usr/bin/mysqld_safe ]
    then
        if [ ! $(pgrep -f mysqld_safe | wc -l) -gt 0 ]
        then
            /usr/bin/mysqld_safe --defaults-file=${rundir}/mysql/my.cnf --user=mysql &
        fi
    fi
    
    if [ $( pgrep -f supervisord | wc -l ) -gt 0 ]
    then 
        supervisorctl start all 
    else 
        supervisord -c ${rundir}/supervisord.conf
    fi 
}

# support docker start
docker()
{
    if [ ! -f ${rundir}/install.log ]
    then
        # application not init,exec setup
        sh ${appdir}/install/docker-install.sh setup
    else
        # database not running, start it
        if [ ! $(pgrep -f mysqld_safe | wc -l) -gt 0 ]
        then
            /usr/bin/mysqld_safe --defaults-file=${rundir}/mysql/my.cnf --user=mysql &
        fi
        
         # toughradius not running, start it
        if [ ! $( pgrep -f supervisord | wc -l ) -gt 0 ]
        then 
            supervisord -n -c ${rundir}/supervisord.conf
        fi 
    fi
}

# support docker
stop()
{
    if [ $( pgrep -f supervisord | wc -l ) -gt 0 ]
    then 
        supervisorctl stop all
        supervisorctl shutdown
    else
        echo "supervisord not running"
    fi 
}

# support docker
status()
{
    if [ $( pgrep -f supervisord | wc -l ) -gt 0 ]
    then 
        supervisorctl status
    else 
        echo "supervisord not running"
    fi 

}

# not support docker
restart()
{
    if [ $( pgrep -f supervisord | wc -l ) -gt 0 ]
    then 
        supervisorctl restart all
    else 
        supervisord -c ${rundir}/supervisord.conf
    fi 
}

# support docker
upgrade()
{
    echo 'starting upgrade...' 
    cd ${appdir} && git pull
    supervisorctl restart all
    supervisorctl status
    echo 'upgrade done'
}

# support docker
startdb()
{
    /usr/bin/mysqld_safe --defaults-file=${rundir}/mysql/my.cnf --user=mysql &
}

# support docker
stopdb()
{
    mysqladmin --defaults-file=${rundir}/mysql/my.cnf -uroot shutdown
}

# support docker
backupdb()
{
    python ${appdir}/bin/toughctl -backup -c ${rundir}/radiusd.conf
}

tracedb()
{
    echo 'type ctrl-c exit'
    tail -f ${rundir}/log/mysqld.log
}

tracerad()
{
    echo 'type ctrl-c exit'
    tail -f ${rundir}/log/radiusd.log
}

traceadmin()
{
    echo 'type ctrl-c exit'
    tail -f ${rundir}/log/admin.log
}

tracecustomer()
{
    echo 'type ctrl-c exit'
    tail -f ${rundir}/log/customer.log
}

case "$1" in

  help)
    usage
  ;;

  start)
    start
  ;;
  
  stop)
    stop
  ;;
  
  status)
    status
  ;;    
  
  restart)
    restart
  ;;    
  
  upgrade)
    upgrade
  ;;  
  
  startdb)
    startdb
  ;; 
  
  stopdb)
    stopdb
  ;;  
  
  backupdb)
    backupdb
  ;;   
  
  docker)
    docker
  ;;  
  
  tracedb)
    tracedb
  ;;     
  
  tracerad)
    tracerad
  ;;     
  
  traceadmin)
    traceadmin
  ;;     
  
  tracecustomer)
    tracecustomer
  ;;     


  *)
   usage
  ;;

esac