#!/bin/sh -e

# determine architecture
ARCH=`dpkg --print-installation-architecture`

#DEBCONF_DEBUG=1
#export DEBCONF_DEBUG

if [ "$1" = "configure" ]; then

update-rc.d setserial start 30 S . stop 30 0 6 . >/dev/null

if [ -f /usr/share/debconf/confmodule ]; then # debconf exists

# source debconf library
. /usr/share/debconf/confmodule

if [ -r /etc/rc.boot/0setserial ] ; then
  if [ "$ARCH" != "m68k" ]; then
    # inform about the renaming of rc.boot file
    db_get setserial/rc-boot-file-renamed
    mv /etc/rc.boot/0setserial /etc/rc.boot/0setserial.pre-2.15
    # ask about using autosave in serial.conf
    #db_get setserial/autosave
    #if [ "$RET" = "true" ]; then
    #  cp /etc/serial.conf /etc/serial.conf.orig
    #  /etc/init.d/setserial stop >&2
    #  echo "setserial thought the following about your system..." >&2
    #  if ! grep "^[^#]" /etc/serial.conf >&2 ; then
    #    echo "   no information..." >&2
    #  fi
    #fi
  else
    # inform about the removal of rc.boot file
    db_get setserial/rc-boot-file-removed
    rm -f /etc/rc.boot/0setserial
  fi
fi

if [ "$ARCH" != "m68k" ]; then
  if [ ! -e /etc/serial.conf ]; then
    touch /etc/serial.conf
  fi
  chead="`head -1 /etc/serial.conf`X"
  db_get setserial/autosave-types
  case "$RET" in 
    ( "AUTOSAVE ONCE" | "autosave once" ) 
       if [  "$chead" != "###AUTOSAVE-ONCE###X"  -a  "$chead" != "###PORT STATE GENERATED USING AUTOSAVE-ONCE###X"  ]; then
          mv /etc/serial.conf /etc/serial.conf.tmp
          echo "###AUTOSAVE-ONCE###" > /etc/serial.conf
          cat /etc/serial.conf.tmp >> /etc/serial.conf
          rm /etc/serial.conf.tmp
          /etc/init.d/setserial stop >&2
       fi
       ;;
    ( "MANUAL" | "manual" ) 
       if [ "$chead" != "#X" ]; then
          mv /etc/serial.conf /etc/serial.conf.tmp
          echo "#" > /etc/serial.conf
          cat /etc/serial.conf.tmp >> /etc/serial.conf
          rm /etc/serial.conf.tmp
       fi
       ;;
    ( "AUTOSAVE ALWAYS" | "autosave always" ) 
       if [ "$chead" != "###AUTOSAVE###X" ]; then
          mv /etc/serial.conf /etc/serial.conf.tmp
          echo "###AUTOSAVE###" > /etc/serial.conf
          cat /etc/serial.conf.tmp >> /etc/serial.conf
          rm /etc/serial.conf.tmp
          /etc/init.d/setserial stop >&2
       fi
       ;;
  esac
  #if [ "$RET" = "true" ]; then
  #  cp /etc/serial.conf /etc/serial.conf.orig
  #  /etc/init.d/setserial stop >&2
  #  echo "setserial thought the following about your system..." >&2
  #  if ! grep "^[^#]" /etc/serial.conf >&2 ; then
  #    echo "   no information..." >&2
  #  fi
  #fi
  if [ -x /sbin/update-modules ]; then
    if ! /sbin/update-modules ; then
      db_input high setserial/update-modules-failed || true
      db_get setserial/update-modules-failed
      db_go
    fi
  fi
fi

else # debconf doesn't exist, revert to old type of questions

# See if the user has an old-style 0setserial entry
if [ -r /etc/rc.boot/0setserial ] ; then
  if [ "$ARCH" != "m68k" ]; then
	echo "You have an old-style 0setserial entry. The configuration"
	echo "mechanism has changed completely from 2.14."
	echo "Renaming your old /etc/rc.boot/0setserial to 0setserial.pre-2.15"
	mv /etc/rc.boot/0setserial /etc/rc.boot/0setserial.pre-2.15
	echo ""
	echo "This release uses serial.conf in /etc/ to configure the ports"
	echo "When you shutdown or halt your machine, your current settings should"
	echo "be automatically stored, providing you do not have either"
	echo "a multiport or a hayes device (edit /etc/serial.conf by hand)."
	echo "Attention PCMCIA users - pcmcia has its own configuration for"
	echo "pcmcia serial-type devices, which is not compatible with setserial."
	echo "I have attempted to compensate for this, but if you have problems"
	echo "read /usr/share/doc/setserial/README.Debian.gz"
	echo "Note that if you edit serial.conf, remember to remove completely"
	echo "the AUTOSAVE line from the file."
	echo ""
	echo -n "Do you want the automatic serial port configuration (recommended) (YES/no):"
	read ANSWER
	if [ "$ANSWER" = "no" -o "$ANSWER" = "NO" -o "$answer" = "n" -o "$ANSWER" = "N" ]
	then
		echo "Okay, not touching your configuration. Edit your /etc/serial.conf yourself."
	else
		echo "Configuring setserial automatically"
		cp /etc/serial.conf /etc/serial.conf.orig
		/etc/init.d/setserial stop
		echo "setserial thought the following about your system..."
		# grep -v "^#" /etc/serial.conf
                if ! grep "^[^#]" /etc/serial.conf >&2 ; then
                  echo "   no information..."
                fi
	fi
  else
	echo "You have an old-style 0setserial entry. The new standard makes"
	echo "use of /etc/init.d rather than /etc/rc.boot, and your old"
        echo "/etc/rc.boot/0setserial file from version 2.14 will be removed."
	rm -f /etc/rc.boot/0setserial
  fi
fi

if [ "$ARCH" != "m68k" -a -x /sbin/update-modules ]; then
  if ! /sbin/update-modules ; then
    cat <<END >&2
 WARNING: setserial tried to install the module management code to support
 the serial.o module being loaded and unloaded dynamically by kerneld (or
 its equivalent). However, update-modules failed to allow its installation.

 There may be something non-standard about your module configuration.
 You should try running /sbin/update-modules on your own.

 Please press <enter> to continue...
END
    read KEYPRESS
  fi
fi

fi # debconf or not

fi # $1 = configure

# Automatically added by dh_installdocs
if [ "$1" = "configure" ]; then
	if [ -d /usr/doc -a ! -e /usr/doc/setserial -a -d /usr/share/doc/setserial ]; then
		ln -sf ../share/doc/setserial /usr/doc/setserial
	fi
fi
# End automatically added section

