#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Description: This script is a wrapper program to run create-drbl-live. Here we assign the required packages to create such a live media.

#
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/opt/drbl/}"
. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
. $DRBL_SCRIPT_PATH/conf/drbl-ocs.conf
. $DRBL_SCRIPT_PATH/sbin/ocs-functions

# Settings
# debian_mirror_url_def, debian_mirror_security_url_def, DRBL_REPOSITORY_URL_def and DRBL_GPG_KEY_URL are loaded from drbl-ocs.conf
# Based on Debian sid/lenny/etch...
debian_dist_default="lenny"
# DRBL branch in drbl-core: experimental, unstable, testing, stable
drbl_branch_default="unstable"
# Live branch in drbl-core: experimental, unstable, testing, stable
live_branch_default="experimental"
# Type: xfce, gnome, kde, standard
de_type_default="xfce"

# Common applications for all the version
common_text_app="arj curlftpfs discover1 gpart mdetect boinc-client dnsutils bind9-host syslogd myrescue pmount vim acpi laptop-detect acpi-support hotkey-setup ipmitool"
# Fonts
font_pkgs="ttf-arphic-newsung ttf-kochi-gothic"
# Common applications for the version with X
common_GUI_app="$font_pkgs x-ttcidfont-conf leafpad conky gpicview isomaster hardinfo pcmanfm xarchiver xfburn iceweasel-l10n-es-es iceweasel-l10n-fr iceweasel-l10n-it iceweasel-l10n-ja iceweasel-l10n-zh-cn iceweasel-l10n-zh-tw scim-chewing scim-tables-ja scim-tables-zh im-switch xresprobe grandr wpagui swfdec-mozilla"

# $debian_pkgs_for_gparted is from drbl.conf
pkgs_for_xfce="mlterm mlterm-im-scim xnetcardconfig $common_text_app $common_GUI_app $debian_pkgs_for_gparted"
pkgs_for_gnome="gnome-cups-manager $common_text_app $common_GUI_app $debian_pkgs_for_gparted"
pkgs_for_kde="$common_text_app $common_GUI_app $debian_pkgs_for_gparted"
pkgs_for_standard="$common_text_app"

#
check_if_root
#
prog="$(basename $0)"

#
USAGE() {
   echo "$prog [OPTION]"
   echo "OPTION:"
   echo "-b, --branch [s|stable|t|testing|u|unstable]  specifies the DRBL branch to be used in Live CD. Default is stable."
   echo "-d, --debian-dist [stable|testing|unstable|etch|lenny|sid...]  Assign Debian dist, the default is $DEBIAN_DIST_DEF if not assigned."
   echo "-e, --drbl-live-branch [s|stable|t|testing|u|unstable|e|experimental]  specifies the DRBL live branch to be used in Live CD. Default is stable."
   echo "-g, --drbl-repo-url URL  Assign the DRBL repository URL instead of default one $DRBL_REPOSITORY_URL_def."
   echo "-k, --package FILE  Specify package FILE to be installed in Live CD."
   echo "-i, --assign-version-no NO  Assign the version no as NO instead of date."
   echo "-m, --mirror-url URL  Assign the Debian repository URL instead of default one $debian_mirror_url_def. "
   echo "-n, --live-kernel-pkg KERNEL_VER Assign kernel version as KERNEL_VER (KERNEL VER package must exist in repository. Ex. if KERNEL_VER is 2.6.20-1-486, then linux-image-2.6.20-1-486, squashfs-modules-2.6.20-1-486, and unionfs-modules-2.6.20-1-486 will be used."
   echo "-s, --mirror-security-url URL  Assign the Debian security repository URL instead of default one $debian_mirror_security_url_def."
   echo "-t, --de-type [xfce|gnome|kde|standard]   Specify the type to create DRBL live. Default is xfce"
   echo "Ex: $0 -m xfce -i my-version-1"
}

# Parse command-line options
while [ $# -gt 0 ]; do
  case "$1" in
    -b|--branch)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              drbl_branch="$1"
              shift
            fi
	    [ -z "$drbl_branch" ] && USAGE && exit 1
            ;;
    -d|--debian-dist)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              debian_dist="$1"
              shift
            fi
	    [ -z "$debian_dist" ] && USAGE && exit 1
            ;;
    -e|--drbl-live-branch)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              live_branch="$1"
              shift
            fi
	    [ -z "$live_branch" ] && USAGE && exit 1
            ;;
    -i|--assign-version-no)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              version_no="$1"
              shift
            fi
	    [ -z "$version_no" ] && USAGE && exit 1
            ;;
    -g|--drbl-repo-url)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              DRBL_REPOSITORY_URL="$1"
              shift
            fi
	    [ -z "$DRBL_REPOSITORY_URL" ] && USAGE && exit 1
            ;;
    -k|--package)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              extra_pkgs="$1"
              shift
            fi
	    [ -z "$extra_pkgs" ] && USAGE && exit 1
            ;;
    -m|--mirror-url)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              mirror_url="$1"
              shift
            fi
	    [ -z "$mirror_url" ] && USAGE && exit 1
            ;;
    -s|--mirror-security-url)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              mirror_security_url="$1"
              shift
            fi
	    [ -z "$mirror_security_url" ] && USAGE && exit 1
            ;;
    -n|--live-kernel-pkg)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              live_kernel_ver="$1"
              shift
            fi
	    [ -z "$live_kernel_ver" ] && USAGE && exit 1
            ;;
    -t|--de-type)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              de_type="$1"
              shift
            fi
	    [ -z "$de_type" ] && USAGE && exit 1
            ;;
    -*)     echo "${0}: ${1}: invalid option" >&2
            USAGE >& 2
            exit 2 ;;
    *)      break ;;
  esac
done

#
# Apply default settings if not assigned
[ -z "$debian_dist" ] && debian_dist="$debian_dist_default"
[ -z "$drbl_branch" ] && drbl_branch="$drbl_branch_default"
[ -z "$live_branch" ] && live_branch="$live_branch_default"
[ -z "$de_type" ] && de_type="$de_type_default"
[ -n "$live_kernel_ver" ] && live_kernel_opt="-n $live_kernel_ver"
[ -n "$version_no" ] && ver_no_opt="-i $version_no"
[ -z "$DRBL_REPOSITORY_URL" ] && DRBL_REPOSITORY_URL="$DRBL_REPOSITORY_URL_def"
[ -z "$mirror_url" ] && mirror_url="$debian_mirror_url_def"
[ -z "$mirror_security_url" ] && mirror_security_url="$debian_mirror_security_url_def"

eval pkgs=\$pkgs_for_${de_type}
time create-drbl-live -l en -d $debian_dist -p $de_type -k "$pkgs $extra_pkgs" -b $drbl_branch -g $DRBL_REPOSITORY_URL -m $mirror_url -s $mirror_security_url -e $live_branch $live_kernel_opt $ver_no_opt 
