#!/bin/sh
T_PX="`cat /tmp/SeTT_PX`"
ROOT_DEVICE="`cat /tmp/SeTrootdev`"
if [ ! -d $T_PX/bin -a ! -d $T_PX/etc ]; then # if there no Linux here, exit
 exit
fi
( cd $T_PX ; chmod 755 ./ )
( cd $T_PX ; chmod 755 ./var )
if [ -d $T_PX/usr/src/linux ]; then
 chmod 755 $T_PX/usr/src/linux
fi
if [ ! -d $T_PX/proc ]; then
 mkdir $T_PX/proc
 chown root.root $T_PX/proc
fi
if [ ! -d $T_PX/var/spool/uucp ]; then
 mkdir -p $T_PX/var/spool/uucp
fi
chown uucp.uucp $T_PX/var/spool/uucp
chmod 1777 $T_PX/var/spool/uucp
if [ ! -d $T_PX/var/spool/uucppublic ]; then
 mkdir -p $T_PX/var/spool/uucppublic
fi
chown uucp.uucp $T_PX/var/spool/uucppublic
chmod 1777 $T_PX/var/spool/uucppublic
chmod 1777 $T_PX/tmp
if [ ! -d $T_PX/var/spool/mail ]; then
 mkdir -p $T_PX/var/spool/mail
 chmod 755 $T_PX/var/spool
 chown root.mail $T_PX/var/spool/mail
 chmod 775 $T_PX/var/spool/mail
fi 

# Install the bootdisk's Linux kernel:
. /usr/lib/setup/SeTkernelj

# make GRUB menu.lst
    cat <<EOF > $T_PX/grub/menu.lst
# Linux HDD boot menu
default = 0
title = boot Linux from $ROOT_DEVICE
kernel = /vmlinuz root=$ROOT_DEVICE ro 

EOF


dialog --title "ƥ" --yesno \
"LinuxƥγƼԤޤ󥹥ȡ뤷 \n\
ƥǤСԤäƤʤȡ˥֡ȤǤ \n\
ǽޤԤʤΤʤᥤ˥塼äƤ \n\
ͥ륤᡼򥤥󥹥ȡ뤷Ȥ LILO \n\
LinuxΥˤ򥤥󥹥ȡ뤷ꡢͥѤΥ֡ \n\
ǥ뤿ˡƥꤹ뤳ȤڤǤ \n\
ƥԤޤ" 14 74 
if [ $? = 1 -o $? = 255 ]; then
 exit
fi
while [ 0 ]; do # the bootdisk menu loop
 dialog --title "֡ȥǥ" --menu \
"֡ȥǥ˵ưǥȴƤ뤳Ȥǧ \n\
Ƥ\n\
ǤϡƥΤΥ֡ȥǥ \n\
뤳Ȥ򶯤ᤷޤ֡ȥǥˤϣ \n\
ޤñʤ֡ȥǥʥͥ륤᡼ǥ\n\
ľܽ񤭹ΡˤLILOȤä֡ȥǥǤ\n\
ɤμΥ֡ȥǥޤ" 18 76 5 \
"format" "/dev/fd0ǥեåԡեޥåȤ" \
"simple" "ͥ륤᡼vmlinuz/dev/fd0Υ֡ȥǥؽ񤭹" \
"lilo" "LILOΥ֡ȥǥ" \
"continue" "֡ȥǥλ³" 2> /tmp/return
 if [ ! $? = 0 ]; then
  break;
 fi
 REPLY=`cat /tmp/return`
 rm -f /tmp/return
 if [ "$REPLY" = "format" ]; then
  dialog --title "եåԡ̤λ" --menu \
