#!/bin/sh
#
#  emacspeakconfig - configuration script for emacspeak
#
#  This script sets values in /etc/emacspeak.conf, which override the
#  defaults compiled into emacspeak TCL scripts and .elc files.
#  Values in the user's environment will override those in
#  /etc/emacspeak.conf

set -e

if [ -x /usr/bin/awk -o -x /bin/awk ]; then true; else
cat <<EOF

awk is missing.
Please execute /usr/sbin/emacspeakconfig again after installing it.

EOF
    echo -n "Press Enter to continue: "; read junk;
    exit 1;
fi

CFG=/etc/emacspeak.conf
GROUPFILE=/etc/group
DRIVERDIR=/usr/lib/emacs/common/emacspeak/drivers

set -- `getopt i $*`
if test $? != 0
then
    echo 'Usage: emacspeakconfig [-i]'
    exit 2
fi
initial=no
for i
do
    case "$i"
    in
       	-i)
       		initial=yes; shift;;
       	--)
       		shift; break;;
    esac
done

#echo initial=$initial

# if there is no configuration file, then create one
if [ -f $CFG ]; then
  true
else
cat >$CFG  <<\EOF
# emacspeak configuration file
#
#
if [ "$DTK_PROGRAM" = "" ]; then
  DTK_PROGRAM=
fi
if [ "$DTK_PORT" = "" ]; then
  DTK_PORT=
fi
if [ "$DTK_TCL" = "" ]; then
  DTK_TCL=
fi
export DTK_PROGRAM DTK_PORT DTK_TCL
EOF
fi


show_devices() {
cat <<EOF

Please enter the number of the text to speech device you have:

    1 DECtalk Express
    2 DECtalk Multivoice
    3 DECtalk 3
    4 DoubleTalk PC (internal)
    5 DoubleTalk LT (serial port)
    6 LiteTalk (serial port)
    O other

EOF
}


show_ports() {
cat <<EOF

Please enter the number of the serial port for your speech device:

    0 /dev/ttyS0 or COM1
    1 /dev/ttyS1 or COM2
    2 /dev/ttyS2 or COM3
    3 /dev/ttyS3 or COM4
    4 None of the above

EOF
}

yesno() {
while true; do
    echo -n "$1 [$2]: "; read ans;
    if [ "$ans" = "" ]; then ans=$2; fi
    if [ "$ans" = "y" -o "$ans" = "Y" -o "$ans" = "yes" ]; then return 0; fi
    if [ "$ans" = "n" -o "$ans" = "N" -o "$ans" = "no" ]; then return 1; fi
done
}

request_params() {
    echo -n "Please enter the full path name of the driver: "; read PROGRAM;
    if yesno "Is the driver a TCL script? " "y" ; then 
	TCL=tcl; 
    else 
	TCL=$PROGRAM; PROGRAM=; 
    fi;
    if yesno "Is the speech synthesizer connected to a serial port?" "y" ; then
	true;
    else
	valid=no;
	while [ "$valid" = "no" ]; do
	    echo -n "Please enter the device (for example, /dev/lp0): "; 
	    read PORT; 
	    if check_port; then valid=yes; fi
	done
    fi;

echo "PROGRAM=$PROGRAM, TCL=$TCL, PORT=$PORT"
}

check_pgm() {
if [ -x $TCL ]; then true; else
    echo
    echo "NOTICE: Before using Emacspeak, please install"
    echo "        $TCL"
    echo
    echo -n "Press Enter to continue: "; read junk;
fi
}

check_port() {
if [ -c $PORT ]; then return 0; fi
echo "$PORT does not exist"
yesno "Will $PORT be compiled into the kernel, or loaded as a module?" "y"
}

