#!/bin/bash

#
# install Debian chroot environment
# for creating SmartClient netboot initrd

#
# this might get a install script in the future.
# currently it is better to use this file only as documentation
#

# size buildsystem: approx. 360 MB



#
# !!! script has to be run as root !!!
#

#
# initialize envirnonment variables
# NETBOOT_DEVEL_DIR, BUILDSYSTEM_DIR
#
source /etc/smartclient/netboot-devel

# local source in buildsystem
BSSRC=$BUILDSYSTEM_DIR/usr/local/src

# proxy settings, eg.
#export http_proxy=http://proxy:3128
#or
#export http_proxy=http://$USER:$PASSWORD@proxy:3128


# Debian Sarge,   20041231, debootstrap 0.2.45-0.2
# Debian Etch, 	  20070814, debootstrap 0.3.3.2
# Debian Squeeze, 20120522, debootstrap 

# Version of debootstrap
DEBIAN_RELEASE=stable
#ARCH=i386
ARCH=amd64

BB_VERSION=1.6.1


#
# debootstrap installation
#

# zu finden über http://software.opensuse.org,
# derzeit im Repository devel:tools.

# Alternativ:
# get the debootstrap package 
# from http://packages.debian.org/stable/admin/debootstrap
# and install it with the following command
# (for info see http://debiananwenderhandbuch.de/debootstrap.html)
#DBSTRVER=0.3.3.2
#cd /
#ar p debootstrap_${DBSTRVER}_all.deb data.tar.gz | tar -xvzf -

##
## install Debian chroot environment
##


# install Debian sarge system
unset LC_CTYPE
unset LANG

# start installation
debootstrap --arch $ARCH $DEBIAN_RELEASE $BUILDSYSTEM_DIR/

# installing additional packages
rsync -av $NETBOOT_DEVEL_DIR/debian-setup/etc $BUILDSYSTEM_DIR/.
chroot $BUILDSYSTEM_DIR/
alias l="ls -la"

#apt-setup
apt-get update

# apt-cache search XYZ

# already install: mkfs.ext2 tune2fs resize2fs
PKGS="busybox mklibs curl dhcpcd dialog dropbear dpkg-dev rsync ldap-utils parted pciutils make makedev libc6-dev libstdc++6-4.7-pic libncurses5-dev perl hwinfo strace less vim patch kexec-tools mklibs make bzip2 util-linux"
# apt-build


# conflict: initrd-tools 
# old ldapmodify: libssl0.9.6. but doesn't work, because other dependencies
# conflict: dhcp-client, dhcp3-client (with udhcpc dhcpcd). 
# udhcpc part of busybox ;-)
# to use libc6-pic mklibs must be patched, otherwise
#   undefined symbol: _nss_files_parse_pwent in libnss_files and libc6
apt-get install $PKGS


#
# apt-build
#
# apt-get install apt-build dh-make
# 
# apt-get source busybox
# cd busybox*
# ? dpkg-buildpackage ?
# oder:
# make menuconfig
# make


#
# local time zone
#
cp /usr/share/zoneinfo/Europe/Berlin /etc/localtime



#####################################
# 
# exit chroot
#


#
# mklibs patch (honor libnss libraries)
#
patch -b -p0 < debian-setup/mklibs.patch



##
## busybox
##
cd
apt-get source busybox

# or

## busybox 1.00 is not part of Debian (20041230)
#cd $BSSRC
#tar -xvjf $NETBOOT_DEVEL_DIR/busybox/busybox-$BB_VERSION.tar.bz2
#cp -a $NETBOOT_DEVEL_DIR/busybox/.config $BSSRC/busybox-$BB_VERSION

# save config as ../busybox.config
# and copy it to subversion

## chroot
#chroot $BUILDSYSTEM_DIR/
#cd /usr/local/src/busybox-$BB_VERSION
##make defconfig
##make menuconfig
#make

#
# exit chroot
#

cp -a busybox-$BB_VERSION/busybox $NETBOOT_DEVEL_DIR/initrd/bin



##
## kexec-tools (if used)
##

# tar -xvzf $NETBOOT_DEVEL_DIR/kexec/kexec-tools-1.98.tgz
# # chroot
# cd kexec-tools-1.98
# make
# # exit chroot
# cp -a kexec-tools-1.98/objdir/build/sbin/kexec $SVNDIR/initrd/sbin



# getldaputl (curl?): altes getldapurl funktioniert noch :-)

###############################################

##
## kernel
##

# not required, if precompiled kernel RPM is used

# # download from http://www.kernel.org to $NETBOOT_DEVEL_DIR/kernel
# # vanilla kernel
# # and mm patches
# # (rsync possible :-)
# 
# KVER=2.6.15.2
# KEXTRA=dbk-nb
# 
# cd /usr/src/
# tar -xjf $NETBOOT_DEVEL_DIR/kernel/linux-$KVER.tar.bz2
# mv linux-$KVER linux-$KVER-$KEXTRA
# 
# cd linux-$KVER-$KEXTRA
# cp $NETBOOT_DEVEL_DIR/kernel/config-$KVER ./.config
# 
# # configure the kernel
# make menuconfig
# # alternativly: make xconfig
# #enable KEXEC if used
# 
# # make help
# 
# # build the kernel 
# make

###############################################

#
# repair system
#
dkpg --root /var/lib/smartclient/netboot-devel/buildsystem_initrd /tmp/libc6_2.3.6.ds1-13etch2_i386.deb
dpkg --root /var/lib/smartclient/netboot-devel/buildsystem_initrd --vextract /tmp/libc6_2.3.6.ds1-13etch2_i386.deb /var/lib/smartclient/netboot-devel/buildsystem_initrd

ldconfig -r /var/lib/smartclient/netboot-devel/buildsystem_initrd

