#!/bin/sh
# mkemovix - Generate an eMoviX ISO image from a directory full of videos.
#
# 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.
#
# Copyright 2004-2005 David L Norris <dave@webaugur.com>
#
# 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

if [ ! -d "$1" ]; then
    echo "usage: $0 </path/to/directory>";
    exit
fi

ROOTDIR="$1"
TEMPDIR="/tmp"
TEMPDIR="/Library/Temp"

MKISOFS="/usr/bin/mkisofs"
MOVIXOPTS=""
MOVIXDIR="/usr/share/emovix"

TMPDIR=`mktemp -d "${TEMPDIR}/emovixiso.XXXXXXXXXX"`  || zenity --error --text "Unable to Create Temporary Directory"
if [ ! -d "$TMPDIR" ]; then exit; fi

TMPFILE=`mktemp "${TMPDIR}/movix.list.XXXXXXXXXX"` || zenity --error --text "Unable to Create Playlist"
if [ ! -f "$TMPFILE" ]; then exit; fi

echo file:$TMPFILE
echo dir:$TMPDIR

DIRNAME=`dirname "${ROOTDIR}"`
BASENAME=`basename "${ROOTDIR}"`
ROOTDIR="${DIRNAME}/${BASENAME}"

# Field seperation on newline only
OLDIFS=$IFS
IFS="
"
    # Find all the video files
    for i in `find ${ROOTDIR} -type f | sort`;
    do
       # If the file is not really a video don't bother
       ISVIDEO=1
       file -bi "${i}" | grep ^video > /dev/null
       ISVIDEO=$?
       if [ $ISVIDEO != 0 ]; then
#           echo "-NonVideo: ${i}" 1>&2
           continue;
       fi

       # Copy Video File to CD
       cp -f "${i}" "${TMPDIR}"
       ERROR=$?
       if [ $ERROR != 0 ]; then
           zenity --error --text "Unable to copy ${i}"
           exit 1
       fi
       # Write Video to Playlist
       echo "${i}" | sed -e "s#${ROOTDIR}#/cdrom#g" >> "${TMPFILE}"

    done

if [ -f "${TMPFILE}" ]; then
    mv "${TMPFILE}" "${TMPDIR}/movix.list"
else
    zenity --error --text "Unable to Create Playlist"
    rm -f "${TMPFILE}"
fi

# Copy directories to ISO
cp -af "${MOVIXDIR}/movix" "${TMPDIR}/"
cp -af "${MOVIXDIR}/isolinux" "${TMPDIR}/"
mkdir -p "${TMPDIR}/mplayer/codecs"
cp -af "${MOVIXDIR}/codecs" "${TMPDIR}/mplayer/"

# Copy files to ISO
cp -af "${MOVIXDIR}/mplayer-fonts/Vera.ttf" "${TMPDIR}/mplayer/subfont.ttf"
cp -af "${MOVIXDIR}/keyboard-i18n/us/shellkey.map" "${TMPDIR}/movix/"
cp -af "${MOVIXDIR}/keyboard-i18n/us/bootkey.map" "${TMPDIR}/isolinux/"
cp -af "${MOVIXDIR}/boot-messages/en/*.txt" "${TMPDIR}/isolinux/"

# write movixrc config
echo "extra-mplayer-options=-fs -zoom
loop=1
eject=y
dma=n" > "${TMPDIR}/movix/movixrc"

# Make all files onCD readable.
chmod -R a+r "${TMPDIR}"

# Create the ISO itself
${MKISOFS} -v -r -J -f -no-emul-boot -boot-load-size 4 -boot-info-table -b isolinux/isolinux.bin -c isolinux/isolinux.boot -sort ${TMPDIR}/isolinux/iso.sort -o "${TMPDIR}.iso" -V "${BASENAME}" ${TMPDIR} || zenity --error --text "Error Writing ISO Image ${TMPDIR}.iso"

OUTPUT=`cp -a "${TMPDIR}.iso" "${HOME}/${BASENAME}.iso"`
ERROR=$?
if [ $ERROR != 0 ]; then
    zenity --error --text "Unable to move ${TMPDIR}.iso to ${HOME}"
else
   rm -Rf ${TMPDIR}
fi

# Thank You.  Come Again...
 
