#! /bin/bash
#small wrapper around hostapd
HOSTAPD=/usr/sbin/hostapd
HOSTAPD_EDI=/usr/sbin/hostapd.edimax
CONFIGBASE=/etc/hostapd/hostapd.conf
CONFIGEXTRA="$(cat /proc/mounts | grep boot | cut -d\  -f2)"
[ "$CONFIGEXTRA" != "" ] && CONFIGEXTRA="$CONFIGEXTRA/hostapd.conf"
CONFIGAVNAV="$(cat /proc/mounts | grep boot | cut -d\  -f2)"
[ "$CONFIGAVNAV" != "" ] && CONFIGAVNAV="$CONFIGAVNAV/avnav.conf"
prfx="avnav-hostapd"

if [ "$1" = "-r" ] ; then
  logger -t $prfx "hostapd supervisor started with $*"
  shift
  while [ 1 = 1 ]
  do
    #first write our own pid - so we can be stopped too
    echo $$ > $2
    $1 $3
    if [ "$4" != "" ] ; then
            if ! ip link show $4 up > /dev/null 2>&1 ; then
                logger -t $prfx "interface $4 not visible any more, exiting"
                exit 0
            fi
    fi
    logger -t $prfx "hostapd stopped, restarting $*"
    sleep 2
  done
  exit 0
fi
if [ "$1" = "-s" ] ; then
    if [ "$2" = "" ] ; then
        logger -t $prfx "ERROR: missing parameter net"
        exit 1
    fi
    pidfile=/run/hostapd.$2.pid
    if [ ! -f $pidfile ] ; then
        logger -t $prfx "ERROR: pidfile $pidfile not found, unable to stop"
        exit 1
    fi
    pid=`cat $pidfile`
    if [ "$pid" = "" ] ; then
        logger -t $prfx "ERROR: no pid in $pidfile, unable to stop"
        exit 1
    fi
    logger -t $prfx "stopping hostapd $pid"
    for cpid in `pgrep -P $pid`
    do
        logger -t $prfx "stopping hostapd child $cpid"
        kill $cpid
    done
    kill $pid
    exit 0
fi
if [ $# != 1 ] ; then
    logger -t $prfx "ERROR: invalid number of parameters, 3 expected"
    exit 1
fi
net=$1
logger -t $prfx "avnav hostapd wrapper started for interface $net"
CONFIG=$CONFIGBASE.$net
if [ ! -f $CONFIG ] ; then
  $CONFIG=$CONFIGBASE.wlan0
fi
if [ ! -f $CONFIG ] ; then
  logger -t $prfx "ERROR: config file $CONFIG not found, cannot start hostapd"
fi
if [ ! -e /sys/class/net/$net ]; then
  logger -t $prfx "ERROR: network interface $net not found, cannot start hostapd"
  exit 1
fi
pidfile=/run/hostapd.$net.pid
drv=`cat /sys/class/net/$net/device/uevent | grep DRIVER `
#as of jessie 2017-03-02 it seems we don't need this any more
#the edimax driver does not seem to work anyway
#so we do not deliver the edimax hostapd - but we do not break old stuff...
hasrtl=`echo $drv | grep -e rtl81 -e r81`
logger -t $prfx "detected driver $drv"
if [ "$hasrtl" != "" ] ; then
    logger -t $prfx "$hasrtl detected"
    if [ -x $HOSTAPD_EDI ] ; then
        logger -t $prfx "using $HOSTAPD_EDI"
        tmpf=/etc/hostapd/$net.edi.conf
        rm -f $tmpf
        sed -e 's/.*driver *=.*/driver=rtl871xdrv/' -e "s/.*interface *=.*/interface=$net/" $CONFIG > $tmpf
        $0 -r $HOSTAPD_EDI $pidfile $tmpf $net &
        exit $?
    else
        logger -t $prfx "$HOSTAPD_EDI not found, starting normal hostapd"
    fi
fi
if [ "$CONFIGAVNAV" != "" -a -f $CONFIGAVNAV ] ; then
 source <(tr -d '\015' < $CONFIGAVNAV)
fi 
tmpf=/etc/hostapd/$net.conf
rm -f $tmpf
common=$CONFIGBASE.common
if [ -f $common ] ; then
  cat $common > $tmpf
fi
if [ "$AVNAV_SSID" != "" ]; then
    logger -t $prfx "using ssid prefix AVNAV_SSID=$AVNAV_SSID for $CONFIGAVNAV"
    sed -e "s/.*interface *=.*/interface=$net/" -e "s/ssid *= *avnav/ssid=$AVNAV_SSID/" $CONFIG >> $tmpf
else
    sed "s/.*interface *=.*/interface=$net/" $CONFIG >> $tmpf
fi
if [ "$CONFIGEXTRA" != "" -a -f $CONFIGEXTRA ] ; then
  logger -t $prfx "adding configuration from $CONFIGEXTRA"
  cat $CONFIGEXTRA >> $tmpf
fi
if [ "$AVNAV_PSK" != "" ] ; then
    logger -t $prfx "using AVNAV_PSK from $CONFIGAVNAV"
    sed -i -e "s/wpa_passphrase *=.*/wpa_passphrase=$AVNAV_PSK/" $tmpf
fi
if [ "$AVNAV_WIFI_COUNTRY" != "" ] ; then
    logger -t $prfx "using AVNAV_WIFI_COUNTRY $AVNAV_WIFI_COUNTRY"
    echo "country_code=$AVNAV_WIFI_COUNTRY" >> $tmpf
fi
logger -t $prfx "starting normal hostapd"
$0 -r $HOSTAPD $pidfile $tmpf $net &


