#!/bin/sh
#
#    Copyright (C) 2009  Rubén Rodríguez <ruben@gnu.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
#    -------------------------------------------------------------------------
#
#    This script generates a Trisquel live usb drive
#    You must run it inside a live Trisquel 3.0 session
#    Pass the device name of the disk as the only parameter
#    It will ERASE all data in the usb drive!!
#

if [ $# -eq 0 ]
then
echo "Trisquel Live USB creator script.

    Copyright (C) 2009 Rubén Rodríguez <ruben@gnu.org>
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    This script generates a Trisquel live usb drive
    You must run it inside a live Trisquel 3.0 session
    Pass the device name of the disk as the only parameter
    It will ERASE all data in the usb drive!!

Usage: $0 image.iso"
exit 1
fi

if ! [ -f $1 ]
then
echo "Iso image not found."
exit 1
fi

DEVICE=$1
TMPDIR=$(mktemp -d)

umount /dev/${DEVICE}*

# Let's blank the device, just in case
dd if=/dev/zero of=/dev/${DEVICE}

# Create two partitions
sfdisk /dev/${DEVICE} << EOF
0 800
;
EOF

# GNOME will remount the disk, let's umount it
sleep 5
umount /dev/${DEVICE}*

# Format the partitions
mkfs.ext2 -F /dev/${DEVICE}1 -L trisquel-live
mkfs.ext4 -F /dev/${DEVICE}2 -L home-rw

# Mount the first one
mount /dev/${DEVICE}1 ${TMPDIR}

# Install grub
grub-install --root-directory=${TMPDIR} /dev/${DEVICE}

# Copy the boot image
cp /boot/boot/message* ${TMPDIR}/boot/message

# Copy the kernel and initrd images
cp /cdrom/isolinux/vmlinuz* ${TMPDIR}/boot/vmlinuz
cp /cdrom/isolinux/initrd* ${TMPDIR}/boot/initrd

# Create the grub config file
cat << EOF > ${TMPDIR}/boot/grub/menu.lst
default		0
timeout		15
gfxmenu /boot/message

title		^Try Trisquel 3.0 without any change to your computer
kernel		/boot/vmlinuz noprompt cdrom-detect/try-usb=true persistent boot=casper splash quiet --
initrd		/boot/initrd

title		^Install Trisquel 3.0
kernel          /boot/vmlinuz noprompt cdrom-detect/try-usb=true persistent boot=casper only-ubiquity splash quiet --
initrd          /boot/initrd

title		Safe graphics mode, acpi off
kernel          /boot/vmlinuz noprompt cdrom-detect/try-usb=true persistent boot=casper splash quiet video=vesa noacpi acpi=off --
initrd          /boot/initrd

title		^Check disk for defects
kernel          /boot/vmlinuz noprompt boot=casper splash integrity-check
initrd          /boot/initrd
EOF

# Copy the requiered files from the CD image
cp /cdrom/.disk /cdrom/casper /cdrom/README.TXT /cdrom/md5sum.txt /cdrom/preseed -a ${TMPDIR}

sync
umount ${TMPDIR}
rmdir ${TMPDIR}
