#!/bin/sh

# local configuration
#
# user configuration file
config_file="$HOME/.screencasterrc"
# audio input (when not defined, script checks for pulse socket
# and falls back to alsa if not found)
#audio_input="-f pulse -i default"
# ffmpeg verision: ffmpeg or avconv
ffmpeg="avconv"
# ffmpeg parameters: ffmpeg needs a preset for libx264, avconv does not.
#ffparams_video=${ffparams_video:-"-vcodec libx264 -vpre fast -qscale 2"}
ffparams_video=${ffparams_video:-"-vcodec libx264 -qscale 2"}
# use ffparams_audio="-an" to disable audio recording
ffparams_audio=${ffparams_audio:-""}
ffparams=${ffparams:-""}
# frame size
frame_size=`LANG=C xrandr -q | grep ^Screen | head -1 | sed 's|^.*current ||;s|,.*$||' | tr -d ' ' `
# file name suffix
ext="mp4"
# date format used in file names
cdate="$(date +%F)"
# ffmpeg process regexp
ffexp="x11grab"
# screencasts directory
data_dir="./"

# usage
usage()
{
    echo "Usage: recstart|recstop"
    echo "User configuration file: $HOME/.screencasterrc"
    echo "Please take a look at the documentation for examples."
    exit 1
}

# error in config file
config_file_error()
{
    echo "User config file seems to contain a syntax error."
    echo "Please take a look at the documentation for examples."
    exit 1
}

# data directory does not exist or is not writable
data_dir_not_usable()
{
    echo "The directory, where your screencasts are to be placed"
    echo "does not exist or is not writable. Please check."
    exit 1
}

# alsa or pulse?
select_audio_input()
{
    if [ -S /tmp/.esd-$(id -u)/socket ]; then
	audio_input="-f pulse -i default"
    else
	audio_input="-f alsa -i plughw:0,0"
    fi
}

# check for the parameter
check_for_param()
{
    grep "^$param_to_check\=" $config_file >/dev/null 2>&1 &&
    param_set=$(grep ^$param_to_check\= $config_file | tail -1 | cut -f2 -d\=)
}

# processing config file parameters
parse_config_file()
{
    # frame size
    param_set=
    param_to_check=frame_size
    check_for_param
    if echo $param_set | egrep '^[[:alnum:]]{3,4}x[[:alnum:]]{3,4}$' >/dev/null 2>&1; then
	frame_size=$param_set
    elif [ -z $param_set ]; then
	true
    else
	config_file_error
    fi
    # screencasts directory
    param_set=
    param_to_check=data_dir
    check_for_param
    if [ -d "$param_set" -a -w "$param_set" ]; then
	data_dir=$param_set
    elif [ -z $param_set ]; then
	true
    else
	config_file_error
    fi
}

rstart()
{
	cd "$data_dir"

	if ls -1 $cdate* >/dev/null 2>&1; then
		filename="$cdate.$[$(ls -1 $cdate* | tail -1 | cut -f 2 -d .)+1]"
	else
		filename="$cdate.0"
	fi

	if pgrep -fl "$ffexp"; then
		exit 1
	else
		$ffmpeg -f x11grab -s $frame_size -r 10 -i $DISPLAY \
			$audio_input \
			$ffparams_video \
			-strict experimental $ffparams_audio \
			$ffparams $filename.$ext
	fi
}

rstop()
{
	if pgrep -fl $ffexp; then
		pkill -f $ffexp	
	else
		exit 1
	fi
}

# check for command line arguments
[ $# -ne 0 ]&& usage

# parse config file
[ -r $HOME/.screencasterrc ] && parse_config_file

# check if data dir is usable
[ -d $data_dir -a -w $data_dir ] || data_dir_not_usable

# select audio input
[ -z $audio_input ] && select_audio_input

case "`basename $0`" in
	recstart)
			rstart
			;;
	recstop)
			rstop
			;;
	*)
			exit 1
esac
