#!/bin/bash
#
# Copyright (c) 2014 Christoph Volkert (http://c3vo.de)
#
# 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.
#
# parts used from http://wiki.ubuntuusers.de/LiveCD_manuell_remastern
# for more information (in German) see  
#

wget -nc http://old-releases.ubuntu.com/releases/precise/ubuntu-12.04.1-desktop-amd64.iso
mount -o loop ubuntu-12.04.1-desktop-amd64.iso /mnt
mkdir -p work/{livecd,old,new}
chmod -R u+w work/livecd
cd work
cp -a /mnt/. livecd/
mount -t squashfs -o loop,ro /mnt/casper/filesystem.squashfs old
dd if=/dev/zero of=ubuntu-fs.ext2 bs=1M count=5120
mke2fs -F ubuntu-fs.ext2
mount -o loop,rw ubuntu-fs.ext2 new
rm -rf new/lost+found
cp -a old/. new/
umount old
umount /mnt
mount -t proc -o bind /proc new/proc
mount -o bind /dev/pts new/dev/pts
chroot new dpkg-divert --local --rename --add /sbin/initctl
chroot new ln -s /bin/true /sbin/initctl

echo "
Autostart
"

ubuntu_home=./new/home/ubuntu

mkdir -p ${ubuntu_home}/.config/autostart/

cat << "__EOF__" >> ${ubuntu_home}/.config/autostart/numlockx_on.desktop
[Desktop Entry]
Name=Ziffernblock anschalten
Type=Application
Exec=numlockx on
Terminal=false
NoDisplay=True
__EOF__


cat << "__EOF__" >> ${ubuntu_home}/.config/autostart/firefox_start.desktop
[Desktop Entry]
Name=Firefox Onlinebanking
Type=Application
Exec=firefox http://www.example.org/bank
Terminal=false
NoDisplay=True
__EOF__

chown -R 999:999 ${ubuntu_home}

#echo "
#Networking (WLAN)
#"
#
#mkdir -p ./new/etc/NetworkManager/system-connections
#
#NetworkSource=/etc/NetworkManager/system-connections/WLAN-XXXX
#
#cp ${NetworkSource} ./new${NetworkSource}
#chown root ./new${NetworkSource}
#chgrp root ./new${NetworkSource}
#chmod 400 ./new${NetworkSource}

cat << "__EOF__" > new/etc/apt/sources.list
deb http://de.archive.ubuntu.com/ubuntu precise main restricted universe multiverse
#deb-src http://de.archive.ubuntu.com/ubuntu precise main restricted universe multiverse

deb http://de.archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse
#deb-src http://de.archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse

deb http://de.archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse
#deb-src http://de.archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse

#deb http://de.archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse
#deb-src http://de.archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse

## This software is not part of Ubuntu, but is offered by third-party
## developers who want to ship their latest software.
# deb http://extras.ubuntu.com/ubuntu precise main
__EOF__

chroot new apt-get update
echo "
Remove unneeded programs
"

chroot new apt-get -y --purge autoremove rhythmbox* gwibber* telepathy* ubuntuone* usb-creator-common transmission-* ubiquity* simple-scan software-center unity-scope-* unity-lens-music unity-lens-video thunderbird* xdiagnose deja-dup gparted gnome-disk-utility

echo "
Update Kernel
"

chroot new apt-get remove -y linux-generic* linux-image* linux-headers*
chroot new apt-get install -y linux-image-generic linux-headers-generic linux-image-generic
chroot new apt-get -y install chromium-browser numlockx language-pack-de language-pack-gnome-de
chroot new apt-get dist-upgrade -y
chroot new apt-get clean

chroot new rm /sbin/initctl
chroot new dpkg-divert --local --rename --remove /sbin/initctl
cp new/boot/vmlinuz* livecd/casper/vmlinuz
cp new/boot/initrd.img* livecd/casper/initrd.lz
chroot new dpkg-query -W --showformat="${Package} ${Version}\n" > livecd/casper/filesystem.manifest 
dd if=/dev/zero of=new/unused_space
rm new/unused_space
rm livecd/wubi.exe
rm livecd/casper/filesystem.squashfs

echo "
Change locales to German
"
echo "de" > livecd/isolinux/lang

umount new/proc
umount new/dev/pts
mksquashfs new/. livecd/casper/filesystem.squashfs -comp xz
umount new
find livecd -type f -print0 |xargs -0 md5sum | tee livecd/md5sum.txt
genisoimage -o ownbanking-ubuntu-12.04-desktop-amd64.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -r -V "own Banking Ubuntu 12.04 Remix" -cache-inodes -J -l livecd

