#!/bin/sh
#(c)copyright Barry Kauler modem wizard 2003,2004,2005. www.puppylinux.org
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)

KERNVER="`uname -r`"

MODTYPE="ko"
MODCONF="modprobe.conf"
#k2.6, need to load core serial modules...
#modprobe 8250_pci 2> /dev/null
#...no, i changed config so built-in the kernel. but, still need for 2.6.11.7 build.

PCISUMMARY="`lspci -b`"
#v2.12 /proce/pci no longer available...
#MODEMINFOx="`cat /proc/pci | grep -i "modem" | head -n 1`"
##outputs a leading line number...
#MODEMINFO2="`cat /proc/pci | grep -ni "modem" | head -n 1`"
#if [ "$MODEMINFO2" ];then
# MODEMLINE2=`echo -n $MODEMINFO2 | cut -f 1 -d ':'`
# MODEMLINE1=`expr $MODEMLINE2 - 1`
# MODEMINFO1="`cat /proc/pci | head -n $MODEMLINE1 | tail -n 1`"
# BUSNUMBER="`echo -n $MODEMINFO1 | tr -s ' ' | cut -f 2 -d ' ' | cut -f 1 -d ','`"
# DEVNUMBER="`echo -n $MODEMINFO1 | tr -s ' ' | cut -f 4 -d ' ' | cut -f 1 -d ','`"
# FUNCNUMBER="`echo -n $MODEMINFO1 | tr -s ' ' | cut -f 6 -d ' ' | cut -f 1 -d ':'`"
# VENDCHIPNUMS="`lspci -b | grep "$BUSNUMBER:$DEVNUMBER.$FUNCNUMBER" | tr -s ' ' | cut -f 2 -d ' '`"
#fi

DEVM=""
IRQM=""

MYCOUNTRY=0 #use modem default.
countryfunc() {
 MYCOUNTRY="`Xdialog --wmclass "smartlinkwizard" --title "Smart-Link モデムウィザード" --stdout --no-tags --menubox "国を選択して下さい:"  0 0 0 0 'MODEM DEFAULT' 1 USA 2 France 3 Germany 4 Italy 5 Sweden 6 UK 7 Japan 8 Australia 9 Spain 10 Taiwan 11 Singapore 12 Korea 13 Switzerland 14 Norway 15 Netherlands 16 Belgium 17 Canada 18 Ireland 19 Portugal 20 Poland 21 Hungary 22 Finland 23 Denmark 24 Austria 25 'South Africa' 26 'CTRL21 countries' 27 China 28 Malaysia 29 Luxumburg 30 Greece 31 Iceland 32 'New Zealand' 33 Brazil 2>/dev/null`"
 [ ! "$MYCOUNTRY" = "" ] && echo "$MYCOUNTRY" > /etc/countrycode
 [ "$MYCOUNTRY" = "" ] && MYCOUNTRY=0
 [ "$MYCOUNTRY" = "0" ] && rm -f /etc/countrycode
}

# return the IRQ that respond to a given device
irq_from_device()
{
 local dev=$1
 #v1.0.2 it seems that setserial may be more trouble than its worth...
 #in the case of linmodems it often doesn't work...
 set -- `setserial -v -b $dev auto_irq \
 skip_test autoconfig session_lockout`
 [ "$6" ] && echo $6 | tr -d \) 
}

# kill processing attached to device
kill_users()
{
[ "`which fuser`" ] && fuser -k $1 2>/dev/null
}

