#!/bin/bash
if [ $UID != 0 ]; then
 echo Error: become root before starting $0 >&2
 exit 100
fi
XDIALOG_HIGH_DIALOG_COMPAT=1
export XDIALOG_HIGH_DIALOG_COMPAT
LANG=C
export LANG
DIALOG=dialog
[ -n "$DISPLAY" ] && [ -x /usr/bin/Xdialog ] && DIALOG="Xdialog"
function inputbox {
 # inputbox title text width [init]
 $DIALOG --stdout --title "$1" --inputbox "\n$2\n\n" 10 $3 "$4"
}
function simplemenu {
 # menu text width [ tag item ] ...
 # only single word tags and items
 title="$1"
 text="$2"
 width="$3"
 shift 3
 $DIALOG --stdout --title "$title" --menu "$text" $((8+($#-1)/2)) $width $((($#-1)/2+1)) $*
}
function msgbox {
 # msgbox title text width
 $DIALOG --title "$1" --msgbox "\n$2\n" $((${#2}/($3-4)+7)) $3
}
function yesno {
 # yesno title text width
 $DIALOG --title "$1" --yesno "\n$2\n" $((${#2}/($3-4)+7)) $3
 echo $?
}
TITLE="FRITZ!Card DSL Configuration for ISDN"
PROVIDER="$(inputbox "$TITLE" "ISDN provider" 55 fcdsl-isdn)"
[ $? != 0 ] && exit 1
if [ "$PROVIDER" == "" ]; then
 msgbox "$TITLE" "Error: bad provider name" 55
 exit 10
fi
NUMBER="$(inputbox "$TITLE" "ISDN number" 55)"
[ $? != 0 ] && exit 2
if [ "$NUMBER" == "" ]; then
 msgbox "$TITLE" "Error: bad number" 55
 exit 20
fi
USERNAME="$(inputbox "$TITLE" "ISDN username" 55)"
[ $? != 0 ] && exit 3
if [ "$USERNAME" == "" ]; then
 msgbox "$TITLE" "Error: bad username" 55
 exit 30
fi
PASSWORD="$(inputbox "$TITLE" "ISDN password" 55)"
[ $? != 0 ] && exit 4
if [ "$PASSWORD" == "" ]; then
 msgbox "$TITLE" "Error: bad password" 55
 exit 40
fi
MSN="$(inputbox "$TITLE" "ISDN outgoing MSN" 55)"
[ $? != 0 ] && exit 5
if [ "$MSN" == "" ]; then
 msgbox "$TITLE" "Error: bad MSN" 55
 exit 50
fi 
mkdir -p /etc/ppp/peers/isdn/
rm -rf "/etc/ppp/peers/isdn/$PROVIDER"
cat <<EOF >"/etc/ppp/peers/isdn/$PROVIDER"
sync
noauth 
user "$USERNAME"
plugin userpass.so
password "$PASSWORD"
defaultroute
plugin capiplugin.so
msn "$MSN"
number "$NUMBER"
protocol hdlc
/dev/null
usepeerdns
EOF
chmod 600 "/etc/ppp/peers/isdn/$PROVIDER"
chown root.root "/etc/ppp/peers/isdn/$PROVIDER"
if [ ! -e /etc/debian_version ]; then
 [ -f /etc/resolv.conf ] && cp /etc/resolv.conf /etc/resolv.conf.1st
 rm -f /etc/resolv.conf
 ln -s ppp/resolv.conf /etc/resolv.conf
fi
PPPD=/usr/sbin/pppd
killall pppd 2>/dev/null
$PPPD call "isdn/$PROVIDER"
