#!/bin/sh

set -e

CFG=/etc/emacspeak.conf
GROUPFILE=/etc/group

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 None of the above

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
}

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"; 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) echo "sorry, other devices are not yet supported" ; 
		exit 1 ;;
	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 [ -c $PORT ]; then valid=yes; 
	    else echo "$PORT is not a character device"; 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
