#!/bin/bash

## ============================================================ ##
## 			Debian HD2 Buildsystem
## ============================================================ ##
##
## Created by bardzuny from XDA and antonizoon (Lawrence Wu)
## 
## Description
## ===========
## This script creates a Debian Unstable rootfs for the HTC HD2
## Script is best run in Debian Unstable host system
## 
## Required packages (may be incomplete)
## =====================================
## binfmt-support, qemu-arm-static, debootstrap
##
## Usage (run as root)
## ===================
## './createrootfs -r' - creates generic rootfs, without 	##
## majority of customizations
## './createrootfs -p' - customizes rootfs created by 		##
## './createrootfs -r' to suit HTC HD2
##
## To generate full rootfs from scratch, you have to run both
## of commands, as in './createrootfs -r && ./createrootfs -p'
##
## IMPORTANT!!!
## ============
## './createrootfs -p' will ask you which users should be able
## to start Xorg - it's important that you choose 'Anybody',
## or you won't be able to start Xorg as regular user!		##
##
## ============================================================ ##

# Configuration
# =============

# use local configuration file for custom configs
source options.conf

# use a list of packages to install, sed is used to parse comments
packages=`sed -e 's/\#.*//' -e 's/[ ^I]*$$//' -e '/^$$/ d' packages`

# Directory Setup
# ===============

# Use current directory
_CURRENTDIR=`pwd`

# create our working directory
mkdir "$_CURRENTDIR/debhd2-target/"

# Set our working directory
_BUILDDIR="$_CURRENTDIR/debhd2-target"

# Set overlay directory
_OVERLAYDIR="$_CURRENTDIR/rootfs-overlay"

# Set directory of deb packages
_DEBDIR="$_CURRENTDIR/debs"

# Build the Image
# ===============

case $1 in
	# Build unmodified debian system
	# ==============================
	'-r')
		# Create hard disk image
		dd if=/dev/zero of=rootfs.ext4 bs=1MB count=$fssize
		mkfs.ext4 -F rootfs.ext4
		
		# mount the image
		mount rootfs.ext4 $_BUILDDIR
		
		# Build the image with debootstrap
		debootstrap --verbose --arch armel --foreign sid $_BUILDDIR $mirror 
		
		cp /usr/bin/qemu-arm-static $_BUILDDIR/usr/bin/
		
		# Second stage of debootstrap
		chroot $_BUILDDIR /debootstrap/debootstrap --second-stage

		echo '\nrootfs.ext4 was created! Run ./createrootfs -p to add HD2-specific customizations...\n'

		;;
		
	# Make HD2-specific modifications (convert to post-install script?)
	# ==============================
	'-p')
		# mount as chroot
		mount rootfs.ext4 $_BUILDDIR
		
		# set hostname
		echo $hostname > $_BUILDDIR/etc/hostname
		
		# ===============================================
		# Package Setup
		# -------------
		
		# Recommended packages are still abused; we want to go as lightweight as possible

		echo 'APT::Install-Recommends "0";' >> $_BUILDDIR/etc/apt/apt.conf
		echo 'APT::Install-Suggests "0";' >> $_BUILDDIR/etc/apt/apt.conf

		# configure apt and install packages

		echo 'deb '$mirror' unstable main' > $_BUILDDIR/etc/apt/sources.list
		chroot $_BUILDDIR aptitude update
		chroot $_BUILDDIR aptitude install $packages
		chroot $_BUILDDIR aptitude clean
		chroot $_BUILDDIR dpkg-reconfigure locales	
		chroot $_BUILDDIR dpkg-reconfigure x11-common
		chroot $_BUILDDIR /etc/init.d/ssh stop

		# ================================================
		# Kernel Setup
		# ------------
		
		# make sure kernel modules get loaded

		echo 'bcm4329' >> $_BUILDDIR/etc/modules
		echo 'alsa-mix-htc-leo' >> $_BUILDDIR/etc/modules

		# preset network interfaces
		
#		echo -e 'auto eth0\niface eth0 inet dhcp\nwpa-ssid "neostrada_12ac"\nwpa-psk "samochodzikarnia"' >> $_BUILDDIR/etc/network/interfaces
##		echo -e '#!/bin/sh -e\n\nping 10.0.0.1 &\n\nwhile [ 1 ]; do su -c startx '$username'; done\n\nexit 0' > $_BUILDDIR/etc/rc.local
		echo -e '#!/bin/sh -e\n\nwhile [ 1 ]; do su -c startx '$username'; done\n\nexit 0'  > $_BUILDDIR/etc/rc.local

		# =================================================
		# Install Overlay Folders
		# -----------------------
		
		cp -r $_OVERLAYDIR/* $_BUILDDIR
		cp $_DEBDIR/*deb $_BUILDDIR/root/
		chroot $_BUILDDIR bash -c 'dpkg -i /root/*deb'

		# prevent upgrading udev to newer version (it has problems with 2.6.32 kernel that we currently use)

		chroot $_BUILDDIR aptitude hold libudev0 udev
		
		# ================================================
		# Create users
		# ------------

		# set root password

		echo -e "\nPlease enter your desired root password (recommended to set to '$username').\n"
		chroot $_BUILDDIR bash -c 'passwd'
		echo -e '\nRoot password set!\n'

		# create regular user

		echo -e '\nPlease enter data for your regular user '$username' (recommended to set the same as username).\n'
		chroot $_BUILDDIR bash -c 'adduser '$username
		chroot $_BUILDDIR addgroup $username audio
		chroot $_BUILDDIR addgroup $username netdev
		echo -e '\nUser account '$username' created!\n'

		# ================================================
		# clean up
		# --------

		rm $_BUILDDIR/root/*deb
		rm $_BUILDDIR/usr/bin/qemu-arm-static

		echo -e '\nrootfs.ext4 was customized!\n'

		;;
esac

# clean up

umount $_BUILDDIR
rm -r $_BUILDDIR

exit 0
