#! /bin/sh

PATH="/usr/sbin:/usr/bin:/sbin:/bin"
CLEAR=`clear || echo '[H[J'`

trap "" 1 2 3 15

export DPKG_NO_TSTP="yes"

rm -f /root/.bash_profile
mv /root/.profile.real /root/.profile

if [ -f /etc/environment ]; then
	. /etc/environment
fi

TEXTDOMAINDIR=/root/messages
TEXTDOMAIN=rootprofile
export LANG TEXTDOMAINDIR TEXTDOMAIN
if [ -x /usr/bin/gettext ]; then
	echo="gettext -s"
else
	echo="echo"
fi

N=`${echo} N`
n=`${echo} n`

if [ -f /root/kbdconf ]; then
	MAP=/usr/share/keymaps/`cat /root/kbdconf`.map.gz
	loadkeys ${MAP} ; dumpkeys > /etc/kbd/default.map
	gzip -9fv /etc/kbd/default.map
	rm /root/kbdconf
fi
	
if [ ! -f /etc/kbd/default.map.gz -a -f /bin/loadkeys ]; then
	kbdconfig
	loadkeys /etc/kbd/default.map
	if [ $? -ne 0 ]; then
		${echo} "Loading the new keymap failed."
		read foo
	fi
fi

echo $CLEAR
${echo} "  Before proceeding, you need to set a password for 'root', the system
administrative account.  (The root account is the account that you're
currently using).  The root password shouldn't be easy to guess, and
it shouldn't be a word found in the dictionary, or a word that could
be easily associated with you, like your middle name.  A good password
will contain a mixture of letters, numbers and punctuation and will be
changed at regular intervals.  The root password is changed by running
the 'passwd' program as root.
  Why such caution?  The root account doesn't have the restrictions that
normal user accounts have.  root can read, modify or erase any file on
the system, change the ownerships and permissions of any file, and run
programs that require special privileges (such as programs that format
the hard disk!).  A malicious or unqualified user with root access can
have disastrous results.
  For the same reason, it's a bad idea to use the root account for normal
day-to-day activities, such as the reading of electronic mail, because
even a small mistake can result in disaster.  As soon as you're logged
in as root, you should create a normal user account.  You should use the
normal user account whenever you're not maintaining the system.  (How to
do this will be described in a minute.)
"
passwd
pass=`cat /etc/passwd | fgrep root | cut -d : -f 2`
while [ -z "$pass" ]
do
{
  ${echo} "
Please try again."
  passwd
  pass=`cat /etc/passwd | fgrep root | cut -d : -f 2`
}
done

${echo} -n "
As mentioned above, it is a bad idea to use the root account except
in cases that you need special privileges (such as when you need to
mount a file system). Now you may create a normal user account.  

Note that you may create it later (as well as any additional account) 
by typing 'adduser <username>' as root, where <username> is an
(eight-character or less) user name, like 'imurdock' or 'rms'.

Shall I create a normal user account now? [Y/n] "
read answer
case "$answer" in
  $N|$n)
    true
    ;;
  *)
    while true
    do
    {
      ${echo} -n "Enter a username for your account: "
      read username
      echo
      LANG=C adduser $username
      if [ $? = 0 ]
      then
        break
      fi
    }
    done
    ;;
esac

if [ -x /usr/sbin/shadowconfig ]; then
	${echo} -n "
Configure shadow passwords

Shadow passwords make your system more secure because nobody is
able to view even encrypted passwords.  Passwords are stored in
a separate file that can only be read by special programs.  We
recommend the use of shadow passwords.  If you're going to use
NIS you could run into trouble.

Shall I install shadow passwords? [Y/n] "
	read answer
	case "$answer" in
	$N|$n)
		true
		;;
	*)
		shadowconfig on
		;;
	esac
fi

# Check whether the system needs pcmcia
if [ -e /lib/modules/`uname -r`/pcmcia/.unconfigured ]; then
	${echo} -n "
It seems your system doesn't need PCMCIA. It was installed with the rest
of the kernel. I will remove it now.

