#!/bin/sh

# $Id: lessdisks-configure,v 1.14 2004/08/26 05:22:22 vagrant Exp $

# copyright 2004 jeff schwaber, distributed under the terms of the
# GNU General Public License version 2 or any later version.

# modifications made by vagrant@freegeek.org

# TODO give opportunities to edit lists of packages, etc.

: ${DIALOG=dialog}
# set ECHO to empty... so commands actually get run
ECHO=

lil_pause() {
  echo
  echo "hit enter to continue"
  read
}

REPLY=$(dialog --stdout --inputbox "which text editor would you like to use?" 0 0 "$EDITOR")

if [ -z "$(which $REPLY)" ]; then
  echo "invalid editor, exiting..."
  exit 1
else
  EDITOR=$REPLY
fi

tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15

configure_nfs(){
$DIALOG --title "Lessdisks Configuration -- /etc/exports" --clear \
        --msgbox "Now, you're going to edit /etc/exports. \n\
We've taken the example /etc/exports file from \n\
/usr/share/doc/lessdisks/examples/\n\
and added it to the end of your /etc/exports. \n\
Please edit it until it is correct." 14 51

case $? in
  0)
    cat /usr/share/doc/lessdisks/examples/exports >> /etc/exports
    $EDITOR /etc/exports
    #$ECHO /etc/init.d/portmap restart
    #$ECHO /etc/init.d/nfs-common restart
    $ECHO /etc/init.d/nfs-kernel-server restart
    lil_pause
    ;;
  255)
    echo "Goodbye."
    exit;
    ;;
esac
}

get_dhcp() {
$ECHO apt-get -y install dhcp
lil_pause

# copy example into place, giving opportunity to back out now...
cp -i /usr/share/doc/lessdisks/examples/dhcpd.conf /etc/dhcpd.conf
$EDITOR /etc/dhcpd.conf

$ECHO /etc/init.d/dhcp restart
lil_pause
}

dhcp_warning(){
  $DIALOG --title "Lessdisks Configuration -- DHCP Server" --clear \
        --msgbox "Not installing DHCP Server.\n\
be sure to have a properly configured DHCP server\n\
somewhere on your network." 0 0
}

ask_dhcp_install(){
$DIALOG --title "DHCP" --clear \
        --yesno "would you like to install a DHCP server?\n\
if you already have a DHCP server on the network, \n\
answer no and configure the existing DHCP server manually.\n\
see /usr/share/doc/lessdisks/examples/dhcpd.conf" 0 0

if [ "$?" = "0" ] ; then
  get_dhcp
else
  dhcp_warning
fi
}

terminal_ready(){
$DIALOG --title "Lessdisks Configuration -- Terminal Ready" --clear \
        --msgbox "At this point you should be able to boot a terminal\n\
and get a console into the chroot. \n\
I'll wait while you do this. Also, try logging in\n\
The username is root, and there's no password." 10 60

case $? in
  0)
  ;;
  255)
    echo "Goodbye."
    exit;
    ;;
esac
}

install_sdm(){
$DIALOG --title "Lessdisks Configuration -- Installing SDM" --clear \
        --msgbox "We're about to install SDM, and it's going to \n\
bring up some more dialog boxes, and I have no control over\n\
them. So I wanted to let you know that you should say yes to\n\
1)SSH protocol 2 only, 2) set the thing SUID root, 3) run sshd\n\
See you again when that's done." 10 60

case $? in
  0)
    $ECHO apt-get install -y sdm
    $ECHO lessdisks-aptget install -y sdm-terminal
    lil_pause
    fix_ssh_config
    ;;
  255)
    echo "Goodbye."
    exit;
    ;;
esac
}

fix_ssh_config(){
$DIALOG --title "Lessdisks Configuration -- Fixing SSH Config" --clear \
        --msgbox "Welcome back. I'm about to put you into an edit window\n\
and I'd like you to look through the file (/etc/ssh/sshd_config)\n\
and find the line that says 'X11Forwarding no. Change that to\n\
'X11Forwarding yes'" 10 60

case $? in
  0)
    $EDITOR /etc/ssh/sshd_config
    $ECHO /etc/init.d/ssh restart
    lil_pause
    ;;
    
  255)
    echo "Goodbye."
    exit;
    ;;
esac
}


tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15

# source lessdisks configuration file
if [ -r /etc/lessdisks/server.config ]; then
  . /etc/lessdisks/server.config
