#!/bin/sh
# /etc/init.d/piratebox

### BEGIN INIT INFO
# Provides:          piratebox
# Required-Start:    $all
# Required-Stop:     $network $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      6 1 0
# Short-Description: All services around piratebox
# Description:       Based on /opt/piratebox/conf/piratebox.conf starts:  * DNSMASQ * HOSTAPD * DROOPY * WLAN-config * CHAT
### END INIT INFO

###  
#  Default start-stop script for piratebox services on a laptop
#   written by Matthias Strubel  (matthias.strubel@aod-rpg.de) 2011-02-22 
#  licenced by gpl, feel free to improve and send me comments


[ -f /etc/default/rcS ] && . /etc/default/rcS

# PATH for /opt piratebox folder
PATH=$PATH:/opt/piratebox/bin

PIDFILE_DROOPY=/var/run/piratebox_droopy.pid
PIDFILE_HOSTAPN=/var/run/piratebox_hostapn.pid
PIDFILE_DNSMASQ=/var/run/piratebox_dnsmasq.pid
PIDFILE_LIGHTTPD=/opt/piratebox/tmp/lighttpd.pid
PIDFILE_SHOUTBOX=/opt/piratebox/tmp/shoutbox_daemon.pid


PIRATEBOX=/opt/piratebox
CONF=$PIRATEBOX/conf/piratebox.conf
#CONF_DROOPY=/opt/piratebox/conf/droopy.conf #not used
CONF_APN=$PIRATEBOX/conf/hostapd.conf

#Some extra config files for dnsmasq
CONF_DNSMASQ=$PIRATEBOX/conf/dnsmasq_generated.conf

CONF_LIGHTTPD=$PIRATEBOX/conf/lighttpd/lighttpd.conf 

export PYTHONPATH=:$PYTHONPATH:$PIRATEBOX_PYTHONPATH

if [ -f $CONF ] ; then
   . $CONF 
else 
   echo "Configuration file not found"
   exit 1
fi

#If you can't find this file 
if [[ -f $PIRATEBOX/conf/init_done ]] ; then
   INIT=OK
else
   $PIRATEBOX/bin/hooks/hook_pre_init.sh  "$CONF" 
   $PIRATEBOX/bin/install_piratebox.sh "$CONF" part2
   $PIRATEBOX/bin/hooks/hook_post_init.sh  "$CONF"
   touch   $PIRATEBOX/conf/init_done 
fi


