#!/bin/sh
# Copyright (C) 1994-1998 Florian La Roche
#
# First read the whole script and check if it will work for you.
# I haven't put in any checks!
#

export PATH=/bin:/usr/bin:/sbin:/usr/sbin

DIR=`pwd`
SIZE=3584

A=
test "$ARCH" = alpha && A=.alpha

LIBC=`cd /lib && ls libc.so.5.4.*`
LIBCURSES=`cd /lib && ls libncurses.so.3.0.*`

dd if=/dev/zero of=/dev/ramdisk bs=1024 count=$SIZE
mke2fs -q -i 1024 -m 0 /dev/ramdisk $SIZE
tune2fs -i 0 /dev/ramdisk
mount -v -t ext2 /dev/ramdisk /mnt || exit 1

cd /mnt || exit 1
chmod 755 . || exit 1
mkdir -p bin dev etc lib mnt proc sbin/init.d source install tmp var/run \
	usr/bin usr/share/kbd/keymaps/i386/{qwertz,include} usr/lib/terminfo/l || exit 1
chmod 1777 tmp || exit 1
: > var/run/utmp
chown root.tty var/run/utmp

tar xzplf ~ftp/pub/linux/binary/tar/base/devs.tgz dev install
sh install/doinst.sh
rm -f dev/MAKEDEV install/doinst.sh
cp -a /dev/initctl dev

ADDBIN="ping mv kill loadkeys"
ADDUSRBIN="free rlogin ftp telnet"

SBIN="badblocks bootpc cfdisk fdisk mingetty init ifconfig killall5 mke2fs mkswap halt reboot route swapon swapoff shutdown update e2fsck"
BIN="cat chmod chown cp df gzip hostname kill ln ls mkdir mount netstat ps pwd rm sleep sync tar umount more $ADDBIN"
USRBIN="du test gzip pico ksh killall $ADDUSRBIN"
LIB="libc.so.5 libncurses.so.3.0"
ETC="ld.so.cache inputrc"

for i in $SBIN; do
  cp -pd /sbin/$i sbin || exit 1
done
for i in $BIN; do
  cp -pd /bin/$i bin || exit 1
done
for i in $USRBIN; do
  cp -dp /usr/bin/$i usr/bin || exit 1
done
for i in $LIB; do
  cp -dp /lib/$i lib || exit 1
done
for i in $ETC; do
  cp -dp /etc/$i etc || exit 1
done
cp -dp /usr/sbin/klogd sbin || exit 1
cp -dp /lib/ld-linux.so.1.9.9 lib/ld-linux.so.1

cp -dp /usr/share/kbd/keymaps/i386/qwertz/de-latin1{,-nodeadkeys}.map.gz usr/share/kbd/keymaps/i386/qwertz/
cp -dp /usr/share/kbd/keymaps/i386/include/{linux-keys-bare,linux-with-alt-and-altgr,qwertz-layout}.inc usr/share/kbd/keymaps/i386/include/
cp -dp /usr/lib/terminfo/l/linux   usr/lib/terminfo/l/

cat > etc/profile <<EOF
PATH=/bin:/sbin:/usr/bin:/install/bin:/install/sbin:/install/usr/sbin:/install/usr/bin
LD_LIBRARY_PATH=/lib:/install/lib:/install/usr/X11R6/lib
LESSCHARSET=latin1
LESS=-m
LESSKEY=/install/etc/lesskey.bin
export PATH LD_LIBRARY_PATH LESSCHARSET LESS LESSKEY
alias l='ls -la'
alias ..='cd ..'
alias ...='cd ../..'
alias md=mkdir
alias rd=rmdir
alias o=more
alias german='loadkeys de-latin1-nodeadkeys'
set -o emacs
EOF

cat > etc/rc <<EOF
#!/bin/sh
export PATH=/bin:/sbin
export HOME=/
export TERM=linux
umask 022
update
mount -n -o remount,rw /
rm -f /etc/mtab
mount -at nonfs
klogd -c 1 -f /dev/tty12
#klogconsole -l 1 -r 8
ifconfig lo 127.0.0.1
route add 127.0.0.1
EOF
chmod 755 etc/rc