else
  echo "couldn't find /etc/lessdisks/server.config"
  echo "perhaps run lessdisks-install first?"
  echo "exiting..."
  exit 1  
fi

install_swiftx(){
  lessdisks-aptget install swiftx
  lil_pause
  fix_swiftx_config
}

fix_swiftx_config(){
$DIALOG --title "Lessdisks Configuration -- Fixing swiftx Config" --clear \
        --msgbox "Okay. Time to fix the swiftx config. I'm going to put\n\
you in an editor with the file $lessdisks_path/etc/swiftx/swiftx.conf\n\
Look for the line:\n\
x_config_dir: <some setting>\n\
and set it to /var/state/lessdisks/etc/\n\
(don't forget the trailing slash...)" 12 60

case $? in
  0)
    $EDITOR $lessdisks_path/etc/swiftx/swiftx.conf
    lil_pause
    ;;
    
  255)
    echo "Goodbye."
    exit;
    ;;
esac
}

install_xfs(){
  apt-get install xfs xfonts-base xfonts-100dpi xfonts-75dpi xfonts-scalable
  lil_pause
  fix_xfs_config
}

fix_xfs_config(){
$DIALOG --title "Lessdisks Configuration -- Fixing xfs Config" --clear \
        --msgbox "Okay. Time to fix the xfs config. I'm going to put\n\
you in an editor with the file /etc/X11/fs/config\n\
Look for the line:
no-listen: tcp
and comment it out (with a # at the beginning of the line" 10 60

case $? in
  0)
    $EDITOR /etc/X11/fs/config
    $ECHO /etc/init.d/xfs restart
    lil_pause
    ;;
    
  255)
    echo "Goodbye."
    exit;
    ;;
esac
}

final_notes(){
$DIALOG --title "Lessdisks Configuration -- Final Notes" --clear \
        --msgbox "This is as far as we go together. There are still\n\
some things you need to do, and I'm going to give you\n\
some last notes.\n\
  - You need to install some window managers. Try icewm, \n\
    gnome, kde, ion, xfce, xfwm, and others. apt-get them\n\
    EXAMPLE: apt-get install icewm\n\
  - Any terminals with video cards that can't use xserver-xfree86\n\
    will need their specific xservers. See swiftx's output for\n\
    what server you'll need. You'll need to apt-get them in the chroot,\n\
    so use the special script lessdisks-aptget for that.\n\
    EXAMPLE: lessdisks-aptget install xserver-mach64\n\
Good luck." 18 78
}

install_dialogs(){
  apt-get install xdialog dialog
  lessdisks-aptget install xdialog dialog
  lil_pause
}

install_xterm(){
  apt-get install xterm
  lil_pause
}

default_runlevel(){
  if [ -z "$alt_x_display_command" ] || [ -z "$x_display_command" ]; then
    if [ -e /etc/lessdisks/x.config ]; then
      . /etc/lessdisks/x.config
    fi
  fi
  if [ -n "$(echo $x_display_opts | egrep query)" ]; then
    level_five="XDMCP (xdm/kdm/gdm)"
  else
    level_five="$x_display_command $x_display_opts"
  fi
  if [ -n "$(echo $alt_x_display_command | egrep sdm)" ]; then
    level_four=sdm
  else
    level_four="$alt_x_display_command $x_display_opts"
  fi
  runlevel=$($DIALOG --stdout --title "$title" --clear \
    --radiolist "Select default runlevel:" \
    0 0 0 \
    2 console off \
    3 console off \
    4 "$level_four" off \
    5 "$level_five" on | \
    sed s/\"//g)
  
  case $runlevel in
    2|3|4|5) mkdir -p /etc/lessdisks/terminals
    echo runlevel=$runlevel >> /etc/lessdisks/terminals/default ;;
  esac
}

yes_no(){
  title=$1
  shift
  $DIALOG --stdout --title "$title" --clear \
    --yesno "$@" 0 0
}

yes_no configure_nfs "configure /etc/exports in NFS server?" && configure_nfs
yes_no install_xfs "install XFS (X font server) and X fonts?" && install_xfs
ask_dhcp_install "configure DHCP server on this machine?"
yes_no install_sdm "configure terminal and server to use SDM?" && install_sdm
yes_no install_dialogs "install X dialog and dialog?" && install_dialogs
yes_no default_runlevel "set default runlevel for terminals?" && default_runlevel
yes_no install_xterm "install X terminal emulator xterm?" && install_xterm
yes_no install_swiftx "install swiftx X configuration tool?" && install_swiftx

final_notes
