#!/bin/sh /etc/rc.common
START=99
APP="GPS_script"
lock() {
    while true; do
        if mkdir /var/lock/$APP; then
            logger -p "user.debug" "$APP$1 $$ : lock"
            break
        else
            logger -p "user.debug" "$APP$1 $$ : wait"
            usleep 500000
        fi
    done
}
unlock() {
    logger -p "user.debug" "$APP$1 $$ : unlock"
    /usr/bin/logger -t $APP -p info "Copying GPS config to /tmp/gps"
    cp /etc/config/gps /tmp/gps
    rm -rf  /var/lock/$APP
}

boot() {
    lock
    /usr/bin/logger -t $APP -p info "boot GPS"
    touch /tmp/last_nofix
    touch /tmp/last_fix
    if [ ! -f  /etc/last_nofix -o ! -h /etc/last_nofix ]; then
        rm -f /etc/last_nofix
        ln -s /tmp/last_nofix /etc/last_nofix
    fi
    if [ ! -f  /etc/last_fix -o ! -h /etc/last_fix ]; then
        rm -f /etc/last_fix   
        ln -s /tmp/last_fix /etc/last_fix
    fi
    start
    unlock
    return 0
}

changed=0
function handle_gps_area()
{
    config=$1
    custom=$2
    logger -t $APP -p info "Check area $config"
    fences=`uci -q get gps.$config.fence`
    for x in $fences; do
        type=`uci -q get gps.$x`
        if [[ "$type" != "fence" ]]; then
                logger -t $APP -p warning "Area $config Fence $x does not exist!"
                uci -q del_list gps.$config.fence=$x
                changed=1
        fi
    done
}
function handle_gps_waypoint()
{
    changed=0
    config=$1
    custom=$2
    logger -t $APP -p info "Check waypoint $config"
    route=`uci -q get gps.$config.route`
    if [[ "$route" == "" ]]; then
        logger -t $APP -p warning "Waypoint $config: unknown format"
        uci del gps.$config
    else
        type=`uci -q get gps.$route`
        if [[ "$type" != "route" ]]; then
             logger -t $APP -p warning "Waypoint $config: route $route does not exist"
             uci del gps.$config
             changed=1
        fi
    fi
}
function handle_gps_route()
{
    changed=0
    config=$1
    custom=$2
    logger -t $APP -p info "Check route $config"
    waypoints=`uci -q get gps.$config.waypoints`
    for x in $waypoints; do
        type=`uci -q get gps.$x`
        if [[ "$type" != "waypoint" ]]; then
                logger -t $APP -p warning "Route $config Waypoint $x does not exist!"
                uci -q del_list gps.$config.waypoints=$x
                changed=1
        fi
    done
}
start() {
    /usr/bin/logger -t $APP -p info "start at GPS"
    /usr/sbin/event_sms.sh
    enable=`uci get gps.@debug[0].gps_enable`
    if [ "$enable" == "1" ];then  
        /usr/bin/logger -t $APP -p info "Start GPS"
        /usr/sbin/gps_start.sh enable
        /usr/sbin/gps_start.sh start
        /usr/sbin/gps_s.sh &
#let the processes start
        sleep 5
    fi
    if grep -q "G526RP" "/proc/device-tree/lantronix/hwcfg";then
        /usr/bin/logger -t $APP -p info "This is transport pack"
        config_load gps
        config_foreach handle_gps_area area "$@"
        if [[ $changed -ne 0 ]]; then
            uci commit gps
        fi
        gps_geofence &> /dev/null &
        config_load gps
        # config_foreach handle_gps_waypoint waypoint "$@"
        # if [[ $changed -ne 0 ]]; then
            # uci commit gps
        # fi
        # config_load gps
        # config_foreach handle_gps_route route "$@"
        # if [[ $changed -ne 0 ]]; then
            # uci commit gps
        # fi
        for i in /etc/config/routes_*; do
            #logger -t $APP -p info "checking waypoints $i!"
            route=`echo $i | awk -F'/etc/config/routes_' '{print $2}'`
            #logger -t $APP -p info "checking route $route!"
            type=`uci -q get gps.$route`
            #logger -t $APP -p info "checking route $route!, type:$type" 
            if [[ "$type" != "route" ]]; then
                logger -t $APP -p warn "route $route does not exist!, clearing waypoints!"
                rm -rf $i
            fi	
        done
        gps_route &>/dev/null &
        pgrep alerts | xargs kill -1 2 > /dev/null    
    fi
    return 0
}
save_last_fix=0
stop_soft() {
    /usr/bin/logger -t $APP -p info "Soft Stop GPS"
    ps | grep -i gps_s.sh | grep -v grep | awk '{print $1}' | xargs kill -9 2>/dev/null
    /usr/sbin/gps_start.sh stop
    rm -f /tmp/gpsdatafiles/*
    if [ $save_last_fix == 0 ]; then
        rm -f /tmp/gpsfile
    fi
    ps | grep -i gps_geofence | grep -v grep | awk '{print $1}' | xargs kill -9 2>/dev/null
    ps | grep -i gps_route | grep -v grep | awk '{print $1}' | xargs kill -9 2>/dev/null
}
stop() {
    /usr/bin/logger -t $APP -p info "Stop GPS"
    ps | grep -i gps_s.sh | grep -v grep | awk '{print $1}' | xargs kill -9 2>/dev/null
    /usr/sbin/gps_start.sh completestop
    rm -f /tmp/gpsdatafiles/*
    if [ $save_last_fix == 0 ]; then
        rm -f /tmp/gpsfile
    fi
    ps | grep -i gps_geofence | grep -v grep | awk '{print $1}' | xargs kill -9 2>/dev/null
    ps | grep -i gps_route | grep -v grep | awk '{print $1}' | xargs kill -9 2>/dev/null
}
reload() {
    lock
    /usr/bin/logger -t $APP -p info "Reload GPS"
    if [ ! -f  /etc/last_fix -o ! -h /etc/last_fix ]; then
        /usr/bin/logger -t $APP -p info "Let GPS boot"
        unlock
        return 0
    fi
    if grep -q "G526RP" "/proc/device-tree/lantronix/hwcfg"; then
        #/usr/bin/logger -t $APP -p info "Checking if it is just a parking change"
        #total_changes=`diff -bBrU 0 /tmp/gps /etc/config/gps | grep '^+[^+]' | wc -l`
        #parking_changes=`diff -bBrU 0 /tmp/gps /etc/config/gps | grep '^+[^+]' | grep "park_" | wc -l`
        #if [ "$total_changes" != "0" ] && [ "$total_changes" == "$parking_changes" ]; then
        #    ps | grep -i gps_geofence | grep -v grep | awk '{print $1}' | xargs kill -1 2>/dev/null
        #    unlock
        #    return
        #fi
        #/usr/bin/logger -t $APP -p info "There are other changes too"
        #/usr/bin/logger -t $APP -p info "Checking if it is just a trip start/stop change"
        #trip_changes=`diff -bBrU 0 /etc/config/gps /tmp/gps | grep '^+[^+]' | grep "trip_" | wc -l`
        #if [ "$total_changes" != "0" ] && [ "$total_changes" == "$trip_changes" ]; then
        #    ps | grep -i ecodrive | grep -v grep | awk '{print $1}' | xargs kill -1 2>/dev/null
        #    unlock
        #    return
        #fi
        #/usr/bin/logger -t $APP -p info "There are other changes too"
        enable=`uci get gps.@debug[0].gps_enable`
        if [ "$enable" == "1" ];then
            /usr/bin/logger -t $APP -p info "GPS is enabled, save the last fix"
            save_last_fix=1
        else
            /usr/bin/logger -t $APP -p info "GPS is disabled, clear the last fix"
            save_last_fix=0
        fi
        parking=`uci -q get gps.@debug[0].park_enable || echo "0"`
        if [[ "$parking" != "1" ]]; then
            uci del gps.@debug[0].park_lat
            uci del gps.@debug[0].park_long
            uci del gps.@debug[0].park_status 
        fi
    fi
    stop_soft
#let the processes terminate
    sleep 5
    start
    unlock
}

