#
# uinstall  2.0a   12/19/90
#           2.0d   01/07/91
#           2.07   08/30/91  name change to uinstall (was install)
#
#  Xenix shell script to install driver
#  (c) 1990-91 Hansco Information Technologies, Inc.
#
tmpdir=/tmp
[ "$1" = "-n" ] && {
   noinput=Y
   shift
}
productname=$1
drivername=$2
version=$3
masterfile=$4
addnfile=$5
[ -z "$6" ] || addnexpr=$6 
#
getyn() {
        while   read resp
        do      case $resp in
                [yY]*)  return 0                        ;;
                [nN]*)  return 1                        ;;
                *)      echo "Please enter Y or N: \c"  ;;
                esac
        done
}
#
# getcurver     - get the currently installed driver version
#
getcurver() {
        curver=`sed -n "\\;# $productname V; {s;# $productname V;;;p;}" $dvrfile`
}
#
# remove <file> - remove HIT/Ansi additions from <file>
#
remove() {
        remfile=$1
        cp $remfile $remfile.old
        sed -n "\\;# $productname V;,\\;# END $productname; !p" $remfile.old >$remfile
}
#
# install <file> <additions> [<expr>] - add <additions> to <file> before <expr>
#
install() {
        insfile=$1
        insaddn=$2
        insexpr=$3
        cp $insfile $insfile.old
        sed "\\;\$productname; s;\$productname;$productname;g" $tmpdir/$insaddn | \
        sed "\\;\$version; s;\$version;$version;g" >$tmpdir/addn
        [ -z "$insexpr" ] && {
           cat $tmpdir/addn >>$insfile
           return
        }
        sed "\\;$insexpr; r $tmpdir/addn" $insfile.old >$insfile
}       
#
# driver <file> <additions> <name> [<expr>] - install/update specified driver
#       assumes $tmpdir = /tmp directory
#               $version = install driver version
#
driver() {
        dvrfile=$1
        dvraddn=$2
        dvrname=$3
        dvrexpr=$4
        [ -w "$dvrfile" ] || return 4
        getcurver
        installed=UPD
        [ -z "$curver" ] && installed=NO
        [ "$curver" = "$version" ] && installed=YES
        case $installed in
          YES)  echo "$dvrname is already installed."
                return 3
                ;;
          UPD)  [ "$noinput" ] && {
                        echo "\n$dvrname version $curver is currently installed." 
                        echo "Please remove the driver and try again." 
                        return 2
                }
                echo "\n$dvrname version $curver is currently installed."
                echo "Would you like to update to version $version? (y/n) \c"
                getyn && {
                        remove $dvrfile 
                        install $dvrfile $dvraddn $dvrexpr 
                        echo "$dvrname updated." 
                        return 1
                }
                ;;
          NO)   install $dvrfile $dvraddn $dvrexpr
                echo "$dvrname installed."
                return 0
                ;;
        esac
}
#
driver $masterfile $addnfile "$productname $drivername" $addnexpr
exit $?
