#!/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
PIDFILE_RADVD=/opt/piratebox/tmp/radvd.pid
PIDFILE_IRC=/opt/piratebox/tmp/irc.pid

PIRATEBOX=/opt/piratebox
CONF=$PIRATEBOX/conf/piratebox.conf
CONF_IRC=$PIRATEBOX/conf/irc.conf
CONF_APN=$PIRATEBOX/conf/hostapd.conf

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

CONF_RADVD=$PIRATEBOX/conf/radvd_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 part2
   $PIRATEBOX/bin/hooks/hook_post_init.sh  "$CONF"
   touch   $PIRATEBOX/conf/init_done
fi


# Recreate the content folder, if it was deleted
#  only if it is not already existing.
#  This can be any time.
if [ ! -d  $WWW_CONTENT ] ; then
        $PIRATEBOX/bin/install_piratebox.sh content
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/ -mindepth 1  -exec rm {} \;

    # Captive Portal Housekeeping file in /tmp (ram)
    "$PIRATEBOX/bin/captive_notify_lease.sh" refresh

    if [ "$CUSTOM_DIRLIST_COPY" = "yes" ]; then
      echo "Copy over directory design files"
      $PIRATEBOX/bin/distribute_files.sh $SHARE_FOLDER/Shared
    fi

    if [ "$PBX_JSON_GENERATION" = "yes" ]; then
        $PIRATEBOX_FOLDER/bin/json_generation.sh  $CONF
    fi

    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


       echo  "Setting up firewall rules..."
       $PIRATEBOX_FOLDER/bin/firewall.sh -s
       echo $?

        if [ "$IPV6_ENABLE" = "yes" ] && [ "$IPV6_ADVERT" = "radvd" ] ; then
          # Ugly hack on PirateBox 1.1.x only
          if test -e "/usr/local/usr/sbin/radvd" && test ! -e "/usr/bin/radvd" ; then
              echo "Deploy ugly hack for radvd on PirateBox 1.1"
              ln -sf /usr/local/usr/sbin/radvd /usr/bin/radvd
          fi
          echo  "Starting radvd..."
          start-stop-daemon  -S -q -x /usr/bin/radvd  --  -p $PIDFILE_RADVD -C $CONF_RADVD
          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
         DROOPY_DO_CHMOD=""
         test -n "$DROOPY_CHMOD" && DROOPY_DO_CHMOD="--chmod $DROOPY_CHMOD"
         start-stop-daemon $DROOPY_USER  -m -S -b  -p $PIDFILE_DROOPY -x $PIRATEBOX/bin/droopy -- -d "$UPLOADFOLDER"  -m "$DROOPY_TXT" $DROOPY_DO_CHMOD $DROOPY_USERDIR  $DROOPY_PORT 
         echo  $?
       fi

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


       #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 

       #Start IRC Server
       if [ "$ENABLE_IRC_SERVER" = "yes" ] ; then
           . $CONF_IRC
           IRC_PARMS="--setuid $IRC_USER --daemon --motd $IRC_MOTD --pid-file $PIDFILE_IRC "

           if [ ! -z "$IRC_STATEDIR" ] ; then
                IRC_PARMS=" $IRCPARMS --statedir $IRC_STATEDIR "
           fi
           echo  "Starting Miniircd..."
           start-stop-daemon -S -x $PIRATEBOX/bin/miniircd.py -- $IRC_PARMS
           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  $?
       rm "$LEASE_FILE_LOCATION"
   fi


       echo  "Removing firewall rules..."
       $PIRATEBOX_FOLDER/bin/firewall.sh -k
       echo $?

     if [ -e $PIDFILE_RADVD ] ; then
        echo "Stopping radvd..."
        start-stop-daemon -K -q -p $PIDFILE_RADVD
        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

    if [ "$ENABLE_IRC_SERVER" = "yes" ] ; then
        echo "Stopping IRC..."
        start-stop-daemon -s 9 -K -q -p $PIDFILE_IRC
        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
