#!/bin/sh
#Barry Kauler, LGPL 2009

#If a different modem selected, quit...
MODEM=""
[ -h /dev/modem ] && MODEM="`readlink /dev/modem | grep -v '^ttyHS[0-9]$'`"
[ "$MODEM" != "" ] && exit

[ ! $1 ] && exit
[ "$1" != "start" ] && exit

#Wait for driver to load and determine network port (ttyHS?). Adapted from hso_connect script.
/sbin/pup_event_backend_modprobe_protect --modcheck=hso || exit #v432

#@MODEM_PORT=""
#@until [ "$MODEM_PORT" != "" ];do
#@ TTYS=`find /sys/class/tty -name "ttyHS*"`
#@ for i in $TTYS; do
#@  if [ `grep 'Modem' $i/hsotype` ];then
#@   MODEM_PORT=$i
#@   break
#@  fi
#@ done
until [ "$MODEM" != "" ];do
 MODEM="`grep -l -s 'Modem' /sys/class/tty/ttyHS[0-9][0-9]*/hsotype | head -n 1 | cut -f 5 -d /`"
#@ if [ "$MODEM_PORT" = "" ];then
 if [ "$MODEM" = "" ];then
  WAITCNT=`expr $WAITCNT + 1`
  [ $WAITCNT -gt $WAITMAX ] && break
  sleep 1
 fi
done
[ $WAITCNT -gt $WAITMAX ] \
 && echo "Script Hso: Timeout after $WAITMAX seconds waiting for modem port determination." >> /tmp/udevtrace-modem.log \
 && exit

#@MODEM="`echo $MODEM_PORT | cut -f 5 -d '/'`"
echo "Script Hso: Waited $WAITCNT seconds for module to load and determination of port as $MODEM." >> /tmp/udevtrace-modem.log

#Even if /dev/modem already set, ensure it matches detected modem.
[ -h /dev/modem ] && rm /dev/modem
ln -snf $MODEM /dev/modem
[ -h /dev/ttyS_HS ] && rm /dev/ttyS_HS
ln -snf $MODEM /dev/ttyS_HS #for pupdial probe

#the module doesn't seem to support any country setting, so...
if [ -f /etc/countryinfo ];then
 SPATTERN="s/^MODEM_COUNTRY_STRING.*/MODEM_COUNTRY_STRING=''/"
 cat /etc/countryinfo | sed -e "$SPATTERN" > /tmp/countryinfo
 sync
 mv -f /tmp/countryinfo /etc/countryinfo
fi
#.../usr/sbin/gen_modem_init_string reads this variable.

###END###
