#!/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
#    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 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 script generates a Trisquel live usb drive
    Pass the device name of the disk and the iso image as parameters.
    This script will ERASE all data in the usb drive!!

Usage: $0 /dev/sd[a-z] image.iso"
exit 1
fi

if ! [ -b $1 ]
then
echo "Device not found."
exit 1
fi

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

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

mkdir $TMPDIR/iso
mkdir $TMPDIR/target

mount -o loop $ISO $TMPDIR/iso
umount ${DEVICE}*

# Let's blank the first MB, just in case
dd if=/dev/zero of=${DEVICE} bs=1M count=1

# Create two partitions
sfdisk ${DEVICE} << EOF
0 900
;
EOF

# Make the first partition bootable
sfdisk -A1 ${DEVICE}

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

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

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

# Copy the boot files
# TODO: it would be cleaner to store all this files in a directory,
# but the extlinux gfxboot piece fails to load the translations.
cp $TMPDIR/iso/isolinux/* $TMPDIR/target
rm $TMPDIR/target/isolinux.cfg
mv $TMPDIR/target/isolinux.txt $TMPDIR/target/extlinux.txt

# Install syslinux mbr
dd if=/usr/lib/syslinux/mbr.bin of=${DEVICE}

# Instal extlinux
extlinux --install ${TMPDIR}/target

# Create the extlinux config file
cat << EOF > ${TMPDIR}/target/extlinux.conf
DEFAULT trisquel
GFXBOOT bootlogo
LABEL trisquel
  menu label ^Try Trisquel without any change to your computer
  kernel vmlinuz
  append initrd=initrd boot=casper splash quiet noprompt cdrom-detect/try-usb=true persistent --
LABEL instalar
  menu label ^Install Trisquel
  kernel vmlinuz
  append initrd=initrd boot=casper only-ubiquity splash quiet noprompt cdrom-detect/try-usb=true persistent --
LABEL safe
  menu label Safe graphics mode
  kernel vmlinuz
  append initrd=initrd boot=casper acpi=off noacpi video=vesa nosplash noprompt cdrom-detect/try-usb=true persistent --
LABEL memtest
  menu label Test ^memory
  kernel mt86plus
DISPLAY isolinux.txt
TIMEOUT 300
PROMPT 1
EOF

# Copy the requiered files from the CD image
cp ${TMPDIR}/iso/.disk ${TMPDIR}/iso/fsf ${TMPDIR}/iso/casper ${TMPDIR}/iso/README.TXT ${TMPDIR}/iso/preseed -a ${TMPDIR}/target

sync
umount ${TMPDIR}/target
umount ${TMPDIR}/iso
rm ${TMPDIR} -rf
