#!/bin/sh -efu

PROG=${0##*/}
CONFDIR="${HOME%/}/.xrandr-align"
if [ ! -d "$CONFDIR" ]; then
    SYSCONFDIR=${SYSCONFDIR:-/etc}
    CONFDIR="${SYSCONFDIR%/}/xrandr-align"
fi
if [ -z "${STATEDIR:-}" ]; then
    if [ -w /var/run ]; then
	STATEDIR=/var/run
    else
	STATEDIR=/tmp
    fi
fi
if [ -z "${DISPLAY:-}" ]; then
    echo "No DISPLAY is set" >&2
    exit 1
fi
PIDFILE="${STATEDIR%/}/$PROG$DISPLAY"

if [ $# -gt 1 ]; then
    echo "Usage: $PROG [--start|--stop]" >&2
    exit 2
fi

if [ $# -gt 0 ] && [ $1 = "--stop" ]; then
    if [ -f "$PIDFILE" ]; then
	(
            flock 4
	    while read pid; do
		if ! kill -0 $pid || kill $pid; then
		    sed -i -e "/^$pid\$/d" "$PIDFILE"
		fi
	    done <&4
	    [ ! -s "$PIDFILE" ] && rm "$PIDFILE"
	) 4<"$PIDFILE"
    fi
elif [ $# -lt 1 ] || [ $1 = "--start" ]; then    
    if [ -f "${CONFDIR%/}/monitor" ]; then
	(
	    flock -n 4
	    cat "${CONFDIR%/}/monitor" |
	    sed -n -e 's/^"\([^"]\+\)"[[:space:]]\+"\([^"]\+\)"\(.*\)$/output="\1"; input="\2"; tail="\3"/p' |
	    while read margs; do
		eval $margs
		pre="${CONFDIR%/}/pre-align.sh"
		post="${CONFDIR%/}/post-align.sh"
		eval $(echo "$tail" | sed -e 's/[[:space:]]*\([a-z]\+\):/"; \1="/g' -e 's/^"; \([a-z]\+=".*\)$/\1"/')
		xrandr-align monitor --output="$output" --input="$input" `[ -n "$pre" ] && echo "--pre-script=\"$pre\""` `[ -n "$post" ] && echo "--post-script=\"$post\""` &
		echo $! >&4
	    done
	    flock -u 4
	) 4>>"$PIDFILE"
    fi
fi
