#!/bin/sh
#
if [ ! $DISPLAY = "" ]; then
DCMD="Xdialog --wrap" 
CMD="rxvt -e "
else
DCMD="dialog"
CMD=""
fi


modem() {
cat <<EOF
#!/bin/sh
#
# /etc/rc.d/rc.serial   `date '+%a %b %e %Y' 2>/dev/null`
#
# This rc.serial is started from: /etc/rc.d/rc.S
#
# This rc.serial file is created when you run: comset
#
# /dev/ttyS0 = com1 ttyS1 = com2 ttyS2 = com3 ttyS3 = com4
#
# Try to autoconfigure modem device using setserial.

# If setserial is not there abort the configuration.
[ -x /sbin/setserial ] || exit 0

echo "Configuring modem serial port: /dev/$MODEM"

/sbin/setserial -v /dev/$MODEM auto_irq skip_test autoconfig session_lockout

# There have been multiple reports of late that the auto configuration
# of the serial ports is broken with some new motherboards, especially
# multi-processor ones.  For this reason, you have the option of telling
# setserial what irq, port, your modem is on.
# Note that we still need an automatic test to find out what uart we are 
# using.

# Tell setserial what irq, port, the modem in on if it can't detect your 
# modem automatically.
# Put # above for /sbin/setserial and remove # below for your modem device.

#/sbin/setserial -v /dev/ttyS0 irq 4 port 0x3F8 skip_test autoconfig session_lockout
#/sbin/setserial -v /dev/ttyS1 irq 3 port 0x2F8 skip_test autoconfig session_lockout
#/sbin/setserial -v /dev/ttyS2 irq 4 port 0x3E8 skip_test autoconfig session_lockout
#/sbin/setserial -v /dev/ttyS3 irq 3 port 0x2E8 skip_test autoconfig session_lockout

# End...
EOF
}

if [ ! "$UID" = "0" ]; then
echo
echo "[$LOGNAME] You need to run this script as root."
echo "Try one of these to run it as root: # sudo comset"
echo "                                    # su -c comset"
echo
exit 1
fi

if ! type -all dialog >/dev/null 2>&1 ; then
echo "Can't find 'dialog', i can't run without 'dialog' on your system."
exit 
fi

SCD() {
$DCMD --backtitle "Vector Linux ...at the speed of light." --title "SELECT CALLOUT DEVICE" \
--menu "Please select the callout device which yo
to use for your modem:" 20 60 10 \
"ttyS0" "COM1: under DOS" \
"ttyS1" "COM2: under DOS" \
"ttyS2" "COM3: under DOS" \
"ttyS3" "COM4: under DOS" \
"ttyS4" "COM5: under DOS" \
"ttyS5" "COM6: under DOS" \
"ttyS6" "COM7: under DOS" \
"ttyS7" "COM8: under DOS" \
"ttyS15" "PCTEL Modem"    \
"ttySHCF0" "HSF/HCF Modem"  \
"cuaHCF0"  "HSF/HCF Modem" 2> /tmp/callout

 if [ $? = 1 ]; then
  rm -f /tmp/callout
 else
  MODEM="`cat /tmp/callout`"
  rm -f /tmp/callout
  (cd /dev && ln -sf $MODEM modem)

modem > /etc/rc.d/rc.serial
chmod 755 /etc/rc.d/rc.serial
 fi
}

$DCMD --backtitle "Vector Linux ...at the speed of light." --title "MODEM CONFIGURATION" --yesno \
"Comset will create a symbolic link /dev/modem that will point   
to the actual modem device (ttyS0, ttyS1, ttyS2, ttyS3).\n  
You can change this link later if you put your modem on a 
different port.\n\n\
The setserial modem configure file /etc/rc.d/rc.serial\n\
will also be created for your modem device.\n\n\
Would you like to set up your modem?" 14 68
if [ $? = 0 ]; then

if cat /proc/tty/driver/serial 2>/dev/null | grep -q "DSR" ; then
MODEM="ttyS`cat /proc/tty/driver/serial | grep "DSR" | cut -b1`"
COM=`echo $MODEM | cut -b5`
$DCMD --backtitle "Vector Linux ...at the speed of light." --title "Possible modem detected!" --yesno "I think you \
have a modem on device /dev/$MODEM = COM`expr $COM + 1` under DOS, 
this could be wrong! \n 
Chose <Yes> to accept or <No> to change." 8 68

if [ $? = 1 ]; then
SCD
else
(cd /dev && ln -sf $MODEM modem)
modem > /etc/rc.d/rc.serial
chmod 755 /etc/rc.d/rc.serial
fi
else
SCD
fi
fi




