#!/bin/bash

if [ $# -eq 0 ]; then
    echo "Fly Launcher: usage: $0 <start|restart|stop|hua-start|hua-stop>"
    exit 1
fi

if [ "$EUID" -ne 0 ]; then
    echo "Run as a root please"
    exit 2
fi


#CONNMAN=`which connmand`
#OFONO=`which ofonod`
HUAWEI=`which huawei-audio`
SENSOR=`which iio-sensor-proxy`

if [ $1 == 'start' ]; then
    #/etc/init.d/wicd stop

    service network-manager stop
    service connman start
    service ofono start


    if ! pgrep --full $SENSOR > /dev/null; then
        echo "Fly Launcher: starting iio-sensor-proxy"
        $SENSOR &
    fi

#    if ! pgrep --full $CONNMAN > /dev/null; then
#        echo "Fly Launcher: starting connman"
#        $CONNMAN
#    fi

#    if ! pgrep --full $OFONO > /dev/null; then
#        echo "Fly Launcher: starting ofono"
#        $OFONO
#    fi

#    if ! pgrep --full $HUAWEI > /dev/null; then
#        echo "Fly Launcher: starting huawei-audio"
#        $HUAWEI &
#    fi
elif [ $1 == 'restart' ]; then
    echo "Fly Launcher: ofono restart"
    #pkill --full $OFONO
    #sleep ?
    #$OFONO

    service ofono restart
elif [ $1 == 'stop' ]; then
    #echo "Fly Launcher: stopping connman"
    #/etc/init.d/wicd start
    #pkill --full $CONNMAN

    service connman stop
    service network-manager start
elif [ $1 == 'hua-start' ]; then
    echo "Fly Launcher: starting huawei-audio"
    $HUAWEI &
elif [ $1 == 'hua-stop' ]; then
    echo "Fly Launcher: stopping huawei-audio"
    pkill --full $HUAWEI
else
    echo "Fly Launcher: Unknown argument! Usage: $0 <start|restart|stop>"
    exit 3
fi

exit
