#!/bin/bash

# Written by Charels Barcza GPSD init script in 2007 for blackPanther OS 
# GPS check a notification script the how you connected to pc a gps device

lockdir=/var/run
sockfile=gpsd.sock
pidfile=gpsd.pid
lockfile=gpscheck.lock
dfile=/dev/gps
debug="0"
log="echo"

if test $debug = "1"
then log="logger" 
fi

check() {
if [ -f $lockdir/$lockfile ]; then
    find $lockdir -name $lockfile -cmin +1 -exec rm {} \;
    $log "***************** GPSDCHECK: Find old lockfile ********************"
fi
AVA='/bin/ps -C gpsd --format args --noheaders'
if $AVA | grep gpsd > /dev/null 2>&1; then
     $log "***************** GPSDCHECK: Daemon OK      ***********************"
     #exit
 else
     $log "*************** GPSDCHECK: Daemon NOT Found ***********************"
    bubblemsg error "GPSD!" "GPS daemon (gpsd) not ready just yet. Please first run gpsd from init and after reconnect GPS device again!"
    exit
fi
if [ -f $lockdir/$lockfile ]; then
    $log "***************** Already run GPSCHECK      ***********************"
    #touch $lockdir/$pidfile
    sleep 10 && cleanlock
    $log "***************** RM old $lockdir/$lockfile  ******************"
    exit
 else
    $log "***************** GPSDCHECK Start           ***********************"
    touch $lockdir/$lockfile
fi
devfile=`ls -lR /dev/gps`
ret=$?
if [ $ret = 0 ] ; then 
    $log "***************** GPSCHECK DEV Found: `echo $devfile | awk  '{print $11}'` linked to /dev/gps ****"
    daemonize
 else
    bubblemsg other "GPS Device Detect" "GPS device connected to PC (here:$dfile). Please wait for making connection.."
    $log "***************** GPSCHECK: DEV Not Found, wait.. *****************"
    sleep 5
    contr
fi
}

cleanlock() {
    rm -f $lockdir/$lockfile
}

daemonize() {

AVA='/bin/ps -C gpsd --format args --noheaders'

if $AVA | grep '/dev/gps' > /dev/null 2>&1; then
    devfile=`ls -lR /dev/gps`
    ret=$?
    if [ $ret = 0 ]&&[ ! -f $lockdir/$pidfile ]; then
	AVAR=`/bin/ps -C gpsd --format args --noheaders`
	$log "***************** GPSDCHECK: Reconnect stop. **********************"
	#exit
     else        
        sleep 5
        rm -f $lockdir/$pidfile
	$log "***************** GPSDCHECK Reconnect, freezed?  ******************************"
	bubblemsg other "GPS Device Detection" "This GPS device You are reconneted. (Freezed device ?) 
	<i>Please restart PC reconnect GPS and waiting for device initializing..</i>"
#    sleep 10 && cleanlock
    exit
    fi
fi
$log "***************** GPSD Starting....         ***********************"
bubblemsg other "GPS Device Test." "Initializing, please wait (5-10sec..)"
sleep 5

if [ -x /usr/bin/bubblemsg ];then
    bubblemsg other "GPS Device Detect..." "GPS Device (Port: $dfile) with GPS Daemon start done...</b>"
 else
    zenity --notification --text "GPS Device Detect..." "GPS Device (Port: $dfile) with GPS Daemon start done...</b>" &
fi
$log "***************** GPSD Running and use $dfile device  ***********"
}

contr() {
devfile=`ls -lR /dev/gps`
ret=$?
if [ $ret = 0 ] ; then 
    $log "***************** GPSCHECK DEV Now found and on using done! *******"
    daemonize
 else
    $log "***************** GPSCHECK ERROR! Device where found ? ***************"
    bubblemsg error "New GPS Device Detect (!!)" "ERROR:New GPS device connected but not found !"
    exit
fi
}
check