#talk to modem, wait for response
chat_with()
{
rm -f /tmp/answer.txt
##note, ^M is equivalent to a carriage-return, and modem
## ...no, that does not work.
##should respond to "AT" with "OK"...
##(
## #echo -e "AT^M"
##  echo -n -e "AT\r\n"
##  tee > /tmp/answer.txt &
## sleep 1
## killall tee
##) < $1 > $1
#dd bs=1 count=10 > /tmp/answer.txt < $1 &
#DDID=$!
#echo -n -e "ATC1\r\n" > $1
#sleep 1
##killall dd 
#kill $DDID

#the above method used to work k2.4, not k2.6. now do it like this...
#no, with one modem at least, it hangs, also only works with atz...
#also hangs if modem unplugged or turned off (contrary to what docs say)...
#modem-stats -c "AT" $1 > /tmp/answer.txt
modem-stats -c "ATZ" $1 > /tmp/answer.txt &
sleep 2
killall modem-stats


if [ -e /tmp/answer.txt ];then
 if [ -s /tmp/answer.txt ];then
  #...filesize is nonzero.
  #the modem should have answered with "OK"...
  grep "^OK" /tmp/answer.txt > /dev/null 2>&1
  [ $? -eq 0 ] && return 1 #success
 fi
fi
return 0
}

testmodem()
{
dev=$1
DEVM=$1
local irq
echo -n "" >/tmp/modemtest.txt
irq=$(irq_from_device $dev)
if [ -z "$irq" ];then
 #wavplay -q /usr/share/audio/error.wav &
 echo "失敗。$dev はIRQが割り当てられていないようです" >>/tmp/modemtest.txt
 echo "このポートには何もないようです。他を試して下さい..." >>/tmp/modemtest.txt
 echo "しかし、他のいくつかのlinmodemsはこのIRQテストでは" >>/tmp/modemtest.txt
 echo "失敗しましたがモデムはまだ働いています。" >>/tmp/modemtest.txt
 echo "ポート $dev のテストを続けます..." >>/tmp/modemtest.txt
 echo " "  >>/tmp/modemtest.txt
 IRQM="unknown"
 #return 1
fi
IRQM="$irq"
kill_users $dev
chat_with $dev
if [ $? -eq 1 ];then
 wavplay -q /usr/share/audio/generic.wav &
 #DEVM="$dev"
 echo "成功。$dev はモデムのようです。IRQ=$irq を使っています。" >>/tmp/modemtest.txt
 echo "しかし不確かならば他をチェックして下さい" >>/tmp/modemtest.txt
 echo -n -e "(確信のあるモデムが見つかったら \"SAVE\" ボタンを押して下さい)"  >>/tmp/modemtest.txt
else
 wavplay -q /usr/share/audio/error.wav &
 #echo "FAIL, there is something at $dev (and irq=$IRQM) but" >/tmp/modemtest.txt
 echo -e "失敗: $dev はモデムではないようです。別の \"tty*\" ポートを試して下さい..." >>/tmp/modemtest.txt
 echo -n -e "(あるいは本当にこれを使いたいならば、\"SAVE\" を押して下さい)" >>/tmp/modemtest.txt
fi
}

TTYLT0='ttyLT0:20,'
TTYSL0='ttySL0:21,'
TTYSESS0='ttyS_ESS0:22,'
TTYSPCTEL0='ttyS_PCTEL0:23,'

BUTTONS="ttyS0":10,"ttyS1":11,"ttyS2":12,"ttyS3":13,"ttyS4":14,${TTYLT0}${TTYSL0}${TTYSESS0}${TTYSPCTEL0}"SAVE":15,"ERASE":16,"HELP":17,"EXIT":18

if [ -e /etc/modemdevice ];then
 DEVM="`cat /etc/modemdevice`"
 echo "以前は $DEVM を選択しました" >/tmp/modemtest.txt
 echo -e "...モデムを使いたくなければ \"ERASE\" ボタンを押して下さい。" >>/tmp/modemtest.txt
 echo >>/tmp/modemtest.txt
 if [ "`echo "$DEVM" | grep --extended-regexp "ttyLT0|ttySL0"`" = "" ];then
  echo "注意。 $DEVM は本当のハードウェアモデムですが、パピーは" >>/tmp/modemtest.txt
  echo "いくつかのソフトウェアモデムをサポートします。" >>/tmp/modemtest.txt
  echo  >>/tmp/modemtest.txt
 fi
 echo -e "もし不確かならば、\"HELP\" ボタンを押して下さい。あるいは" >>/tmp/modemtest.txt
 echo -n -e "\"tty*\" ボタンを押してモデムが存在するかテストして下さい..." >>/tmp/modemtest.txt
