#!/bin/bash

# Description: make_iso.sh
#
# This script has been written so that vdr users are able to make
# DVD iso's of their converted vdr recordings.
# This script has also been improved to automatically search for
# and include other segments of split recordings. The script will
# look for any segments following the segment selected. 
#         (ie. If Segment_002.mpg is selected it will look for 003)
# 
#	This script is part of the vdr-roaster plugin
#
#		    TESTED WITH VDR-1.4.4
#
#
# command-line usage:   sh make_iso.sh /full/path/to/converted_vdr_file_00x.mpg
#			sh make_iso.sh /full/path/to/converted_avi_file.mpg
#
#
#					11/14/2006 John Germs

# Variables
MPG_FOLDER="/video/mpgs"
ISO_FOLDER="/video/iso"
MPG_RECORDING="$1"
TMP_FOLDER="/tmp"
DVD_STANDARD="704x480"
XML_FILE="dvdauthor.xml"
CHAPTERS="0,15:00,30:00,45:00,1:00:00,1:15:00,1:30:00,1:45:00,2:00:00,2:15:00,2:30:00,2:45:00,3:00:00,3:15:00,3:30:00,3:45:00"

# CHECK IF USER HAS THE PROPER DEPENDENCIES TO RUN THIS SCRIPT -----------------------
command_list=(awk cat df du dvdauthor echo grep mkisofs nice rm sed sleep which)

for command in ${command_list[@]}; do
        if [ ! -x "`which $command 2>&1`" ]; then
                echo -e "\nERROR: $command not found! "
                base=`basename $command`
                if [ "`which $base 2>&1 | grep "no \`basename $command\` in"`" != "" ]; then
                        echo -e "ERROR: $base was not found anywhere in your \$PATH."
                fi
                exit
        fi
done
# ------------------------------------------------------------------------------------

# check if its an mpg file
case "$MPG_RECORDING" in
*.vdr)
  echo "This recording is still in vdr format"
  exit
  ;;
*.avi)
  echo "This recording is still in avi format"
  exit
  ;;
*.mp3)
  echo "This recording is still in mp3 format"
  exit
  ;;
*.mpg)
  echo "Mpg file detected"
  ;;
*.iso)
  echo "This is already an iso file"
  exit
  ;;
*.wav)
  echo "This recording is still in wav format"
  exit
  ;;
*)
  echo "Unknown file format"
  exit
  ;;
esac

# create working directories
for X in "$MPG_FOLDER" "$ISO_FOLDER"
do
if [ ! -d "$X" ]; then
  echo $X" not found. Creating directory"
  mkdir $X
  if [ ! -d "$X" ]; then
  echo
  echo "Unable to create "$X
  echo "Check the main folder permissions."
  echo "Maybe the folder variables in make_iso.sh"
  echo "need to be altered?"
  exit
  fi
fi
done

# begin creating xml file
# start of autogenerated xml file
cat > $MPG_FOLDER/$XML_FILE <<BLOCK1
<dvdauthor dest="dvd/">
	<vmgm />
	<titleset>
		<titles>
         	   <video format="ntsc" aspect="4:3" resolution="$DVD_STANDARD" />
         	   <audio format="ac3" samplerate="48khz" />
			<pgc>
				<vob file="$MPG_RECORDING" chapters="$CHAPTERS" />
BLOCK1

# lets get the size of the selected mpg
MPG_RECORDING_SIZE=$(du -k $MPG_RECORDING | awk '{print $1}')
# Check for other recording segments
echo $MPG_RECORDING > $TMP_FOLDER/_VDR_current_file.txt
# remove the linux carriage return
tr -d '\012' < $TMP_FOLDER/_VDR_current_file.txt > $TMP_FOLDER/_VDR__current_file.txt
# remove file extension
sed -i 's/.mpg//g' $TMP_FOLDER/_VDR__current_file.txt
# load the users first recording segment
SEGMENT=$(tail -c 1 $TMP_FOLDER/_VDR__current_file.txt)
# This will present the message 'integer expression expected', but it works
if [ "$SEGMENT" -gt 0 ]; then 
echo "Looking for other segments ..."
sleep 1
# check for next segment
Z="1"
# create a file for segment names
tr -d '\012' < $TMP_FOLDER/_VDR_current_file.txt > $TMP_FOLDER/_VDR_segment_names.txt
while [ "$Z" = "1" ]; do
# add one to segment
PREVIOUS_SEGMENT=$SEGMENT
(( SEGMENT++ ))
sed -i 's/00'$PREVIOUS_SEGMENT'/00'$SEGMENT'/g' $TMP_FOLDER/_VDR_segment_names.txt
NEXT_SEGMENT=$(tail $TMP_FOLDER/_VDR_segment_names.txt)
if [ -f "$NEXT_SEGMENT" ]; then
  # display only name so remove mpg path
  echo $NEXT_SEGMENT > $TMP_FOLDER/_VDR_segment_found.txt
  sed -i 's#'$MPG_FOLDER/'##g' $TMP_FOLDER/_VDR_segment_found.txt
  SEGMENT_FOUND=$(tail $TMP_FOLDER/_VDR_segment_found.txt)
  echo "Found "$SEGMENT_FOUND
  # might want to >> here to write next segment of dvd_compliant.xml
