#!/bin/sh /etc/rc.common
START=95


APP="EVENTSMSRELOAD"
lock() {
	trap clean_up SIGINT SIGTERM
	while true; do
		if mkdir /var/lock/$APP; then
			logger -p "user.debug" "$APP$1 $$ : lock"
			break
		else
			logger -p "user.warn" "$APP$1 $$ : wait"
			usleep 500000
		fi
	done
}

unlock() {
	logger -p "user.debug" "$APP$1 $$ : unlock"
	rm -rf  /var/lock/$APP
}

clean_up() {
	logger -p "user.warn" "$APP$1 $$ : clean_up"
	unlock
	exit
}

boot() {
        lock
        #       ps | grep -i event_sms.sh | grep -v grep | awk '{print $1}' | xargs kill
        #       ps | grep -i eventsms | grep -v grep | awk '{print $1}' | xargs kill
        prodid=`cat /proc/device-tree/lantronix/product_id 2>/dev/null`
        if [[ "$prodid" == "U1" ]]; then
        # detect tty ports for cellular module.
        # if there is a serial FTDI on USB port, it will enumberate as ttyUSB0 and cellular module indices will shift by 1
         for x in `ls /dev/ttyUSB*`; do
            r=`sendat $x  AT 5 | grep ^OK | wc -l`;
            logger -p "user.info" "EVENTSMS sendat AT response $x [$r]"
            if [[ $r == 1 ]]; then
                atport=`jq -r '.gsm.module[0].usedatport[0]' /etc/hwinfo.json 2>/dev/null | tr -d '\n'`
                logger -p "user.info" "EVENTSMS configured port: ${atport}, match with $x"
                if [[ "$atport" != "$x" ]]; then
                    isG528=`cat /proc/device-tree/lantronix/pid | grep G528 | wc -l`
                    if [[ isG528 == 1 ]]; then
                            logger -p "user.warn" "EVENTSMS G528 hwinfo: set usedatport $x"
                            jq --arg usedatport "$x" '.gsm.module[0].usedatport[0] = $usedatport' /etc/hwinfo.json > tmp.json && mv tmp.json /etc/hwinfo.json
                            logger -p "user.warn" "EVENTSMS  hwinfo: set usedatport $x"
                            at_index=$(echo "$x" | grep -o '[0-9]\+$');
                            gps_index=$((at_index+1))
                            gpsport="/dev/ttyUSB$gps_index";
                            jq --arg gpsport "$gpsport" '.gsm.module[0].gpsport[0] = $gpsport' /etc/hwinfo.json > tmp.json && mv tmp.json /etc/hwinfo.json
                            logger -p "user.warn" "EVENTSMS hwinfo: set gpsport $gpsport"
                            dm_index=$((at_index+2))
                            dmport="/dev/ttyUSB$dm_index";
                            jq --arg dmport "$dmport"'.gsm.module[0].dmport[0] = $dmport' /etc/hwinfo.json > tmp.json && mv tmp.json /etc/hwinfo.json
                            logger -p "user.warn" "EVENTSMS hwinfo: set dmport $dmport"
                    else
                            jq --arg usedatport "$x" '.gsm.module[0].usedatport[0] = $usedatport' /etc/hwinfo.json > tmp.json && mv tmp.json /etc/hwinfo.json
                            logger -p "user.warn" "EVENTSMS  hwinfo: set usedatport $x"
                            at_index=$(echo "$x" | grep -o '[0-9]\+$');
                            gps_index=$((at_index-1))
                            gpsport="/dev/ttyUSB$gps_index";
                            jq --arg gpsport "$gpsport" '.gsm.module[0].gpsport[0] = $gpsport' /etc/hwinfo.json > tmp.json && mv tmp.json /etc/hwinfo.json
                            logger -p "user.warn" "EVENTSMS hwinfo: set gpsport $gpsport"
                            dm_index=$((at_index-2))
                            dmport="/dev/ttyUSB$dm_index";
                            jq --arg dmport "$dmport"'.gsm.module[0].dmport[0] = $dmport' /etc/hwinfo.json > tmp.json && mv tmp.json /etc/hwinfo.json
                            logger -p "user.warn" "EVENTSMS hwinfo: set dmport $dmport"
                    fi
                    uci set network.cellular.atport="$x"
                    uci commit network
                    logger -p "user.warn" "EVENTSMS network: set atport $x"
                fi
            else
                logger -p "user.info" "EVENTSMS $x is not cellular AT port"
            fi
        done
        fi
        [ -f /etc/eventsms_config ] || event_sms.sh
        /usr/bin/eventsms &> /dev/null &
        unlock
}

start() {
	if [ -z $(ps | grep -i event_sms.sh | grep -v grep | awk '{print $1}') ];then
		/usr/sbin/event_sms.sh
	fi
	/usr/bin/eventsms &> /dev/null &
}

stop() {
	ps | grep -i event_sms.sh | grep -v grep | awk '{print $1}' | xargs kill -15 2>/dev/null
	ps | grep -i eventsms | grep -v grep | awk '{print $1}' | xargs kill -9	2>/dev/null
	return 0
}

reload() {
	lock
	stop
	start
	unlock
}