enroll() {
#	enroll - give a user a supplementary group ID
#
#       usage:  enroll user1[,user2...]  group1[,group2...]
set -e
awk '
BEGIN {
  FS=":";
  OFS=":";
  if (ARGC<4) {
    print "requires two arguments" >"/dev/stderr";
    exit(1);
  }
  userlist=ARGV[1];
  if (userlist!~/^[a-z][a-z,]*$/) {
    print "invalid user list" >"/dev/stderr"; 
    exit(1);
  }
  nu=split(userlist,user,",");
  grouplist=ARGV[2];
  if (grouplist!~/^[a-z][a-z,]*$/) {
    print "invalid group list" >"/dev/stderr"; 
    exit(1);
  }
  ng=split(grouplist,group,",");
#  printf("%d users: ",nu); 
#  for (i=1; i<=nu; i++) printf("%s ", user[i]); 
#  printf("\n");
#  printf("%d groups: ",ng); 
#  for (i=1; i<=nu; i++) printf("%s ", group[i]); 
#  printf("\n");
  ARGC=2;
  ARGV[1]=ARGV[3];
}
{
  for (i=1; i<=ng; i++) {
    if ($1~group[i]) {
      if (NF==3) {		# no group members yet
	$4=userlist;
      } else {			# already some group members
	delete names;
	delete member;
	split($4,names,",");
	for (j in names) {member[names[j]]=1;}
	for (j=1;j <= nu;j++) {member[user[j]]=1;}
	$4="";
	for (name in member) {$4=$4 "," name;}
	$4=substr($4, 2);
      }
    }
  }
  print;
}' $1 $2 $GROUPFILE >$GROUPFILE.$$ && mv $GROUPFILE.$$ $GROUPFILE
}

dismiss() {
#	dismiss - take a supplementary group ID away from a user
#
#       usage: dismiss  user1[,user2...]  group1[,group2...]
set -e
awk '
BEGIN {
  FS=":";
  OFS=":";
  if (ARGC<4) {
    print "requires two arguments" >"/dev/stderr";
    exit(1);
  }
  userlist=ARGV[1];
  if (userlist!~/^[a-z][a-z,]*$/) {
    print "invalid user list" >"/dev/stderr"; 
    exit(1);
  }
  nu=split(userlist,user,",");
  grouplist=ARGV[2];
  if (grouplist!~/^[a-z][a-z,]*$/) {
    print "invalid group list" >"/dev/stderr"; 
    exit(1);
  }
  ng=split(grouplist,group,",");
#printf("userlist=%s\n",userlist);
#printf("%d users: ",nu); for (i=1; i<=nu; i++) printf("%s ", user[i]); printf("\n");
#printf("%d groups: ",ng); for (i=1; i<=nu; i++) printf("%s ", group[i]); printf("\n");
  ARGC=2;
  ARGV[1]=ARGV[3];
}
{
  for (i=1; i<=ng; i++) {
    if ($1~group[i]) {
      if (NF==3) {		# no group members yet
      } else {			# already some group members
	delete names;
	delete member;
	split($4,names,",");
	for (j in names) {member[names[j]]=1;}
	for (j=1;j <= nu;j++) {delete member[user[j]];}
	$4="";
	for (name in member) {$4=$4 "," name;}
	$4=substr($4, 2);
      }
    }
  }
  print;
}' $1 $2 $GROUPFILE >$GROUPFILE.$$ && mv $GROUPFILE.$$ $GROUPFILE
}

# get the current parameter values (if any) from the configuration file
if [ -s $CFG ]; then
   PORT=`awk 'BEGIN{FS="="}/^ *DTK_PORT/{print $2}' $CFG`
   TCL=`awk 'BEGIN{FS="="}/^ *DTK_TCL/{print $2}' $CFG`
   PROGRAM=`awk 'BEGIN{FS="="}/^ *DTK_PROGRAM/{print $2}' $CFG`

   if [ "$PROGRAM" = "dtk-exp" ]; then DEVICE="DECtalk Express"; fi
   if [ "$PROGRAM" = "dtk-mv" ]; then 
      DEVICE="DECtalk Multivoice or DECtalk 3"; 
   fi
fi

#echo initial values are: PROGRAM=$PROGRAM TCL=$TCL PORT=$PORT

