--- /dev/null
+#!/bin/bash
+
+# exit script if we try to use an uninitialised variable
+set -u
+
+# exit the script if any statement returns a non-true return value
+set -e
+
+# be verbose
+#set -x
+
+#########################################
+# base variables
+
+VERSION="0.2.0"
+
+DEBUG="n"
+VERBOSE="n"
+QUIET="n"
+YES="n"
+SIMULATE="n"
+
+# console colors:
+RED=""
+YELLOW=""
+GREEN=""
+BLUE=""
+NORMAL=""
+
+HAS_TTY='y'
+
+BASENAME="$(basename ${0})"
+BIN_DIR="$(dirname ${0})"
+BASE_DIR=$( readlink -f "${BIN_DIR}/.." )
+ROOT_DIR="${BASE_DIR}/image"
+CDROM_ROOT_DIR="${ROOT_DIR}/cdrom"
+TGT_DIR="${BASE_DIR}/target"
+GRML2USB="${BASE_DIR}/grml2usb/grml2usb"
+
+SRC_ISO_IMAGE=
+SRC_IMAGE_DEV=
+SRC_IMAGE_MOUNTPOINT=
+SRC64_SQUASHFS_FILE=
+TGT_ISO_IMAGE=
+
+BOOT_ARGS="-no-emul-boot -boot-load-size 4 -boot-info-table -b boot/isolinux/isolinux.bin -c boot/isolinux/boot.cat -input-charset utf-8"
+
+#-------------------------------------------------------------------
+detect_color() {
+
+ local safe_term="${TERM//[^[:alnum:]]/?}"
+ local match_lhs=""
+ local use_color="false"
+ [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
+ [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
+ [[ -z ${match_lhs} ]] \
+ && type -P dircolors >/dev/null \
+ && match_lhs=$(dircolors --print-database)
+ [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color="true"
+
+ # console colors:
+ if [ "${use_color}" = "true" ] ; then
+ RED="\033[38;5;196m"
+ YELLOW="\033[38;5;226m"
+ GREEN="\033[38;5;46m"
+ BLUE="\033[38;5;27m"
+ NORMAL="\033[39m"
+ else
+ RED=""
+ YELLOW=""
+ GREEN=""
+ BLUE=""
+ NORMAL=""
+ fi
+
+ local my_tty=$(tty)
+ if [[ "${my_tty}" =~ 'not a tty' ]] ; then
+ my_tty='-'
+ fi
+
+ if [[ "${my_tty}" = '-' || "${safe_term}" = "dump" ]] ; then
+ HAS_TTY='n'
+ fi
+
+}
+detect_color
+
+#########################################
+# Some often used funktions
+
+empty_line() {
+ if [[ "${QUIET}" != "y" ]] ; then
+ echo
+ fi
+}
+
+#------------------------------------------------------------------------------
+my_date() {
+ date +'%F %T.%N %:::z'
+}
+
+#------------------------------------------------------------------------------
+debug() {
+ if [[ "${VERBOSE}" != "y" ]] ; then
+ return 0
+ fi
+ echo -e " * [$(my_date)] [${BASENAME}:DEBUG]: $@" >&2
+}
+
+#------------------------------------------------------------------------------
+info() {
+ echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@" >&2
+}
+
+#------------------------------------------------------------------------------
+warn() {
+ echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2
+}
+
+#------------------------------------------------------------------------------
+error() {
+ echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2
+}
+
+#------------------------------------------------------------------------------
+RM() {
+ if [[ "${VERBOSE}" == "y" ]] ; then
+ rm --verbose "$@"
+ else
+ rm "$@"
+ fi
+
+}
+
+#------------------------------------------------------------------------------
+CP() {
+ if [[ "${VERBOSE}" = "y" ]] ; then
+ cp --verbose "$@"
+ else
+ cp "$@"
+ fi
+}
+
+#------------------------------------------------------------------------------
+MV() {
+ if [[ "${VERBOSE}" = "y" ]] ; then
+ mv --verbose "$@"
+ else
+ mv "$@"
+ fi
+}
+
+#------------------------------------------------------------------------------
+LN() {
+ if [[ "${VERBOSE}" = "y" ]] ; then
+ ln --verbose "$@"
+ else
+ ln "$@"
+ fi
+}
+
+#------------------------------------------------------------------------------
+CHMOD() {
+ if [[ "${VERBOSE}" = "y" ]] ; then
+ chmod --verbose "$@"
+ else
+ chmod "$@"
+ fi
+}
+
+#------------------------------------------------------------------------------
+CHOWN() {
+ if [[ "${VERBOSE}" = "y" ]] ; then
+ chown --verbose "$@"
+ else
+ chown "$@"
+ fi
+}
+
+#------------------------------------------------------------------------------
+MKDIR() {
+ if [[ "${VERBOSE}" = "y" ]] ; then
+ mkdir --verbose "$@"
+ else
+ mkdir "$@"
+ fi
+}
+
+#------------------------------------------------------------------------------
+CHROOT() {
+ chroot "${ROOT_DIR}" "$@"
+}
+
+#------------------------------------------------------------------------------
+description() {
+ echo -e $( cat <<-EOF
+ Dieses Script generiert ein individuell angepaßtes GRML-ISO-Image,
+ in welchem weitere nützliche Software-Pakete enthalten sind.
+ Nach erfolgreicher Generierung befindet sich das ISO-Image
+ unter '${TGT_DIR}'.
+
+ EOF
+ )
+}
+
+#------------------------------------------------------------------------------
+usage() {
+ cat <<-EOF
+ Usage: ${BASENAME} [-d|--debug] [-v|--verbose] [-t|--test] [--nocolor] <SOURCE_ISO_IMAGE>
+ ${BASENAME} -h|--help
+ ${BASENAME} -V|--version
+
+ $( description )
+
+ Optionen:
+ -d|--debug Debug Ausgabe (sh -x)
+ -v|--verbose Erhöht das Verbose-Niveau.
+ -t|--test Test-/Simulationsmodus, nichts wird wirklich gemacht.
+ --nocolor Keine farblichen Darstellungen.
+ -h|--help Zeigt diese Ausgabe und beendet sich.
+ -V|--version Zeigt die Versionsnummer des Scripts und beendet sich.
+ EOF
+}
+
+#------------------------------------------------------------------------------
+get_options() {
+
+ local tmp=
+ local base_dir=
+
+ set +e
+ tmp=$( getopt -o dvthV \
+ --long debug,verbose,test,nocolor,help,version \
+ -n "${BASENAME}" -- "$@" )
+ if [[ $? != 0 ]] ; then
+ echo "" >&2
+ usage >&2
+ exit 1
+ fi
+ set -e
+
+ # Note the quotes around `$TEMP': they are essential!
+ eval set -- "${tmp}"
+
+ local p=
+
+ while true ; do
+ case "$1" in
+ -d|--debug)
+ DEBUG="y"
+ shift
+ ;;
+ -v|--verbose)
+ VERBOSE="y"
+ shift
+ ;;
+ -t|--test)
+ SIMULATE="y"
+ shift
+ ;;
+ --nocolor)
+ RED=""
+ YELLOW=""
+ GREEN=""
+ BLUE=""
+ NORMAL=""
+ shift
+ ;;
+ -h|--help)
+ usage
+ exit 0
+ ;;
+ -V|--version)
+ echo "${BASENAME} version: ${VERSION}"
+ exit 0
+ ;;
+ --) shift
+ break
+ ;;
+ *) echo "Internal error!"
+ exit 1
+ ;;
+ esac
+ done
+
+ if [[ "${DEBUG}" = "y" ]] ; then
+ set -x
+ fi
+
+ if [[ "$#" -ne 1 ]] ; then
+ empty_line
+ error "Das Ausgangs-(Original-)ISO-Image von GRML muss angegeben werden."
+ empty_line
+ usage >&2
+ exit 1
+ fi
+
+ SRC_ISO_IMAGE="${1}"
+
+}
+
+#----------------------------------------------------------
+destroy_src_img_dev() {
+
+ if [[ -n "${SRC_IMAGE_DEV}" ]] ; then
+ empty_line
+ info "Detache Loopback Device für '${SRC_ISO_IMAGE}' ..."
+ losetup -d "${SRC_IMAGE_DEV}"
+ SRC_IMAGE_DEV=
+ fi
+
+}
+
+#----------------------------------------------------------
+unmount_src_image() {
+
+ if [[ -n "${SRC_IMAGE_MOUNTPOINT}" && -d "${SRC_IMAGE_MOUNTPOINT}" ]] ; then
+ info "Unmounting '${GREEN}${SRC_IMAGE_MOUNTPOINT}${NORMAL}' ..."
+ umount "${SRC_IMAGE_MOUNTPOINT}" || true
+ info "Removing '${GREEN}${SRC_IMAGE_MOUNTPOINT}${NORMAL}' ..."
+ RM -r "${SRC_IMAGE_MOUNTPOINT}"
+ SRC_IMAGE_MOUNTPOINT=
+ fi
+
+}
+
+#----------------------------------------------------------
+mount_src_image() {
+
+ empty_line
+ SRC_IMAGE_MOUNTPOINT=$( mktemp --directory )
+ info "Mounte '${SRC_IMAGE_DEV}' nach '${GREEN}${SRC_IMAGE_MOUNTPOINT}${NORMAL}' ..."
+ mount -o ro "${SRC_IMAGE_DEV}" "${SRC_IMAGE_MOUNTPOINT}"
+
+ SRC64_SQUASHFS_FILE="${SRC_IMAGE_MOUNTPOINT}/live/grml64-full/grml64-full.squashfs"
+
+ trap "unmount_src_image; destroy_src_img_dev; exit 9" EXIT INT QUIT TERM
+
+ if [[ ! -f "${SRC64_SQUASHFS_FILE}" ]] ; then
+ error "Datei '${RED}${SRC64_SQUASHFS_FILE}${NORMAL}' nicht gefunden."
+ exit 3
+ fi
+
+ debug "Datei '${SRC64_SQUASHFS_FILE}${NORMAL}':\n$(ls -l "${SRC64_SQUASHFS_FILE}")"
+
+}
+
+#----------------------------------------------------------
+create_src_img_dev() {
+
+ empty_line
+ info "Erstelle Loopback-device für '${SRC_ISO_IMAGE}' ..."
+ SRC_IMAGE_DEV=$( losetup --find --show "${SRC_ISO_IMAGE}" )
+ info "Loopback Device für '${SRC_ISO_IMAGE}' ist jetzt '${GREEN}${SRC_IMAGE_DEV}${NORMAL}'."
+
+ trap "destroy_src_img_dev; exit 9" EXIT INT QUIT TERM
+
+}
+
+#------------------------------------------------------------------------------
+unpack_orig() {
+ empty_line
+ if [[ -d "${TGT_DIR}" ]] ; then
+ info "Lösche ${TGT_DIR} ..."
+ RM -rf "${TGT_DIR}"
+ fi
+ info "Unsquashing '${SRC64_SQUASHFS_FILE}' ..."
+ local np=""
+ if [[ "${VERBOSE}" != "y" ]] ; then
+ np="-no-progress"
+ fi
+ unsquashfs ${np} -d "${TGT_DIR}" "${SRC64_SQUASHFS_FILE}"
+ debug "Unsquashing beendet."
+}
+
+#------------------------------------------------------------------------------
+init_grml2usb() {
+
+ empty_line
+ if [[ -d "${CDROM_ROOT_DIR}" ]] ; then
+ info "Lösche ${CDROM_ROOT_DIR} ..."
+ RM -rf "${CDROM_ROOT_DIR}"
+ fi
+ MKDIR "${CDROM_ROOT_DIR}"
+
+ info "Bereite ISO-Image in '${CDROM_ROOT_DIR}' vor ..."
+ local verb_flag=""
+ if [[ "${VERBOSE}" == "y" ]] ; then
+ verb_flag="--verbose"
+ fi
+
+ local cmd="\"${GRML2USB}\" ${verb_flag} \"${SRC_ISO_IMAGE}\" \"${CDROM_ROOT_DIR}\""
+ debug "Ausführen:\n${cmd}"
+ eval ${cmd}
+
+}
+
+#------------------------------------------------------------------------------
+make_isofs() {
+
+ empty_line
+ local iso_file="${ROOT_DIR}/grml64.$( date +'%Y-%m-%d_%H-%M-%S' ).iso"
+
+ cd "${CDROM_ROOT_DIR}"
+ info "Bereite mkisofs vor ,,,"
+ # move syslinux to isolinux
+ MV boot/syslinux boot/isolinux
+ echo "menu label ^Isolinux prompt" > boot/isolinux/promptname.cfg
+ echo "include hd.cfg" >> boot/isolinux/grmlmain.cfg
+
+
+ # adjust files from overlay directory
+ for GRML_VERSION_FILE in $(find . -name grml-version) ; do
+ GRML_NAME=$(awk '{print $1}' "$GRML_VERSION_FILE")
+ VERSION=$(awk '{print $2}' "$GRML_VERSION_FILE")
+ RELEASENAME=$(sed 's/.*- \(.*\).*\[.*/\1/' "$GRML_VERSION_FILE")
+ DATE=$(sed 's/.*\[\(.*\)].*/\1/' "$GRML_VERSION_FILE")
+ SHORT_NAME="$(echo $GRML_NAME | tr -d ',./;\- ')"
+ RELEASE_INFO="$GRML_NAME $VERSION - $RELEASENAME"
+ BOOTID=$(cat conf/bootid.txt)
+
+ for param in GRML_NAME VERSION RELEASENAME DATE SHORT_NAME RELEASE_INFO BOOTID ; do
+
+ value="$(eval echo '$'"$param")"
+ # adjust config files
+ for file in ./boot/isolinux/*.cfg ./boot/isolinux/*.msg ./boot/grub/*.cfg ; do
+ sed -i "s/%$param%/$value/g" ${file} 2>/dev/null || true
+ done
+ done
+ done
+
+ empty_line
+ info "Erstelle ISO-Datei '${GREEN}${iso_file}${NORMAL}' ..."
+ local cmd="mkisofs -V 'grml-multiboot' -l -r -J -no-pad ${BOOT_ARGS} -o \"${iso_file}\" ."
+ debug "Ausführen:\n${cmd}"
+ eval ${cmd}
+
+}
+
+#----------------------------------------------------------
+# Main:
+
+main() {
+
+ get_options "$@"
+
+ if [[ ! -f "${SRC_ISO_IMAGE}" ]] ; then
+ empty_line
+ error "Die Datei '${RED}${SRC_ISO_IMAGE}${NORMAL}' existiert nicht oder ist keine reguläre Datei."
+ empty_line
+ usage >&2
+ exit 1
+ fi
+
+ if [[ "${EUID}" != "0" ]] ; then
+ echo "Du benötigst root-Rechte, um dieses Script auszuführen." >&2
+ empty_line
+ exit 1
+ fi
+
+ create_src_img_dev
+ mount_src_image
+ unpack_orig
+
+ trap - EXIT INT QUIT TERM
+ unmount_src_image
+ destroy_src_img_dev
+
+ init_grml2usb
+ make_isofs
+
+ empty_line
+ info "Fertig!"
+ empty_line
+
+}
+
+main "$@"
+
+exit 0
+
+# vim: ts=4 list