#!/bin/bash

# Chroot install - PLD Th
# Script for PLD Rescue CD v2.97 - install base system
# Author: Kamil Mroczkowski
#	  nospe@o2.pl or kamil@kamilm.net
#
# Copyright (c) 2010 Kamil Mroczkowski
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

# Script config
HDD_TYPE="sata"			# Hard disk type: ide, sata, scsi
MBRD="/dev/sda"			# Or auto - where install bootloader
SWAP="/dev/sda1"		# SWAP
HDDG="/dev/sda2"		# /
DIRG="/d"			# Temporary install dir
SYSG="ext3"			# File system
BOOTLOADER="lilo"		# Bootloader: lilo, grub, grub2
KEEPDOWNLOAD="no"		# or yes
OFFLINE="no"			# Install offline (Poldek Packages must be /root/.poldek-cache/)
HOSTNAME="pldmachine"		# Hostname

if [ "$HDD_TYPE" != "sata" -a "$HDD_TYPE" != "ide" -a "$HDD_TYPE" != "scsi" ];
then
 echo "HDD_TYPE: please correct define!";
 exit 1
fi

if [ `fdisk -l | wc -l 2>/dev/null` == "0" ];
then
 echo "Hard disk not found?!"
 exit 1
fi

if [ "$MBRD" != "auto" -a `fdisk -l | grep $MBRD | wc -l 2>/dev/null` == "0" ];
then
 echo "MBRD: device not found?"
 exit 1
fi

if [ `fdisk -l | grep $SWAP | wc -l 2>/dev/null` == "0" ];
then
 echo "SWAP: device not found?"
 exit 1
fi

if [ `fdisk -l | grep $HDDG | wc -l 2>/dev/null` == "0" ];
then
 echo "HDDG: device not found?"
 exit 1
fi

if [ "$SYSG" != "ext2" -a "$SYSG" != "ext3" -a "$SYSG" != "ext4" -a "$SYSG" != "reiser4" -a "$SYSG" != "reiserfs" -a "$SYSG" != "xfs" ];
then
 echo "SYSG: please correct define filesystem!";
 exit 1
fi

if [ "$BOOTLOADER" != "grub" -a "$BOOTLOADER" != "lilo" -a "$BOOTLOADER" != "grub2" ];
then
 echo "BOOTLOADER: please correct define!";
 exit 1
fi

if [ "$OFFLINE" == "yes" -a ! -d /root/.poldek-cache/ ];
then
 echo "OFFLINE: can't see repo in /root/.poldek-cache/ !";
 exit 1
fi

if [ "${#HOSTNAME}" == "0" ];
then
 echo "HOSTNAME: please define hostname!";
 exit 1
fi

# Let's go...
if [ $SYSG == "xfs" ]; then
 MKFSARG="-f"
fi

echo "Start install..."

# Partition
mkswap $SWAP
mkfs.$SYSG $MKFSARG $HDDG
mkdir $DIRG

swapon $SWAP
mount -t $SYSG $HDDG $DIRG

# Poldek
mkdir -p $DIRG/var/cache/poldek