# Command Line for DNSMASQ,  use extra config file generated from command above
CMD_DNSMASQ="-x $PIDFILE_DNSMASQ -C $CONF_DNSMASQ "

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting script piratebox "

    # Generate hosts & dnsmasq file
    $PIRATEBOX/bin/generate_config_files.sh  "$CONF"
    
    $PIRATEBOX/bin/hooks/hook_piratebox_start.sh  "$CONF"

    echo "Empty tmp folder"
    find   $PIRATEBOX/tmp/  -exec rm {} \;
    echo "Copy over design files"
    $PIRATEBOX/bin/distribute_files.sh $SHARE_FOLDER/Shared



    if [ "$DO_IW" = "yes" ] ; then
       log_daemon_msg " Setting up Interface (iw) "
       iw $PHY_IF interface add $INTERFACE type managed
    fi

    if [ "$PROBE_INTERFACE" = yes ] ; then
      echo "  Probe wlan"
      #Setting up WLAN Interface
      piratebox_setup_wlan.sh  $CONF probe
    fi
     if  [ $? -ne 0 ] ;  then
       echo "failed probing Interface"
     else

       # Only  activate
       if  [ "$USE_APN" =  "yes" ] ;  then
         echo  "Starting hostap... "
         start-stop-daemon -m -S -b -p $PIDFILE_HOSTAPN  -x /usr/sbin/hostapd --  $CONF_APN
         echo  $?
       fi

       if [ "$DO_IFCONFIG" = yes ] ; then
         echo "  Setting up wlan"
         #Setting up WLAN Interface
         piratebox_setup_wlan.sh  $CONF start
         if  [ $? -ne 0 ] ;  then
          echo "failed setting up Interface"
          exit 255
         fi
      fi
      #BRIDGE
      if [ "$DO_BRIDGE" = "yes" ] ; then
         echo "Adding $INTERFACE to bridge $BRIDGE //  brctl addif $BRIDGE  $INTERFACE "
         sleep 1
         BR_CMD="brctl addif  $BRIDGE  $INTERFACE"
         ( $BR_CMD ; )
         if  [ $? -ne 0 ] ;  then
           echo "failed :( "
         fi
      fi

       if [ "$USE_DNSMASQ" = "yes" ] ;  then
         echo  "Starting dnsmasq... "
   # pidfile is written by dnsmasq
         start-stop-daemon -S -q -x /usr/sbin/dnsmasq  --  $CMD_DNSMASQ  
         echo  $?
       fi

       if [ "$DROOPY_ENABLED" = "yes" ] ; then
         #Start here the PirateBox-Parts droopy i.e.
         echo "Starting droopy..."
         find  $UPLOADFOLDER/ -iname tmp\* -exec rm {} \;
         #Delete 0 Byte Files
         delete_empty.sh  $UPLOADFOLDER

         DROOPY_USER=""
         if [ "$DROOPY_USE_USER" = "yes" ] ; then
             DROOPY_USER=" -c $LIGHTTPD_USER:$LIGHTTPD_GROUP "
         fi
         start-stop-daemon $DROOPY_USER  -m -S -b  -p $PIDFILE_DROOPY -x $PIRATEBOX/bin/droopy -- -d "$UPLOADFOLDER"  -m "$DROOPY_TXT" --chmod $DROOPY_CHMOD $DROOPY_USERDIR  $DROOPY_PORT 
         echo  $?
       fi

       #Do shoutbox stuff
       $PIRATEBOX/bin/shoutbox_stuff.sh $WWW_FOLDER $CONF


       #Start here the lighttpd i.e.
       echo "Starting lighttpd..."
       start-stop-daemon -S -q  -p $PIDFILE_LIGHTTPD -x /usr/sbin/lighttpd -- -f $CONF_LIGHTTPD
       echo $?

       #Start Global Chat daemon if needed.
       if [ "$GLOBAL_CHAT" = "yes" ] ; then
          echo  "Starting global chat service..."
          start-stop-daemon -S -m -b -p $PIDFILE_SHOUTBOX  -x $PIRATEBOX/bin/shoutbox_daemon.sh -- $CONF
          echo $?
       fi 
     fi

      $PIRATEBOX/bin/hooks/hook_piratebox_start_done.sh  "$CONF"

    ;;
  stop)
    echo  "Stopping script piratebox"

    $PIRATEBOX/bin/hooks/hook_piratebox_stop.sh  "$CONF"

    if [  "$USE_APN"  = "yes" ] ;  then
       echo  "Stopping hostap... "
       start-stop-daemon -K  -q  -p $PIDFILE_HOSTAPN 
       echo   $?
    fi
    if [ "$USE_DNSMASQ" = "yes" ] ;  then 
       echo "Stopping dnsmasq..."
       start-stop-daemon -K  -q  -p $PIDFILE_DNSMASQ  
       echo  $?
    fi

    if [ "$DROOPY_ENABLED" = "yes" ] ; then
       #Kill Droopy
       echo "Stopping droopy... "
       start-stop-daemon  -s 9 -K  -q -p  $PIDFILE_DROOPY 
       echo  $?
    fi

    #Start Global Chat daemon if needed.
    if [ "$GLOBAL_CHAT" = "yes" ] ; then
        echo  "Stopping global chat service..."
        start-stop-daemon -K  -q -p $PIDFILE_SHOUTBOX
        echo $?
    fi 



    echo "Stopping lighttpd..."
    start-stop-daemon -K -q -p $PIDFILE_LIGHTTPD
    echo $?


     if [ "$DO_IFCONFIG" = yes ] ; then
       piratebox_setup_wlan.sh $CONF stop
     fi

     if [ "$DO_IW" = "yes" ] ; then
       iw dev $INTERFACE del
     fi

      #BRIDGE
      if [ "$DO_BRIDGE" = "yes" ] ; then
         echo "Remove  $INTERFACE to bridge $BRIDGE //  brctl addif 
$BRIDGE  $INTERFACE "
         sleep 1
         BR_CMD="brctl delif  $BRIDGE  $INTERFACE"
         ( $BR_CMD ; )
         if  [ $? -ne 0 ] ;  then
           echo "failed :( "
         fi
      fi

      $PIRATEBOX/bin/hooks/hook_piratebox_stop_done.sh  "$CONF"
    ;;
  *)
    echo "Usage: /etc/init.d/piratebox {start|stop}"
    exit 1
    ;;
esac

exit 0
