#!/bin/bash
#
# Copyright (c) 2019 Clementine Computing LLC.
# 
# This file is part of PopuFare.
# 
# PopuFare is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# PopuFare is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with PopuFare.  If not, see <https://www.gnu.org/licenses/>.
#

export BASEDIR="/home/bus"

. $BASEDIR/bin/common_values.sh

SERIAL_PORT="/dev/ttyGPRS"
PORT_CONFIG="115200 -raw"
REPLY_TIMEOUT="3"
RAW_DROPFILE="/tmp/raw_net_ids"
COOKED_DROPFILE="$NETWORK_ID_DROPFILE"

function collect_modem_results
{
	regex="$1"
	dest="$2"
	
	while read line; do
		match="`echo "$line" | egrep "$regex"`"
		echo RX: $line >> $RAW_DROPFILE
		echo MATCH: $match >> $RAW_DROPFILE
		if [ -n "$match" ]; then
			echo DONE. >> $RAW_DROPFILE
			echo "$match" >> $dest
			exit 0;
		fi
	done
}

function query_modem
{
	query_command="$1"
	reply_regex="$2"

  if [[ ! -e $SERIAL_PORT ]] ; then return ; fi

	if [ -z "$query_command" ]; then
		query_command="AT"
	fi

	if [ -z "$reply_regex" ]; then
		reply_regex='.*'
	fi

	dest_name="/tmp/$$.`date +%s`.tmp"
	echo -n > $dest_name

	(collect_modem_results "$reply_regex" $dest_name < $SERIAL_PORT) 2>&1 > /dev/null &
	serial_coprocess="$!"

	sleep 2
	
	echo TX: $query_command >> $RAW_DROPFILE
	echo -e "$query_command\r" > $SERIAL_PORT

	(sleep $REPLY_TIMEOUT; echo TIMEOUT >> $RAW_DROPFILE; kill $serial_coprocess) 2>&1 > /dev/null &
	watchdog_coprocess="$!"

	wait $serial_coprocess 2>&1 > /dev/null
	echo "`cat $dest_name`"
	rm $dest_name
	kill $watchdog_coprocess 2> /dev/null
	
}

function query_mac_address
{
	iface=$1
	
	if [ -z "$iface" ]; then
		iface="eth0"
	fi
	
	#/sbin/ifconfig $iface | head -n 1 | egrep '([0-9a-fA-F][0-9a-fA-F]:?)+' | tr -s ' ' | cut -d' ' -f5
  /sbin/ifconfig $iface | grep -P '^ *ether ' | head -n 1 | egrep -o '([0-9a-fA-F][0-9a-fA-F]:?)+'  | head -n1
}


function setup_modem
{
  if [[ ! -e $SERIAL_PORT ]] ; then return ; fi

	stty -F $SERIAL_PORT $PORT_CONFIG
	query_modem 'AT' 2>&1 > /dev/null
	sleep 2
	#echo -n "+++" > $SERIAL_PORT
	#sleep 2
	query_modem 'ATZ' 'OK' 2>&1 > /dev/null
}

maxtries=5
tries=0

while [[ ! -e $SERIAL_PORT && $tries -lt $maxtries ]] ; do
  echo "# $tries / $maxtries"
  sleep 5
	tries=$((tries + 1))
done

if [[ ! -e $SERIAL_PORT ]] ; then
  echo "IMEI = " > $COOKED_DROPFILE
  echo "IMSI = " >> $COOKED_DROPFILE
  echo "ETH0 = `query_mac_address eth0 2> /dev/null`"  >> $COOKED_DROPFILE
  exit
fi

setup_modem
query_modem 'AT+CMEE=1'	'OK' 2>&1 > /dev/null

tries=0

while !(echo $imei | egrep '[0-9]+') && [ $tries -lt $maxtries ]; do
	imei="`query_modem 'AT+CGSN' '[0-9]+|.*ERROR.*' 2> /dev/null`"
	tries=$((tries + 1))
done

tries=0

while !(echo $imsi | egrep '[0-9]+') && [ $tries -lt $maxtries ]; do
	imsi="`query_modem 'AT+CIMI' '[0-9]+|.*ERROR.*' 2> /dev/null`"
	tries=$((tries + 1))
done

echo "IMEI = $imei" > $COOKED_DROPFILE
echo "IMSI = $imsi" >> $COOKED_DROPFILE
echo "ETH0 = `query_mac_address eth0 2> /dev/null`"  >> $COOKED_DROPFILE
echo "PPP0 = `query_mac_address ppp0 2> /dev/null`"  >> $COOKED_DROPFILE

