#!/bin/bash
# Script encapsulant mencoder pour faciliter le travail.
# Default values
SCRIPT_NAME=$0
NO_SND_OPT="--nosound"
WITH_SND_OPT="--withsound"
ONLY_SND_OPT="--onlysound"
CONCAT_FILES="--concat"
DEFAULT_FPS=25
DEFAULT_OUTPUT=output.avi
DEFAULT_OUTPUT_WITH_SND=output-snd.avi
DEFAULT_CODEC=mpeg4
DEFAULT_SCALE=512
DEFAULT_EXT_SOURCE=JPG

usage() {
  echo "Usage : `basename $SCRIPT_NAME` [options]"
  echo -e "\t$NO_SND_OPT\t: transforme un lot d'images en animation"
  echo -e "\t$WITH_SND_OPT\t: transforme un lot d'images en animation"
  echo -e "\t\t\t  puis ajoute un fichier son"
  echo -e "\t$ONLY_SND_OPT\t: mixe un fichier son avec une animation existante"
  echo -e "\t$CONCAT_FILES\t: assemble plusieurs animations en une seule"
  echo -e "\t\t\t  les animations doivent avoir été encodée avec les "
  echo -e "\t\t\t  mêmes paramètres (codec video, framerate, ...)"
}

isNumber() { 
  [ -z "$1" ] && return 1 # 1 == false 
  echo "$1" | grep "^[0-9]*$" &>/dev/null
  return $?
}

encodingWithNoSound() {
  echo "OUTPUT_FILE [$DEFAULT_OUTPUT]"
  read output
  if [ -z "$output" ]
	then OUTPUT_FILE=$DEFAULT_OUTPUT
	else OUTPUT_FILE=$output
  fi

  echo "Encoding ... With no sound ..."
  mencoder "mf://*.$EXT_SOURCE" -mf fps=$FPS -o $OUTPUT_FILE  -ovc lavc -lavcopts vcodec=$CODEC -vf scale -zoom -xy $SCALE -nosound 2>/tmp/doMovie.$$.err > /tmp/doMovie.$$.log
  if [ -f "$OUTPUT_FILE" ]
	then echo "Encoding successfull !"
	else 	echo "Encoding failed :_["
		more /tmp/doMovie.$$.err
  fi
}

encodingWithSound() {
  if [ -z "$OUTPUT_FILE" ]
	then 	echo "INPUT_FILE [$DEFAULT_OUTPUT]"
		read file
		[ -z "$file" ] && file=$DEFAULT_OUTPUT
		if [ -f "$file" ]
			then INPUT_FILE=$file
			else echo "File not found ... [$file] ... exiting ..."
			     exit
		fi
	else 	INPUT_FILE=$OUTPUT_FILE
		OUTPUT_FILE=""
  fi

  echo "SOUND_FILE [none]"
  read sound
  if [ -z "$sound" ]
	then echo "No sound file ... exiting ..."
	     exit
	else SOUND_FILE="$sound"
  fi

  echo "OUTPUT_FILE [$DEFAULT_OUTPUT_WITH_SND]"
  read output
  if [ -z "$output" ]
	then OUTPUT_FILE=$DEFAULT_OUTPUT_WITH_SND
	else OUTPUT_FILE="$output"
  fi

  echo "Encoding ... With sound ..."
  mencoder -audiofile "$SOUND_FILE" -oac copy -o $OUTPUT_FILE -ovc copy $INPUT_FILE 2>>/tmp/doMovie.$$.err >> /tmp/doMovie.$$.log
  if [ -f "$OUTPUT_FILE" ]
	then echo "Encoding successfull !"
	else 	echo "Encoding failed :_["
		more /tmp/doMovie.$$.err
  fi
}

concat() {
  echo "DIR_SRC [.]"
  read dir
  if [ -z "$dir" -o ! -d "$dir" ]
	then DIR_SRC=.
	else DIR_SRC=$dir
  fi
  echo "EXT_FILES [.avi]"
  read ext
  if [ -z "$ext" ]
	then EXT_FILES=.avi
	else EXT_FILES=$ext
  fi
  echo "OUTPUT_FILE [$DEFAULT_OUTPUT]"
  read output
  if [ -z "$output" ]
	then OUTPUT_FILE=$DEFAULT_OUTPUT
	else OUTPUT_FILE=$output
  fi
  mencoder -o $OUTPUT_FILE -oac lavc -ovc copy $DIR_SRC/*$EXT_FILES  
}
# Start of script
# load configuration file
if [ -f ~/.doMovie.cfg ]
	then . ~/.doMovie.cfg
else
	# Saisie du nombre de frame par seconde	
	echo "FPS = [$DEFAULT_FPS]"
	read fps
	if isNumber "$fps" 
		then FPS=$fps
		else FPS=$DEFAULT_FPS
	fi 

	# Saisie du codec	
	echo "CODEC [$DEFAULT_CODEC]"
	read codec
	if [ -z "$codec" ]
		then CODEC=$DEFAULT_CODEC
		else CODEC=$codec
	fi

	# Saisie de l'échelle (Seule celle en X est intéressante 
	#                      on garde les porportions)
	echo "SCALE [$DEFAULT_SCALE]"
	read scale
	if isNumber "$scale" 
		then SCALE=$scale
		else SCALE=$DEFAULT_SCALE
	fi 

	# Extensions des fichiers
	echo "EXT_SOURCE [$DEFAULT_EXT_SOURCE]"
	read ext
	if [ -z "$ext" ] 
		then EXT_SOURCE=$DEFAULT_EXT_SOURCE
		else EXT_SOURCE=$ext
	fi
	echo "Writing configuration file"
	echo "FPS=$FPS" > ~/.doMovie.cfg
	echo "CODEC=$CODEC" >> ~/.doMovie.cfg
	echo "SCALE=$SCALE" >> ~/.doMovie.cfg
	echo "EXT_SOURCE=$EXT_SOURCE" >> ~/.doMovie.cfg
fi

case "$1" in 
	"$NO_SND_OPT") 	encodingWithNoSound
			;;
	"$WITH_SND_OPT") encodingWithNoSound
		       	 encodingWithSound
			;;
	"$ONLY_SND_OPT") encodingWithSound
			;;
	"$CONCAT_FILES") concat
			;;
	*) 	usage
esac 
