#!/bin/sh

# $Id: lessdisks-configure,v 1.8 2004/03/20 00:21:27 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

: ${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

$DIALOG --title "Lessdisks Configuration -- add users to lessdisks group" \
	--clear \
        --inputbox "Enter some users to add to the lessdisks group. \n
You need at least one, so when you get an x-based\n
terminal working, you'll have someone to log in as.\n
\n
If you don't have any users here yet, other than root, \n
hit Ctrl-Alt-F2 to go to the second virtual terminal \n
and create a user. You'll need it later.\n
\n
You can enter multiple users by putting spaces between \n
their names:" 18 60 2> $tempfile

retval=$?

case $retval in
  0)
    users=`cat $tempfile`
    for user in $users; do
    	$ECHO adduser $user lessdisks
    done
    lil_pause
    ;;
  1)
    echo "Goodbye."
    exit;
    ;;
  255)
    if test -s $tempfile ; then
      echo "Goodbye."
      exit;
      cat $tempfile
    else
      echo "Goodbye."
      exit;
    fi
    ;;
esac

$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
}

$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
  $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
fi

$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

$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
    ;;
  255)
    echo "Goodbye."
    exit;
    ;;
esac

$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
. /etc/lessdisks/server.config

chroot_packages="sdm-terminal xdialog dialog swiftx"
server_packages="xfs xfonts-base xfonts-100dpi xfonts-75dpi xfonts-scalable xdialog xterm rxvt"

$ECHO chroot $lessdisks_path apt-get install $chroot_packages
$ECHO apt-get install $server_packages

lil_pause

$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

$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

$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\
    EXAMPLE: chroot $lessdisks_path apt-get install xserver-mach64\n\
Good luck." 18 78