"/dev/fd0(a:)ΥǥưǥؤƤ뤳Ȥ \
ǧƤ/dev/fd0 Υǥ̤ϡ" 12 72 3 \
"1.44" "1.44 MB" \
"1.2" "1.2 MB" 2> /tmp/return
  if [ "`cat /tmp/return`" = "1.44" ]; then
   dialog --title "/dev/fd0u1440եޥå" --infobox "/dev/fd01.44ᥬ\
ХȤǥեޥåȤƤޤ" 3 70 
   fdformat -n /dev/fd0u1440 1> /dev/null 2> /dev/null
  elif [ "`cat /tmp/return`" = "1.2" ]; then
   dialog --title "/dev/fd0h1200եޥå" --infobox "/dev/fd01.2ᥬ \
ХȤǥեޥåȤƤޤ" 3 70
   fdformat -n /dev/fd0h1200 1> /dev/null 2> /dev/null
  fi
  rm -f /tmp/return
 elif [ "$REPLY" = "simple" ]; then # make simple bootdisk
  dialog --title "֡ȥǥ" --yesno \
"\n\
֡ȥɥ饤֡ʤƤ3.5FDDˤ˥եޥå \n\
ѤߤΥեåԡƤ 줬Linux \n\
֡ȥǥȤʤޤLILOꤷơϡ \n\
ǥ֡ȤǤ褦ˤʤޤǤϡΥեåԡ \n\
ѤƤ \n\n\
ΥեåԡǥƤϾ񤭤ޤ\n\n\
YESǥǥNOߤޤ\n" 14 70
  if [ $? = 0 ]; then
   dialog --title "ǥ" --infobox "'vmlinux'֡ȥǥƤޤ..." 5 70
   KERNEL="/vmlinuz"
   dd if=$T_PX$KERNEL of=/dev/fd0 2> /dev/null
   rdev /dev/fd0 $ROOT_DEVICE
   rdev -v /dev/fd0 -1
   rdev -R /dev/fd0 1
   sync
  fi
 elif [ "$REPLY" = "lilo" ]; then # make lilo bootdisk
  dialog --title "/dev/fd0LILOΥ֡ȥǥ" --yesno "
֡ȥɥ饤֡̾3.5FDDˤ˥եޥåȺѤߤΥեåԡ \n\
Ƥ줬Linuxƥư֡ȥǥȤ \n\
ޤեåԡƤϾ񤭤ޤYESǥǥ \n\
NOߤޤ" 9 72 
  if [ $? = 0 ]; then # make the disk
   dialog --title "եåԡ̤λ" --menu "/dev/fd0(a:)Υǥ\
̤ϡ" 9 70 2 \
"1.44" "1.44 MB" \
"1.2" "1.2 MB" 2> /tmp/return
   if [ "`cat /tmp/return`" = "1.44" ]; then
    DEV=/dev/fd0u1440
   else
    DEV=/dev/fd0h1200
   fi
   dialog --infobox "$ROOT_DEVICEΤLILO֡ȥǥ..." 3 70
   mke2fs -i 1024 $DEV 1> /dev/null 2> /dev/null
   if [ ! -d /tmp/lilo ]; then
    mkdir /tmp/lilo
   fi
   mount -t ext2 /dev/fd0 /tmp/lilo 1> /dev/null 2> /dev/null
   cp $T_PX/vmlinuz /tmp/lilo/vmlinuz
   cp -a /dev /tmp/lilo
   mkdir /tmp/lilo/etc
   cat << EOF > /tmp/lilo/etc/lilo.conf
boot = /dev/fd0
message=/boot/message
prompt
image = /vmlinuz
        label = mount
        ramdisk = 0
        root = $ROOT_DEVICE
        vga = normal
        read-only
EOF
   cp -a /boot /tmp/lilo
   cat << EOF > /tmp/lilo/boot/message

Welcome to the Plamo Linux custom LILO bootdisk!  

By default, this disk boots a root Linux partition on $ROOT_DEVICE when
you hit ENTER.  If you'd like to boot some other partition, use a command
like this on the LILO prompt below:

    mount root=/dev/sda1 ro