if [ "$OFFLINE" == "yes" ];
then
 cp -r /root/.poldek-cache/* $DIRG/var/cache/poldek
fi

# Poldek - config
sed "s/#cachedir = \$HOME\/.poldek-cache/cachedir = \\$DIRG\/var\/cache\/poldek/g" /etc/poldek/poldek.conf > /etc/poldek/poldek.conf.new
mv /etc/poldek/poldek.conf.new /etc/poldek/poldek.conf
sed "s/#suggests = yes/suggests = no/g" /etc/poldek/poldek.conf > /etc/poldek/poldek.conf.new
mv /etc/poldek/poldek.conf.new /etc/poldek/poldek.conf
if [ "$KEEPDOWNLOAD" == "yes" ];
then
 sed "s/#keep downloads = no/keep downloads = yes/g" /etc/poldek/poldek.conf > /etc/poldek/poldek.conf.new
 mv /etc/poldek/poldek.conf.new /etc/poldek/poldek.conf
fi

# Init RPM DB
rpm --root $DIRG --initdb

# Install RPMS
poldek --root $DIRG -i setup FHS dev pwdutils chkconfig SysVinit dhcpcd poldek vim geninitrd cpio mount login mingetty mc pldconf iputils-* reiserfsprogs xfsprogs progsreiserfs e2fsprogs util-linux-ng wget screen lynx kbd localedb-src pico syslog-ng issue gpm libstdc++ openssh-clients fontconfig bzip bzip2 lynx p7zip-standalone rpm-utils unzip terminus-font-console traceroute

# Install kernel and bootloader
mount /proc $DIRG/proc -o bind

echo "$SWAP       swap                    swap    defaults                0 0
$HDDG       /                       $SYSG defaults               0 0" >> $DIRG/etc/fstab

echo "PROBESCSI=yes" >> $DIRG/etc/sysconfig/geninitrd
echo "PROBEIDE=no" >> $DIRG/etc/sysconfig/geninitrd
if [ $HDD_TYPE == "ide" ];
then
 HDD_TYPE="sata"
fi

echo "BASICMODULES=\""`pcidev $HDD_TYPE | awk "{ print $ 2 }"`"\"" >> $DIRG/etc/sysconfig/geninitrd

poldek --root $DIRG -i kernel $BOOTLOADER

if [ "$MBRD" == "auto" ];
then
 disk=`fdisk -l | grep Disk | grep bytes | awk '{ print substr($2, 1, length($2)-1) }' | head -n1`
else
 disk=$MBRD
fi

# Lilo
if [ $BOOTLOADER == "lilo" ]; then
 echo "boot=$disk" > $DIRG/etc/lilo.conf
 echo "read-only" >> $DIRG/etc/lilo.conf
 echo "lba32" >> $DIRG/etc/lilo.conf
 echo "prompt" >> $DIRG/etc/lilo.conf
 echo "timeout=100" >> $DIRG/etc/lilo.conf
 echo " image=/boot/vmlinuz" >> $DIRG/etc/lilo.conf
 echo " label=pld" >> $DIRG/etc/lilo.conf
 echo " root=$HDDG" >> $DIRG/etc/lilo.conf
 echo " initrd=/boot/initrd" >> $DIRG/etc/lilo.conf
 chroot $DIRG /sbin/lilo
fi

# Grub
if [ $BOOTLOADER == "grub" ]; then
 NRPART=$((${HDDG:8:1}-1))
 NRDISK=0
 if [ "$MBRD" != "auto" ];
 then
  for var_disks in `fdisk -l | grep Disk | grep bytes | awk '{ print substr($2, 1, length($2)-1) }'`
  do
   if [ "$disk" == "$var_disks" ];
   then
    break
   else
    NRDISK=$((NRDISK+1))
   fi
  done
 fi
 echo "color yellow/blue blue/cyan" > $DIRG/boot/grub/menu.lst
 echo "timeout 15" >> $DIRG/boot/grub/menu.lst
 echo "default 0" >> $DIRG/boot/grub/menu.lst
 echo "fallback 1" >> $DIRG/boot/grub/menu.lst
 echo "" >> $DIRG/boot/grub/menu.lst
 echo "title PLD Th" >> $DIRG/boot/grub/menu.lst
 echo "root (hd$NRDISK,$NRPART)" >> $DIRG/boot/grub/menu.lst
 echo "kernel /boot/vmlinuz root=$HDDG" >> $DIRG/boot/grub/menu.lst
 echo "initrd /boot/initrd" >> $DIRG/boot/grub/menu.lst
 echo "root (hd$NRDISK,$NRPART)" > $DIRG/grub.conf
 echo "setup (hd$NRDISK)" >> $DIRG/grub.conf
 echo "quit" >> $DIRG/grub.conf
 chroot $DIRG grub --batch < $DIRG/grub.conf
 rm $DIRG/grub.conf
fi

# Grub2
if [ $BOOTLOADER == "grub2" ]; then
 chroot $DIRG grub-mkconfig -o /boot/grub/grub.cfg
 chroot $DIRG grub-install --force --no-floppy $disk
fi

# Root password
chroot $DIRG /usr/bin/passwd

# Copy settings
sed "s/cachedir = \\$DIRG\/var\/cache\/poldek/cachedir = \/var\/cache\/poldek/g" /etc/poldek/poldek.conf > $DIRG/etc/poldek/poldek.conf
if [ "$KEEPDOWNLOAD" == "yes" ];
then
 echo "keep downloads = yes" >> $var_installdir/etc/poldek/poldek.conf
fi
sed "s/HOSTNAME=rescue/HOSTNAME=$HOSTNAME/g" /etc/sysconfig/network > $DIRG/etc/sysconfig/network
sed "s/CONSOLESCREENFONTMAP=lat2u/CONSOLESCREENFONTMAP=/g" $DIRG/etc/sysconfig/console > $DIRG/etc/sysconfig/console.new
sed "s/CONSOLEFONT=lat2u-16/CONSOLEFONT=LatArCyrHeb-16/g" $DIRG/etc/sysconfig/console.new > $DIRG/etc/sysconfig/console
cp /etc/sysconfig/interfaces/ifcfg-* $DIRG/etc/sysconfig/interfaces/
echo "QDISK=sfq" >> $DIRG/etc/sysconfig/interfaces/ifcfg-eth0
cp /etc/resolv.conf $DIRG/etc/resolv.conf
sed "s/CONSOLEMAP=/CONSOLEMAP=8859-2/g" $DIRG/etc/sysconfig/console > $DIRG/etc/sysconfig/console.new
mv $DIRG/etc/sysconfig/console.new $DIRG/etc/sysconfig/console
pcidev net | awk '{print $2}' >> $DIRG/etc/modules
pcidev usb | awk '{print $2}' >> $DIRG/etc/modules
echo "usbhid" >> $DIRG/etc/modules
echo "hid" >> $DIRG/etc/modules
chroot $DIRG localedb-gen
echo "\"\e[1~\": beginning-of-line" >> $DIRG/etc/inputrc
echo "\"\e[4~\": end-of-line" >> $DIRG/etc/inputrc
echo "\"\e[3~\": delete-char" >> $DIRG/etc/inputrc

# End
umount $DIRG/proc
umount $DIRG
echo "Install finish..."
echo "Please reboot komputer"

