#!/bin/bash

# Shell script for taking screenshots #
# by Sam Gynn #
# amended by Alan Hazelden #

usage="\
Usage:\n\
timelapse capture [-time interval] [-d directory] [-size scale] [-cam <webcam capture>]\n\
timelapse compile [-audio file] [-fps rate] output\n\
timelapse addaudio -audio audiofile videofile\n\n\
Example: timelapse capture -time 30 -size 50%\n\
Time interval defaults to every minute (60)\n\
FPS defaults to 10"

interval=60
bell="off"
dir="." 
fps=10

if [ ! -f /usr/bin/scrot ]; then
	echo "timelapse needs scrot to run"
	exit 1
fi

if test $# -eq 0; then
	echo -e "$usage" 1>&2
	exit 1
fi



while test $# -gt 0; do
	case $1 in
	-time|-t)
		interval=$2
		shift
		;;
	-b|-bell)
		bell=$2
		shift
		;;
	-d|-dir|-directory)
		dir=$2
		shift
		;;
	-s|-size)
		size=$2
		shift
		;;
	-cam)
		ccam=`echo $2 | sed 's/\([^ \t]*\).*/\1/'`
		cam=`which $ccam 2> /dev/null`
		if [ ! -n "$cam" ]; then
			echo "Invalid webcam capture program '$ccam'"
			echo -e "\n$usage" 1>&2
			exit
		fi
		shift
		;;
	-audio)
		audio=$2
		shift
		;;
	-fps)
		fps=$2
		shift
		;;
	-*)
		echo "Invalid option $1"
		exit 1
		;;
	*)
		if [ ! -n "$mode" ]; then
			mode=$1
		else
			if [ ! -n "$output" ]; then
				output=$1
			else
				echo "Error: found $1"
				echo -e "\n$usage" 1>&2
				exit 1
			fi
		fi
		;;
	esac
	shift
done

#make sure mode is set
if [ ! -n "$mode" ]; then
	echo "No mode set"
	echo -e "\n$usage" 1>&2
	exit 1
fi

#Do stuff
case $mode in
	capture)
	#Turn off system bell
	xset b $bell
	
	echo "Starting capture every $interval seconds"

	#Create directories
	if [ ! "$dir" = "." ]; then mkdir -p $dir; fi
	if [ -n "$cam" ]; then mkdir -p "$dir/cam"; fi

	#Conversion
	if [ -n "$size" ]; then
		convert='convert $f -resize '$size' -quality 90 '
	else
		convert='convert $f -quality 90 '
	fi

	#Start Capture
	count=0
	while true; do
		echo -en "Image $count\r"
		name=$(date +%s)

		#Webcam capture
		if [ -n "$cam" ]; then
			$cam "$dir/cam/$name.png" &> /dev/null  &
		fi

		#screenshot capture
		xset b off
		file=$dir/$name
		scrot $file.png -e "$convert $file.jpg; rm -f $file.png" &

		#Increment counter
		count=$(($count+1))

		#Sleep until next image
		sleep $interval
	done
	;;
	compile|video)
		if [ ! -n "$output" ]; then
			output="timelapse.avi"
		fi

		echo "Creating Video $output"
		if [ -n "$audio" ]; then
			audioc="-audiofile $audio -oac pcm"
		fi

		#Png or jpg
		if [ ! `ls $dir | grep .jpg` = "" ]
		then ext="jpg"
		else ext="png"
		fi

		#Compile video with mplayer
		mencoder "mf://$dir/*.$ext" -mf fps=$fps -o "$output" -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=800 $audioc
	;;
	addaudio|mix)
		if [ ! -n "$output" ]; then
			output="timelapse.avi"
		fi

		if [ ! -n "$audio" ]; then
			echo "No audio file set"
			echo -e "\n$usage" 1>&2
			exit 1
		fi
		
		if [ ! -f "$output" ]; then
			echo "$output does not exist"
			echo -e "\n$usage" 1>&2
			exit 1
		fi
		
		echo "Adding audio"

		mencoder $output -audiofile $audio -oac copy -ovc copy -o $output.tmp
		mv $output.tmp $output
	;;
	*)
	echo -e $usage 2>&1
	exit 1
	;;
esac