cat > etc/inittab <<EOF
id:2:initdefault:
si::sysinit:/etc/rc
ca::ctrlaltdel:/sbin/shutdown -r -t 4 now
l0:0:wait:/sbin/init.d/halt
l6:6:wait:/sbin/init.d/reboot
1:12:respawn:/sbin/mingetty --noclear tty1
2:12:respawn:/sbin/mingetty tty2
3:12:respawn:/sbin/mingetty tty3
4:12:respawn:/sbin/mingetty tty4
EOF

cat > etc/host.conf <<EOF
order hosts,bind
multi on
EOF
cat > etc/resolv.conf <<EOF
nameserver 127.0.0.1
EOF
cat > etc/hosts <<EOF
127.0.0.1	localhost
EOF

cat > etc/issue <<EOF

        Welcome to jurix-Linux by Florian La Roche.

Login as 'root'. Use 'fdisk' or 'cfdisk' to partition your harddisk
and then use 'setup' for a menue-guided installation. After everything
is done, just use Crl-Alt-Del to reboot your computer.

Start 'german' if you have a german keyboard.
Start 'trybootp eth0' to get network settings via bootp.

You might find the following progs useful: pico (editor), more, fdisk,
cfdisk, mke2fs, mkswap, e2fsck, ifconfig, route, ping, rlogin, telnet,
netstat, ftp, ps, kill, gzip, tar.

If you mount a Linux partition on /install, you can also start those
binaries. (PATH and /etc/ld.so.conf are configured accordingly.)

\l
EOF

cat > etc/fstab <<EOF
/dev/fd0	/	ext2	defaults	0	1
none		/proc	proc	defaults	0	0
EOF

cat > etc/ld.so.conf <<EOF
/install/lib
/install/usr/lib
EOF

cat > etc/ttytype <<EOF
linux tty1
linux tty2
linux tty3
EOF

cat > etc/passwd <<EOF
root::0:0:root:/:/bin/sh
daemon:x:1:2::/:/bin/sh
lp:x:4:7::/:/bin/sh
news:x:9:13::/:/bin/sh
uucp:x:10:14::/:/bin/sh
games:x:12:100::/:/bin/sh
man:x:13:2::/:/bin/sh
at:x:25:25::/:/bin/sh
postgres:x:26:2::/:/bin/sh
mdom:x:27:27::/:/bin/sh
named:x:28:28::/:/bin/sh
amanda:x:37:6::/:/bin/sh
ftp:x:40:2::/local/ftp:/bin/sh
wwwrun:x:41:-2::/:/bin/sh
squid:x:42:-2::/:/bin/sh
msql:x:43:-2::/:/bin/sh
gnats:x:44:-2::/:/bin/sh
nobody:x:-2:-2::/:/bin/sh
EOF
chmod 644 etc/passwd

cat > etc/group <<EOF
root:x:0:root
other:x:1:nobody
daemon:x:2:
tty:x:5:
disk:x:6:
lp:x:7:
kmem:x:9:
wheel:x:10:
news:x:13:news
uucp:x:14:uucp,root
dialout:x:15:
shadow:x:16:
at:x:25:at
mdom:x:27:mdom
named:x:28:
ftp:x:30:
game:x:40:
users:x:100:
nogroup:x:-2:
EOF
chmod 644 etc/group

cat > etc/services <<EOF
login		513/tcp
EOF
chmod 644 etc/services
cat > etc/protocols <<EOF
icmp	1	ICMP
EOF
chmod 644 etc/protocols

cp $DIR/trybootp sbin/
cp $DIR/termcap etc/
cp $DIR/setup$A sbin/
#cp $DIR/klogconsole$A sbin/
cp $DIR/login$A bin/
cp $DIR/reboot sbin/init.d/
strip --strip-unneeded /lib/$LIBC -o lib/$LIBC
strip --strip-unneeded /lib/$LIBCURSES -o lib/$LIBCURSES
ln -s reboot sbin/init.d/halt
ln -s /usr/bin/ksh bin/sh
cp ~florian/binary/docs/README.txt .

touch etc/mtab

sync && sync
df /mnt
cd $DIR || exit 1
umount /mnt || exit 1

dd if=/dev/ramdisk count=$SIZE bs=1024 | gzip -9 > rootdisk
ls -l rootdisk
dd if=/dev/zero of=/dev/ramdisk count=1440 bs=1024
dd if=rootdisk of=/dev/ramdisk
dd if=/dev/ramdisk of=rootdisk count=1440 bs=1024

echo Ready