Shall I remove the pcmcia packages? [Y/n] "

	read answer
	case "$answer" in
	  $N|$n)
	    true
	    ;;
	  *)
	    (dpkg --purge pcmcia-cs pcmcia-modules-`uname -r` 2>&1)>/dev/null
	    (rm -f /lib/modules/`uname -r`/pcmcia/.unconfigured)>/dev/null
	    ;;
	esac
fi

if [ -f /etc/inittab.real ]; then
	mv -f /etc/inittab.real /etc/inittab
	sync
	kill -HUP 1
fi

if [ -f /root/ppp ]; then 
	${echo} -n "
If you have an account on an ISP, and you want to use it to fetch the 
packages to install on the system from the Internet, you may configure
the PPP service now, and I will open a PPP connection to your ISP.

Do you want to use a PPP connection to install the system? [Y/n] "
	read answer
	case "$answer" in
	  $N|$n)
	    rm -f /root/ppp
	    ;;
	  *)
	    pppconfig
	    ${echo} -n "
Do you want me to start a PPP connection now? [Y/n] "
	    read answer
	    case "$answer" in
	      $N|$n)
	        rm -f /root/ppp
	        ;;
	      *)
	        status=1
		while [ "$status" != "0" ]; do
	          pon
		  status=$?
                  if [ "$status" != "0" ]; then
                    ${echo} -n "Couldn't establish connection. Retry?  [Y/n] "
                    read answer
                    case "$answer" in
	              $N|$n)
                        rm -f /root/ppp
                        status=0
                        ;;
                    esac
                  fi
		done
	        ;;
	    esac
	    ;;
	esac
fi	
	
echo $CLEAR
${echo} "
Now you may choose one of several selections of packages to be installed.
If you prefer to select one by one which packages to install on the system
you may skip this step, else you may skip the 'Select' step later when I
run the 'dselect' program. 

Do you want to perform this step? [Y/n] "
read answer
case "$answer" in
  $N|$n)
    true
    ;;
  *)
    /root/pkgsel
    touch /root/preselected
    ;;
esac

rm -fr /root/pkgsel /root/tasks /root/profiles /root/rev_task 

echo $CLEAR
${echo} "
I'm now going to start the 'dselect' program.  'dselect' is used to select
which of the hundreds of packages included with Debian GNU/Linux to install
on the system.  (It is also used for general maintenance of the packages on
the system, but you'll be primarily interested in installation now.)"
if [ -f /root/preselected ]; then
  ${echo} "
Because you have chosen already a preselected set of packages you should 
skip the [S]elect step. Just use [A]ccess, [U]pdate and go directly to
[I]nstall afterwards.
"
    rm -f /root/preselected
else   
  ${echo} "
You should follow the on-line instructions to select any packages that you
want to have on your system, such as GNU Emacs, TeX or the X Window System.
A default set of packages are automatically selected that provide a fairly
complete Unix-like environment, so manual selection of packages is optional.
"
fi
${echo} -n "Press <ENTER> to continue: "
read enter

dselect

if [ -f /root/ppp ]; then 
	rm -f /root/ppp
        ${echo} -n "
The installation is complete. I may close the PPP connection to your 
ISP now or I may keep it open if you want.

Do you want me to close the PPP connection? [Y/n] "
        read answer
        case "$answer" in
	  $N|$n)
	    ${echo} "Remember to use 'poff' to close it when you finish."
	    ;;
          *)
            poff
	    ;;
        esac
fi

${echo} "
You may now login as 'root' at the  login:  prompt.  To create a normal
user account, you should run 'adduser' as root with a user name as an
argument.  For example, to create a user called 'imurdock', you would
type this at the shell prompt: 'adduser imurdock'."

expr "`tty`" : /dev/ttyS >/dev/null || ${echo} "
You may also take advantage of the multi-tasking features of Debian
GNU/Linux by pressing <Left Alt><Fn> to switch to a new 'virtual
console', where n is the number of the virtual console to switch to.
For example, to switch to virtual console #3, you would press <Left
Alt><F3>, and to return to this virtual console (virtual console #1),
you would press <Left Alt><F1>."

${echo} "
Have fun!
"

rm -rf /root/messages
exit