Where "/dev/sda1" is the partition you want to boot, and "ro" specifies that
the partition should be initially mounted as read-only.  If you wish to mount
the partition read-write, use "rw" instead.  You may also add any other kernel
parameters you might need depending on your hardware, and which drivers are
included in your kernel.

EOF
   lilo -r /tmp/lilo 1> /dev/null 2> /dev/null
   umount /tmp/lilo
   rm -rf /tmp/lilo
  fi

  elif [ "$REPLY" = "initrd" ]; then # make initrd bootdisk
  dialog --title "/dev/fd0initrd ֡ȥǥ" --yesno "
֡ȥɥ饤֡̾3.5FDDˤ˥եޥåȺѤߤ \n\
եåԡƤΥǥ˥⥸塼 \n\
ߤ initrd ޤ줬Linuxƥư \n\
֡ȥǥȤʤޤեåԡƤϾ \n\
ޤYESǥǥNOߤޤ" 10 72 
  if [ $? = 0 ]; then # make the disk
   dialog --title "եåԡ̤λ" --menu "/dev/fd0(a:)Υǥ\
̤ϡ" 9 70 2 \
"1.44" "1.44 MB" \
"1.2" "1.2 MB" 2> /tmp/return
   if [ "`cat /tmp/return`" = "1.44" ]; then
    DEV=/dev/fd0u1440
   else
    DEV=/dev/fd0h1200
   fi
   dialog --infobox "$ROOT_DEVICEΤinitrdǥ..." 3 70
   mke2fs -i 1024 $DEV 1> /dev/null 2> /dev/null
   if [ ! -d /tmp/lilo ]; then
    mkdir /tmp/lilo
   fi
   mount -t ext2 /dev/fd0 /tmp/lilo 1> /dev/null 2> /dev/null
   echo "FD  initrd 档"
   # make initrd
   cp -a /dev /tmp/lilo
   mkdir /tmp/lilo/bin
   cp /bin/sh /tmp/lilo/bin
   mkdir /tmp/lilo/sbin
   cp /sbin/insmod /tmp/lilo/sbin
   mkdir /tmp/lilo/lib
   mkdir /tmp/lilo/lib/modules
   cp /lib/ld.so /tmp/lilo/lib/
   cp /lib/libc.so.4.7.6 /tmp/lilo/lib/
   cd /tmp/lilo/lib
   ln -s libc.so.4.7.6 libc.so.4
   cd /

   PC=$T_PX/lib/modules/`uname -r`/pcmcia
   if [ -d $PC ]; then
      mkdir /tmp/lilo/lib/`uname -r`
      cp -r $PC /tmp/lilo/lib/`uname -r`
      cp /sbin/cardmgr /tmp/lilo/sbin
      mkdir /tmp/lilo/etc
      mkdir /tmp/lilo/etc/rc.d
      cp /etc/rc.d/rc.pcmcia /tmp/lilo/etc/rc.d
   else
      cp /tmp/modules/* /tmp/lilo/lib/modules/
   fi

   echo "#!/bin/sh " > /tmp/lilo/linuxrc
   if [ -d $PC ]; then
	  echo "/etc/rc.d/rc.pcmcia start" > /tmp/lilo/linuxrc
   else
      cat /tmp/init_cmd >> /tmp/lilo/linuxrc
   fi

   chmod +x /tmp/lilo/linuxrc
   umount /tmp/lilo
   echo "FD ˺ initrd ɤߤǤޤ"
   dd if=$DEV of=/mnt/tmp/initrd bs=10k count=144
   # make lilo disk
   echo "FD  initrd Ѥ lilo boot disk ޤ"
   mke2fs -i 1024 $DEV 1> /dev/null 2> /dev/null
   mount -t ext2 /dev/fd0 /tmp/lilo 1> /dev/null 2> /dev/null
   mkdir /tmp/lilo/etc
   cp $T_PX/vmlinuz /tmp/lilo/vmlinuz
   cp -a /dev /tmp/lilo
   gzip /mnt/tmp/initrd
   cp /mnt/tmp/initrd.gz /tmp/lilo
   cp /mnt/tmp/initrd.gz $T_PX
   cat << EOF > /tmp/lilo/etc/lilo.conf
boot = /dev/fd0
append="root=$ROOT_DEVICE"
message=/boot/message
prompt
image = /vmlinuz
        label = initrd
        root = $DEV
        vga = normal
        initrd = /initrd.gz
        read-only
EOF
   cp -a /boot /tmp/lilo
   cat << EOF > /tmp/lilo/boot/message

Welcome to the Plamo Linux custom INITRD bootdisk!  

By default, this disk load initrd into ramdisk and install
modules. Then boots a root Linux partition on $ROOT_DEVICE when you
hit ENTER.  If you'd like to boot some other partition, use a command
like this on the LILO prompt below:

    mount root=/dev/sda1 ro

Where "/dev/sda1" is the partition you want to boot, and "ro" specifies that
the partition should be initially mounted as read-only.  If you wish to mount
the partition read-write, use "rw" instead.  You may also add any other kernel
parameters you might need depending on your hardware, and which drivers are
included in your kernel.

EOF
   lilo -r /tmp/lilo 1> /dev/null 2> /dev/null
   umount /tmp/lilo
   rm -rf /tmp/lilo
  fi

 elif [ "$REPLY" = "continue" ]; then
  break;
 fi
done
dialog --title "ǥ" --yesno \
"\n\
ǤȯƥǥХ(ttyS0ttyS1ttyS2ttyS3)/dev/modemؤ\n\
󥯤ĥޤǥ³ΥݡȤѹϡ\n\
󥯤ѹ뤳ȤǤޤʣΥǥȤϡ\n\n\
/etc/rc.d/rc.serial Ƥ\n\
ǥꤷޤ\n" 14 73
if [ $? = 0 ]; then
 dialog --title "ȯƥǥХ" \
--menu "ǥ³ȯƥǥХꤷƤ" 12 60 4 \
"ttyS0" "DOSǤcom1:" \
"ttyS1" "DOSǤcom2:" \
"ttyS2" "DOSǤcom3:" \
"ttyS3" "DOSǤcom4:" 2> /tmp/callout
 if [ $? = 1 ]; then
  rm -f /tmp/callout
 else
  MODEM_DEVICE="`cat /tmp/callout`"
  rm -f /tmp/callout
  (cd $T_PX/dev; ln -sf $MODEM_DEVICE modem)
 fi
fi

dialog --title "ޥ" \
--yesno "\n\
줫ޥԤޤǻꤷޥǥХ\n\
/dev/mouseإ󥯤ĥޤ̤Υޥ˼괹硢\n\
Υ󥯤ѹƤޥꤷޤ" 12 70
if [ $? = 0 ]; then
 dialog --title "ޥפ" --menu "ʲΥפΥޥݡȤƤޤ" 14 74 7 \
"1" "MicrosoftߴΥꥢޥ" \
"2" "C&T 82C710뤤PS/2Υޥ (Auxiliary port)" \
"3" "LogitechХޥ" \
"4" "ATI XLХޥ" \
"5" "MicrosoftХޥ" \
"6" "Mouse Systemsꥢޥ" \
"7" "Logitech (MouseMan)ꥢޥ" 2> /tmp/mtype
 if [ $? = 1 ]; then
  rm -f /tmp/mtype
 fi
 if [ -f /tmp/mtype ]; then
  MOUSE_TYPE="`cat /tmp/mtype`"
 else
  unset MOUSE_TYPE
 fi
 rm -f /tmp/mtype
 if [ "$MOUSE_TYPE" = "1" -o "$MOUSE_TYPE" = "6" -o "$MOUSE_TYPE" = "7" ]; then
  dialog --title "ꥢݡȤ" --menu "ʤΥޥϥꥢݡȤ³ޤ\n\
ɤ³ޤ" 12 60 4 \
"ttyS0" "DOSǤcom1:" \
"ttyS1" "DOSǤcom2:" \
"ttyS2" "DOSǤcom3:" \
"ttyS3" "DOSǤcom4:" 2> /tmp/mport
  if [ $? = 1 ]; then
   rm -f /tmp/mport
  fi
  if [ -f /tmp/mport ]; then
   MOUSE_DEVICE="`cat /tmp/mport`"
  else
   unset MOUSE_DEVICE
  fi
  rm -f /tmp/mport
  if [ "$MOUSE_TYPE" = "1" ]; then
   MTYPE="ms"
  elif [ "$MOUSE_TYPE" = "7" ]; then
   MTYPE="mman"
  else
   MTYPE="msc"
  fi
  (cd $T_PX/dev; ln -sf $MOUSE_DEVICE mouse)
 elif [ "$MOUSE_TYPE" = "2" ]; then
  (cd $T_PX/dev; ln -sf psaux mouse)
  MTYPE="ps2"
 elif [ "$MOUSE_TYPE" = "3" ]; then
  (cd $T_PX/dev; ln -sf logibm mouse)
  MTYPE="logi"
 elif [ "$MOUSE_TYPE" = "4" ]; then
  (cd $T_PX/dev; ln -sf atibm mouse)
  MTYPE="bm"
 elif [ "$MOUSE_TYPE" = "5" ]; then
  (cd $T_PX/dev; ln -sf inportbm mouse)
  MTYPE="bm"
 fi
fi
export MTYPE

# Post installation and setup scripts added by packages.
if [ -d $T_PX/var/log/setup ]; then
 for INSTALL_SCRIPTS in $T_PX/var/log/setup/setup.*
 do
  SCRIPT=`basename $INSTALL_SCRIPTS`
  # Here, we call each script in /var/log/setup. Two arguments are provided:
  # 1 -- the target prefix (normally /, but /mnt from the bootdisk)
  # 2 -- the name of the root device.
  ( cd $T_PX ; sh var/log/setup/$SCRIPT $T_PX $ROOT_DEVICE )
  if echo $SCRIPT | fgrep onlyonce 1> /dev/null 2> /dev/null; then # only run after first install
   if [ ! -d $T_PX/var/log/setup/install ]; then
    mkdir $T_PX/var/log/setup/install
   fi
   mv $INSTALL_SCRIPTS $T_PX/var/log/setup/install
  fi
 done
fi

# Load keyboard map (if any) when booting
LOADER="/usr/bin/loadkeys"
KEYDIR="/usr/lib/kbd/keytables"
MAP="$T_PX/etc/rc.d/rc.keymap"

if [ -r /tmp/keyset ]; then
   echo "#!/bin/sh" > $MAP

	if fgrep 106 /tmp/keyset 1> /dev/null 2>/dev/null ; then
		if fgrep cl2c  /tmp/keyset  1> /dev/null 2>/dev/null  ; then
			echo "$LOADER $KEYDIR/jp106.map $KEYDIR/cl2c-d.map" >> $MAP
		else
			echo "$LOADER $KEYDIR/jp106.map" >> $MAP
		fi

	elif fgrep cl2c /tmp/keyset 1> /dev/null 2>/dev/null  ; then
		if fgrep k2esc  /tmp/keyset 1> /dev/null 2>/dev/null  ; then
			echo "$LOADER $KEYDIR/defkeymap.map $KEYDIR/cl2c-d.map $KEYDIR/k2esc-d.map" >> $MAP
		else
			echo "$LOADER $KEYDIR/defkeymap.map $KEYDIR/cl2c-d.map" >> $MAP
		fi

	elif fgrep k2esc  /tmp/keyset  1> /dev/null 2>/dev/null  ; then
		echo "$LOADER/defkeymap.map $KEYDIR/k2esc-d.map" >> $MAP
	fi
	chmod 755 $MAP
fi