if [ $initial = no -o "$PROGRAM" = "" -o "$TCL" = "" ]; then
    valid=no
    while [ $valid = no ]; do
	show_devices
	echo -n "Number: " ; read area
	case $area in
	    1) DEVICE="DECtalk Express" ; PROGRAM=dtk-exp ; 
		TCL=tcl ; valid=yes ;;
	    2) DEVICE="DECtalk Multivoice" ; PROGRAM=dtk-mv ; 
		TCL=tcl ; valid=yes ;;
	    3) DEVICE="DECtalk 3" ; PROGRAM=dtk-mv ; 
		TCL=tcl ; valid=yes ;;
	    4) echo "sorry, not yet supported" ;;
#	    4) DEVICE="Double Talk PC" ; PROGRAM=none ; 
#		TCL=$DRIVERDIR/emacspeak-dt ; check_pgm ; valid=yes ;;
	    5) DEVICE="Double Talk LT" ; PROGRAM=none ; 
		TCL=$DRIVERDIR/emacspeak-dt ; check_pgm ; valid=yes ;;
	    6) DEVICE="LiteTalk" ; PROGRAM=none ; 
		TCL=$DRIVERDIR/emacspeak-dt ; check_pgm ; valid=yes ;;
	    o|O|0) request_params ; check_pgm ; valid=yes ;;
	esac
    done
fi

#echo new values are: PROGRAM=$PROGRAM TCL=$TCL PORT=$PORT

if [ $initial = no -o "$PORT" = "" ]; then
    valid=no
    while [ $valid = no ]; do
	show_ports
	echo -n "Number: " ; read area
	case $area in
	    0) PORT=/dev/ttyS0 ; valid=yes ;;
	    1) PORT=/dev/ttyS1 ; valid=yes ;;
	    2) PORT=/dev/ttyS2 ; valid=yes ;;
	    3) PORT=/dev/ttyS3 ; valid=yes ;;
	    4) echo -n "What device? " ; read PORT
	    if check_port $PORT; then valid=yes; fi
	esac
    done
fi

#echo " "
#echo emacspeak is configured for $DEVICE attached to $PORT

DEVICEGROUP=`ls -l $PORT|awk '{print $4}'`

if [ $DEVICEGROUP = root ];
then
    echo "$PORT is in group: $DEVICEGROUP"
    echo "please move it to another group with chown,"
    echo "then repeat emacspeak configuration"
    exit 1
fi

# skip the enrolling of additional users if there is already at least
# one user, and it's the initial installation
MEMBERS=`grep ^${DEVICEGROUP} $GROUPFILE|awk 'BEGIN{FS=":"}{print $4}'`
if [ "$MEMBERS" = "" -o $initial = no ];
then
    echo " "
    echo $PORT can be accessed by members of the group: $DEVICEGROUP.
    if [ "$MEMBERS" = "" ]; 
    then echo "currently, no users are in group $DEVICEGROUP";
    else echo "currently, these users are in group $DEVICEGROUP: $MEMBERS";
    fi
    finished=no
    while [ $finished = no ]; do
	echo " "
	echo "If you wish to enroll any other users into group $DEVICEGROUP,"
	echo "(even if their accounts have not been created yet),"
	echo -n "please list their usernames here separated by commas: "
	read USERS
	if [ "$USERS" = "" ] || enroll $USERS $DEVICEGROUP; then 
	    finished=yes; 
	fi
    done
    finished=no
    while [ $finished = no ]; do
	echo " "
	echo "If you wish to remove any users from group $DEVICEGROUP,"
	echo -n "please list their usernames here separated by commas: "
	read USERS
	if [ "$USERS" = "" ] || dismiss $USERS $DEVICEGROUP; then 
	    finished=yes; 
	fi
    done
fi

# make sure the group has both read and write permissions
chmod g+rw $PORT
    
cp $CFG $CFG.TMP
sed -e "s@^ *DTK_PROGRAM=.*@  DTK_PROGRAM=$PROGRAM@" \
    -e "s@^ *DTK_PORT=.*@  DTK_PORT=$PORT@" \
    -e "s@^ *DTK_TCL=.*@  DTK_TCL=$TCL@" \
    < $CFG.TMP > $CFG

if [ -s $CFG ]
then
    rm -f $CFG.TMP
fi