else
 DEVM=""
 echo "ようこそ！パピーアナログモデムウィザードで運試しをしましょう。" >/tmp/modemtest.txt

 if [ ! "$MODEMINFO2" = "" ];then
  echo " " >>/tmp/modemtest.txt
  echo "Puppy has found a PCI modem:" >>/tmp/modemtest.txt
  echo "$MODEMINFO1" >>/tmp/modemtest.txt
  #echo "$BUSNUMBER" >>/tmp/modemtest.txt
  #echo "$DEVNUMBER" >>/tmp/modemtest.txt
  #echo "$FUNCNUMBER" >>/tmp/modemtest.txt
  echo -n "The vendor:chip numbers are (hex): " >>/tmp/modemtest.txt
  echo "$VENDCHIPNUMS" >>/tmp/modemtest.txt
 fi

 echo " " >>/tmp/modemtest.txt
 echo "これが輝かしいLinmodem (リナックス互換ソフトウェアモデム)だと思うなら" >>/tmp/modemtest.txt
 echo -e "テストするには \"ttyLT0\" ボタンを押して下さい。" >>/tmp/modemtest.txt
 echo " " >>/tmp/modemtest.txt

 echo "これがAMR/CNR/MDC/ACR PCI linmodemベースのSmart Link HAMR5600、あるいは" >>/tmp/modemtest.txt
 echo "PCI linmodemベースのSmartPCI56/561/562/563、" >>/tmp/modemtest.txt
 echo "または USB linmodemベースのSmartUSB56と思うなら、\"ttySL0\" ボタンを押して下さい。" >>/tmp/modemtest.txt
 echo " " >>/tmp/modemtest.txt

 echo "ESS 2898ソフトモデムでしたら、\"ttyS_ESS0\" をクリックして下さい。" >>/tmp/modemtest.txt
 echo >>/tmp/modemtest.txt
 
 echo "PCTEL pct789ソフトモデムでしたら、\"ttyS_PCTEL0\" をクリックして下さい。" >>/tmp/modemtest.txt
 echo >>/tmp/modemtest.txt

 echo "PCI、ISAバスあるいは外部シリアルのハードウェアモデムがあるならば"  >>/tmp/modemtest.txt
 echo -e "\"ttyS*\" ボタンの1つを押してどのポートが開いているか見つける" >>/tmp/modemtest.txt
 echo "事ができます。" >>/tmp/modemtest.txt
 echo "注意。 ttyS0 は COM1、 ttyS1 は COM2、などです。" >>/tmp/modemtest.txt
 echo " " >>/tmp/modemtest.txt

 echo -e "不確かならば、\"HELP\" ボタンを押して下さい。あるいは" >>/tmp/modemtest.txt
 echo -en "\"tty*\" ボタンを押してモデムが存在するかどうかテストして下さい..." >>/tmp/modemtest.txt

fi

while :; do
gxmessage -bg "#c0c0ff" -center -name "xutilities" -title "Puppyモデムウィザード" -buttons \
 "$BUTTONS" -file /tmp/modemtest.txt

case ${?} in
   10)# ttyS0
     testmodem /dev/ttyS0
     ;;
   11)# 
     testmodem /dev/ttyS1
     ;;
   12)# 
     testmodem /dev/ttyS2
     ;;
   13)# 
     testmodem /dev/ttyS3
     ;;
   14)# 
     testmodem /dev/ttyS4
     ;;
   15)# = save
     if [ ! "$DEVM" ];then
      wavplay -q /usr/share/audio/error.wav &
      gxmessage -bg "#ffc0c0" -center -name "xutilities" -title "Puppyモデムウィザード" -file -<<MSG1
