#!/bin/sh -e
#
# Script to (re-)configure the lsh-server package.
#
# Copyright (C) 2000, 2001 Timshel Knoll <timshel@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# On Debian systems, see /usr/share/common-licenses/GPL for the GNU GPL.

LSHD_DEFAULTS=/etc/default/lsh-server
LSHD_PORT=
SSH1_FALLBACK=

# This needs to be fixed. If we do stuff this way, strange things will
# happen ... the user can specify stuff to debconf and old options can
# still be written to the config file :-(
## First, get default options
#[ -e "$LSHD_DEFAULTS" ] && . "$LSHD_DEFAULTS"

# Fall back to default options if necessary
LSHD_PORT=${LSHD_PORT-2222}
SSH1_FALLBACK=${SSH1_FALLBACK-true}

# Make sure SSH1_FALLBACK is either "true" or "false", set up option value
# for non-debconf stuff in case that is required
case "$SSH1_FALLBACK" in
   true|TRUE|y*|Y*)
      SSH1_FALLBACK=true
      SSH1OPT=Y
   ;;
   *)
      SSH1_FALLBACK=false
      SSH1OPT=N
   ;;
esac

if [ -f /usr/share/debconf/confmodule ]; then
   . /usr/share/debconf/confmodule
   
   if [ "$1" != "--no-reconfigure" -a -x "/usr/bin/dpkg-reconfigure" ]; then
      # FIXME - if I do this, it creates inconsistency when debconf /
      # dpkg-preconfigure auto - configures the package and when this
      # script is run :-( Need to do stuff to the config file too to
      # get rid of these inconsistencies...
      ## Substitute values read from config file
      #db_set "lsh-server/lshd_port" "$LSHD_PORT"
      #db_set "lsh-server/ssh1_fallback" "$SSH1_FALLBACK"
      dpkg-reconfigure lsh-server
   fi
   

   db_get "lsh-server/lshd_port"; LSHD_PORT="$RET"
   db_get "lsh-server/ssh1_fallback"; SSH1_FALLBACK="$RET"
   
else
   # Damn. We don't have debconf.
   printf "What port do you want lshd to run on [$LSHD_PORT]? "
   read PORT
   
   case "$PORT" in
      "")
         # leave LSHD_PORT as it is ...
         ;;
      *)
         LSHD_PORT=$PORT
         ;;
   esac
   
   if test "$LSHD_PORT" = 22; then
      printf "Do you want to use the ssh1-fallback option of lshd [$SSH1OPT]? "
      read SSH1
      
      case "$SSH1" in
         "")
	    # leave SSH1_FALLBACK as it is ...
         ;;
         y*|Y*|true|TRUE)
            SSH1_FALLBACK=true
         ;;
         *)
            SSH1_FALLBACK=false
         ;;
      esac
   fi
fi

# OK, now make the config file

cat <<EOF >>"$LSHD_DEFAULTS"
# Configuration file generated by lsh-server-config.
# You can change the lsh-server configuration either by editing
# this file, or by running /usr/sbin/lsh-server-config, which uses
# a debconf interface to set up lsh-server.
#
EOF

echo "LSHD_PORT=\"$LSHD_PORT\"" >>"$LSHD_DEFAULTS"
echo "SSH1_FALLBACK=\"$SSH1_FALLBACK\"" >>"$LSHD_DEFAULTS"

# Generate a hostkey, if none already exist
# FIXME: should this convert an existing OpenSSH/SSH1 hostkey if one exists?
if [ ! -f /etc/lsh_host_key ]; then
   printf "Generating a new host key: /etc/lsh_host_key"
   lsh-keygen -l 8 | lsh-writekey -o /etc/lsh_host_key
   echo " done."
fi

if [ "$LSHD_PORT" -eq 22 -a "$SSH1_FALLBACK" = true ]; then
   if [ ! -d /etc/ssh ]; then
      mkdir -p /etc/ssh
   fi

   if [ ! -f /etc/ssh/NOSERVER ]; then
      # stop ssh from starting at bootup
      cat  <<"EOF" >> /etc/ssh/NOSERVER
LSH_SERVER_CONFIG_GENERATED
# Generated by lsh-server-config
# Please don't remove this file unless you have first disabled lsh, and don't
# change the first line ... otherwise lsh-server won't recognise it!!!
EOF
   else
      echo "/etc/ssh/NOSERVER already exists - not overwriting with lsh version" >&2
   fi
fi

# Emacs stuff:
# Local-Variables:
# sh-indent: 3
# End:
