#!/bin/sh

################################################################################
#      Copyright (C) 2009-2010 OpenELEC.tv
#      http://www.openelec.tv
#
#  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, 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 OpenELEC.tv; see the file COPYING.  If not, write to
#  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

# usage:   sudo ./create_installstick <drive>
# example: sudo ./create_installstick /dev/sdb

if [ "$(id -u)" != "0" ]; then
  clear
  echo "#########################################################"
  echo "# please execute with 'sudo' or -DANGEROUS!!!- as root  #"
  echo "# example: sudo ./create_installstick <drive>           #"
  echo "#########################################################"
  exit 1
fi

if [ -z "$1" ]; then
  clear
  echo "#########################################################"
  echo "# please execute with your drive as option              #"
  echo "# example: sudo ./create_installstick /dev/sdb          #"
  echo "#########################################################"
  exit 1
fi

DISK="$1"
PART="${DISK}1"

clear
echo "#########################################################"
echo "#                                                       #"
echo "#             OpenELEC.tv USB Installer                 #"
echo "#                                                       #"
echo "#########################################################"
echo "#                                                       #"
echo "#     This will wipe any data off your chosen drive     #"
echo "# Please read the instructions and use very carefully.. #"
echo "#                                                       #"
echo "#########################################################"

# check MD5 sums
  echo "checking MD5 sum..."
  md5sum -c target/KERNEL.md5
  MD5_ERROR="$?"
  md5sum -c target/SYSTEM.md5
  MD5_ERROR="$?"

if [ "$MD5_ERROR" = "1" ]; then
  echo "#########################################################"
  echo "#                                                       #"
  echo "# OpenELEC.tv failed md5 check - Installation will quit #"
  echo "#                                                       #"
  echo "#      Your original download was probably corrupt.     #"
  echo "#   Please visit www.openelec.tv and get another copy   #"
  echo "#                                                       #"
  echo "#########################################################"
  exit 1
fi

# (TODO) umount anything
  umount "$PART"

# remove any partition on your drive
  echo "writing new disklabel on $DISK (remove any partition)..."
  dd if=/dev/zero of="$DISK" bs=4096 count=1024
  parted -s "$DISK" mklabel msdos

# create one partition
  echo "create an partition on $DISK..."
  parted -s "$DISK" unit cyl mkpart primary fat32 -- 0 -0

# make partition active (bootable)
  echo "make partition active..."
  parted -s "$DISK" set 1 boot on

# tell kernel we have a new partitiontable
  echo "tell kernel we have a new partitiontable..."
  partprobe "$DISK"

# make filesystem
  echo "make filesystem on $PART..."
  mkfs.vfat "$PART" -n INSTALL

# install syslinux
  echo "install syslinux to $PART..."
  syslinux -f "$PART"

# mount partition
  echo "mounting partition $PART to /tmp/usb_install..."
  mkdir -p /tmp/usb_install
  mount "$PART" /tmp/usb_install

# copy files
  echo "copy files to $PART..."
  cp target/KERNEL /tmp/usb_install
  cp target/KERNEL.md5 /tmp/usb_install
  cp target/SYSTEM /tmp/usb_install
  cp target/SYSTEM.md5 /tmp/usb_install
  cp sample.conf/syslinux_installer.cfg /tmp/usb_install/syslinux.cfg

# syncing disk
  echo "syncing disk..."
  sync

# unmount partition
  echo "unmounting partition $PART..."
  umount "$PART"

# cleaning
  echo "cleaning tempdir..."
  rmdir /tmp/usb_install

echo "...ready"