Huh? You have not yet selected a modem port,
so nothing to save.
MSG1
     else
      echo -n "$DEVM" >/etc/modemdevice
      #note, rc.local0 will link /dev/modem to this at bootup,
      #however do it now also (not remembered next bootup)...
      rm -f /dev/modem
      ln -s $DEVM /dev/modem
      wavplay -q /usr/share/audio/ok.wav &
      echo "パピーを $DEVM (IRQ=$IRQM)用に設定しました" >/tmp/modemtest.txt
      #echo "Note, a dialup icon has been created on the desktop," >>/tmp/modemtest.txt
      #echo "but you need to restart the window manager to see it." >>/tmp/modemtest.txt
      echo "注意。デスクトップに「接続」アイコンがあります。" >>/tmp/modemtest.txt
      echo -n -e "出ていくには \"EXIT\" ボタンを押して下さい..." >>/tmp/modemtest.txt
      BUTTONS="EXIT:18"
      echo '#!/bin/sh' > /usr/local/bin/defaultconnect
      echo 'exec gkdial' >> /usr/local/bin/defaultconnect
      ##v1.0.5 this also done in rc.local0 if not here...
      #if [ "`cat /root/Choices/ROX-Filer/PuppyPin | grep "defaultdialup"`" = "" ];then
      # if [ "`cat /root/Choices/ROX-Filer/PuppyPin | grep 'x=\"32\" y=\"512\"'`" = "" ];then
      #  echo '<icon x="32" y="512" label="dialup">/usr/local/bin/defaultdialup</icon>' >> /root/Choices/ROX-Filer/PuppyPin
      # else
      #  echo '<icon x="224" y="512" label="dialup">/usr/local/bin/defaultdialup</icon>' >> /root/Choices/ROX-Filer/PuppyPin
      # fi
      # cat /root/Choices/ROX-Filer/PuppyPin | grep -v '/pinboard' > /tmp/PuppyPin-CPY
      # sync
      # cp -f /tmp/PuppyPin-CPY /root/Choices/ROX-Filer/PuppyPin
      # echo '</pinboard>' >> /root/Choices/ROX-Filer/PuppyPin
      #fi
     fi
     ;;
   16)# = erase
     echo "モデムの全ての設定(もしあれば)は消去されました" >/tmp/modemtest.txt
     echo -n -e "出ていくには \"EXIT\" ボタンを押して下さい..." >>/tmp/modemtest.txt
     rm -f /dev/modem
     rm -f /etc/modemdevice
     grep -v --extended-regexp "ltserial|slamr|slusb" /etc/modprobe.conf > /tmp/modprobe.conf
     sync
     mv -f /tmp/modprobe.conf /etc/modprobe.conf
     BUTTONS="EXIT:18"
     ;;
   17)# HELP
     defaulthtmlviewer file:///usr/share/doc/HOWTO_Internet.htm &
     ;;
   18)# = exit
     break;;
   0)# exit
     break;;
   1)# exit
     break;;
   20) #Lucent Linmodem (refer rc.network)
      lsmod | grep "ltserial" > /dev/null 2>&1
      if [ ! $? -eq 0 ];then #=0 found.
       if [ "`cat /etc/$MODCONF | grep "ltserial"`" = "" ];then
        #rc.network will do this properly at next boot...
        echo "alias char-major-62 ltserial" >> /etc/$MODCONF
        echo "alias /dev/ttyLT0 ltserial" >> /etc/$MODCONF
        echo "alias /dev/modem ltserial" >> /etc/$MODCONF
       fi
       #modprobe serial_core 2> /dev/null
       modprobe ltmodem
       modprobe ltserial
       sleep 1
      fi    
      testmodem /dev/ttyLT0
     ;;
   21) #smartlink linmodem (refer rc.network)
      #need to choose country...
      SLCOUNTRY="`Xdialog --wmclass "smartlinkwizard" --title "Smart-Link Modem Wizard" --stdout --no-tags \
 --menubox "国を選択して下さい:"  0 0 0 \
 ALGERIA Algeria \
 ARGENTINA Argentina \
 AUSTRALIA Australia \
 AUSTRIA Austria \
 BAHREIN Bahrein \
 BELGIUM Belgium \
 BRAZIL Brazil \
 BRUNEI Brunei \
 BULGARIA Bulgaria \
 CANADA Canada \
 CHILE Chile \
 CHINA China \
 CTR21EUROPE CTR21_Europe \
 CYPRUS Cyprus \
 CZECH_REPUBLIC Czech_Replublic \
 DENMARK Denmark \
 EGYPT Egypt \
 ESTONIA Estonia \
 FINLAND Finland \
 FRANCE France \
 GERMANY Germany \
 GREECE Greece \
 HONG_KONG Hong_Kong \
 HUNGARY Hungary \
 ICELAND Iceland \
 INDIA India \
 INDONESIA Indonesia \
 IRELAND Ireland \
 ISRAEL Israel \
 ITALY Italy \
 JAPAN Japan \
 JORDAN Jordan \
 KOREA Korea \
 KUWAIT Kuwait \
 LATVIA Latvia \
 LEBANON Lebanon \
 LITHUANIA Lithuania \
 LUXEMBOURG Luxembourg \
 MALAYSIA Malaysia \
 MALTA Malta \
 MEXICO Mexico \
 MOROCCO Morocco \
 NETHERLANDS Netherlands \
 NEW_ZEALAND New_Zealand \
 NORWAY Norway \
 OMAN Oman \
 PAKISTAN Pakistan \
 PERU Peru \
 PHILIPPINES Philippines \
 POLAND Poland \
 PORTUGAL Portugal \
 ROMANIA Romania \
 RUSSIA Russia \
 SAUDIARABIA Saudi_Arabia \
 SINGAPORE Singapore \
 SLOVAKIA Slovakia \
 SLOVENIA Slovenia \
 SOUTHAFRICA South_Africa \
 SOUTHKOREA South_Korea \
 SPAIN Spain \
 SRILANKA Sr_Lanka \
 SWEDEN Sweden \
 SWITZERLAND Switzerland \
 TAIWAN Taiwan \
 THAILAND Thailand \
 TUNISIA Tunisia \
 TURKEY Turkey \
 UAE UAE \
 UK UK \
 URUGUAY Uruguay \
 USA USA \
 VIETNAM Vietnam 2> /dev/null`"

      if [ ! $? -eq 0 ];then
       gxmessage "中止: 国を選択していません！！！"
      else
       #v2.02 rerwin, code block moved down...
       if [ "`cat /etc/$MODCONF | grep "slamr"`" = "" ];then
        #rc.network will do this properly at next boot...
        #echo "#Needed for Smartlink Linmodem..." >> /etc/$MODCONF
        #v2.01 rerwin found 212,213 need to be changed to 242,243...
        echo "alias char-major-242 slamr" >> /etc/$MODCONF
        #v2.02 rerwin...
        echo 'install slamr modprobe --ignore-install ungrab-winmodem ; modprobe --ignore-install slamr' >> /etc/modprobe.conf
        echo "alias char-major-243 slusb" >> /etc/$MODCONF
       fi
       #v2.01 modprobe usbcore 2> /dev/null
       #v2.01 bug, rerwin found needs ungrab-winmodem before load slamr...
       #v2.02 rerwin, rmmod...
       rmmod slamr #unload for ungrab, in case slamr misloaded as serial driver.
       modprobe ungrab-winmodem
       modprobe slamr
       [ ! $? -eq 0 ] && xmessage -center "Failed to load slamr module."
       if [ ! "`lsmod | grep '^usbcore'`" = "" ];then #v2.01
        modprobe slusb
        [ ! $? -eq 0 ] && xmessage -center "Failed to load slusb module."
       fi
       #choose usb or pci modem...
       MYSLMODEM="`Xdialog --wmclass "smartlinkwizard" --title "Smart Link Modem Wizard" --stdout --no-tags \
 --menubox "Smart-Linkモデムのタイプを選択して下さい:"  0 0 0 \
 slusb "USB" \
 slamr "PCI (内蔵カード)" 2> /dev/null`"
       if [ ! $? -eq 0 ];then
        gxmessage "中止: モデムタイプを選択していません！！！"
       else
        #start slmodemd...
        echo "#/bin/sh" > /usr/sbin/slmodemdshell
        echo "/usr/sbin/slmodemd --country=$SLCOUNTRY /dev/${MYSLMODEM}0" >> /usr/sbin/slmodemdshell
        sync
        chmod 755 /usr/sbin/slmodemdshell
        /usr/sbin/slmodemdshell &
        sleep 1
        #test modem...
        testmodem /dev/ttySL0
       fi
      fi     
     ;;
    22) #ESS modem
      #avoid clashes with pctel modules...
      lsmod | grep "^pctel" > /dev/null 2>&1
      if [ $? -eq 0 ];then #=0 found.
       rmmod pctel
       rmmod pctel_hw
       rmmod linmodem
      fi
      #lsmod | grep "^linmodem " > /dev/null 2>&1
      #[ $? -eq 0 ] && rmmod linmodem
      lsmod | grep "^esscom " > /dev/null 2>&1
      if [ ! $? -eq 0 ];then #=0 found.
       rmmod esscom
       rmmod esscom_hw
       rmmod linmodem
      fi
       [ ! $MYCOUNTRY ] && countryfunc #choose country code
       #[ -f /etc/countrycode ] && CNTRYPARAM="country_code=`cat /etc/countrycode`"
       cat /etc/modprobe.conf | grep -v 'options esscom ' > /tmp/modprobeconf.tmp
       sync
       cp -f /tmp/modprobeconf.tmp /etc/modprobe.conf
       if [ ! "$MYCOUNTRY" = "0" ];then
        echo "options esscom country_code=$MYCOUNTRY" >> /etc/modprobe.conf
       fi
       #rc.modules may autoload esscom and has to be told to use linmodem_ess.ko
       #instead of linmodem.ko...
       cat /etc/modprobe.conf | grep '^install linmodem '
       [ ! $? -eq 0 ] && echo 'install linmodem /sbin/modprobe -o linmodem linmodem_ess' >> /etc/modprobe.conf
       ##to avoid conflict with pctel linmodem.ko...
       #modprobe -o linmodem linmodem_ess 2>/dev/null
       modprobe esscom #$CNTRYPARAM #will also load esscom_hw.
       sleep 1
      #fi    
      testmodem /dev/ttyS_ESS0
     ;;
    23) #PCTEL pct789 modem
      #avoid clashes with ess modules...
      lsmod | grep "^esscom" > /dev/null 2>&1
      if [ $? -eq 0 ];then #=0 found.
       rmmod esscom
       rmmod esscom_hw
       rmmod linmodem
      fi
      #lsmod | grep "^linmodem " > /dev/null 2>&1
      #[ $? -eq 0 ] && rmmod linmodem
      lsmod | grep "^pctel " > /dev/null 2>&1
      if [ ! $? -eq 0 ];then #=0 found.
       rmmod pctel
       rmmod pctel_hw
       rmmod linmodem
      fi
       [ ! $MYCOUNTRY ] && countryfunc #choose country code
       cat /etc/modprobe.conf | grep -v 'options pctel ' > /tmp/modprobeconf.tmp
       sync
       cp -f /tmp/modprobeconf.tmp /etc/modprobe.conf
       if [ ! "$MYCOUNTRY" = "0" ];then
        echo "options pctel country_code=$MYCOUNTRY" >> /etc/modprobe.conf
       fi
       #ess setup puts this line, remove it...
       cat /etc/modprobe.conf | grep '^install linmodem '
       if [ $? -eq 0 ];then #0=found.
        cat /etc/modprobe.conf | grep -v '^install linmodem ' > /tmp/modprobeconf.tmp
        sync
        cp -f /tmp/modprobeconf.tmp /etc/modprobe.conf
       fi
       modprobe pctel #$CNTRYPARAM #also loads linmodem,pctel_hw.
       sleep 1
      #fi    
      testmodem /dev/ttyS_PCTEL0
     ;;
esac
done
rm -f /tmp/modemtest.txt
rm -f /tmp/answer.txt

#END#