cat >> $MPG_FOLDER/$XML_FILE <<BLOCK1
				<vob file="$NEXT_SEGMENT" />
BLOCK1
  # better keep a tally of the sizes
  MPG_SEGMENT_SIZE=$(du -k $MPG_RECORDING | awk '{print $1}')
  MPG_RECORDING_SIZE=$(($MPG_RECORDING_SIZE + $MPG_SEGMENT_SIZE))
 else
   # done looking
   Z="0"
fi
done
fi
# finish creating the xml file
cat >> $MPG_FOLDER/$XML_FILE <<BLOCK1
			</pgc>
		</titles>
	</titleset>
</dvdauthor>
BLOCK1
# end of autogenerated xml file

# Lets check if we have enough room
echo
echo "We need almost 2.5 times the size of your MPG/s"
echo "as free space for working room... Let's check it out"
sleep 2
echo
echo -e "Your MPG Recording(s)\t"$(($MPG_RECORDING_SIZE/1024))" megs"

RATIO_OF_SPACE_FOR_DVD=$(echo "scale=1; (24/10)" | bc)
# how much room do we need for authoring
NEEDED_SPACE=$(echo "scale=0; ($MPG_RECORDING_SIZE * $RATIO_OF_SPACE_FOR_DVD)/ 1024" | bc)
echo
echo -e "Free Space Needed\t"$NEEDED_SPACE" megs"
echo
# how much room do we have
mkdir make_dvd_check_size_folder
CHECK_SIZE=$(/bin/df make_dvd_check_size_folder | /bin/awk '/^\/dev\// {print $4}')
# now convert the format to megs
CURRENT_SPACE=$(echo "scale=0; ($CHECK_SIZE / 1024)" | bc)
rm -rf mkdir make_dvd_check_size_folder
# do we have enough room
if [ "$NEEDED_SPACE" -gt "$CURRENT_SPACE" ]; then
    echo "You only have "$CURRENT_SPACE" megs of free space"
    echo
    echo "It looks like you dont have enough room"
    echo "to create this iso"
    exit
fi
echo "You have "$CURRENT_SPACE" megs of free space"
echo
echo "You are good to go !"
echo
sleep 4


# load name for dvd iso 
# remove the linux carriage return
tr -d '\012' < $TMP_FOLDER/_VDR_current_file.txt > $TMP_FOLDER/_VDR_dvd_iso_name.txt
# take off path
sed -i 's#'$MPG_FOLDER/'##g' $TMP_FOLDER/_VDR_dvd_iso_name.txt
# change file extension to iso
sed -i 's/.mpg/.iso/g' $TMP_FOLDER/_VDR_dvd_iso_name.txt
# load the variable
ISO_NAME=$(tail $TMP_FOLDER/_VDR_dvd_iso_name.txt)

cd $MPG_FOLDER

# create dvd file structure
nice -n 0 dvdauthor -o dvd -x $XML_FILE & wait

# test it with xine
#xine dvd:"$(pwd)/dvd"

# make the file structure into an iso
nice -n 0 mkisofs -dvd-video -v -o $ISO_NAME dvd & wait

# move iso to iso folder
mv -f $ISO_NAME $ISO_FOLDER/

# clean up files
rm -f $XML_FILE
rm -f -r $MPG_FOLDER/dvd
rm -f $TMP_FOLDER/_VDR_*

exit
