#!/bin/bash
# AQ
# 20111015

ADDIR="/home/terminal/atm/ad"
NUMMONITORS=`xrandr | grep -e " connected " | wc | awk '{print $1}'`

if [ $NUMMONITORS -gt 1 ]; then 
    echo "multi head"
    OUTPUTS=`xrandr | grep -e " connected " | awk '{print $1}'`
    COUNT=0
    for output in $OUTPUTS; do
        MODE=`xrandr | grep -A 3 -e "$output" | grep -e "*" | awk '{print $1}'`
        ZEROMON=`xrandr | grep -e "+0+0" | awk  '{print  $1}'`
	if [ "$ZEROMON" == "$output" ]; then
            DEFOUT="$output"
            DEFMODE="$MODE"
        else
            SECOUT="$output"
        fi
        
        (( COUNT++ ))
    done
    geometry=`xrandr | grep $SECOUT | awk '{print $3}'`
    while ((1)); do
	find $ADDIR -size 0 -delete
	find $ADDIR -name '*.*' -type f > $ADDIR/playlist
	if [ "$(stat -c%s "$ADDIR/playlist")" == "0" ];then
	    exit 1
	fi
	mplayer -fs -zoom -loop 0 -vo xv -framedrop -fixed-vo -shuffle -really-quiet -geometry $geometry -playlist $ADDIR/playlist
	sleep 2
    done
else
    echo "single head"
    find $ADDIR -size 0 -delete
    find $ADDIR -name '*.*' -type f > $ADDIR/playlist
    if [ "$(stat -c%s "$ADDIR/playlist")" == "0" ];then
        xscrfile="/home/terminal/.xscreensaver"
        echo -n > $xscrfile
        echo "timeout:	23:59:00" >> $xscrfile
        echo "cycle:		0:10:00" >> $xscrfile
        echo "lock:		False" >> $xscrfile
        echo "lockTimeout:	0:00:00" >> $xscrfile
        echo "passwdTimeout:	0:00:15" >> $xscrfile
        echo "visualID:	default" >> $xscrfile
        echo "installColormap:    True" >> $xscrfile
        echo "verbose:	False" >> $xscrfile
        echo "timestamp:	True" >> $xscrfile
        echo "splash:		False" >> $xscrfile
        echo "splashDuration:	0:00:05" >> $xscrfile
        echo "demoCommand:	xscreensaver-demo" >> $xscrfile
        echo "prefsCommand:	xscreensaver-demo -prefs" >> $xscrfile
        echo "nice:		10" >> $xscrfile
        echo "memoryLimit:	0" >> $xscrfile
        echo "fade:		True" >> $xscrfile
        echo "unfade:		False" >> $xscrfile
        echo "fadeSeconds:	0:00:03" >> $xscrfile
        echo "fadeTicks:	20" >> $xscrfile
        echo "captureStderr:	False" >> $xscrfile
        echo "ignoreUninstalledPrograms:True" >> $xscrfile
        echo "font:		*-medium-r-*-140-*-m-*" >> $xscrfile
        echo "dpmsEnabled:	False" >> $xscrfile
        echo "dpmsQuickOff:	False" >> $xscrfile
        echo "dpmsStandby:	2:00:00" >> $xscrfile
        echo "dpmsSuspend:	2:00:00" >> $xscrfile
        echo "dpmsOff:	4:00:00" >> $xscrfile
        echo "grabDesktopImages:  False" >> $xscrfile
        echo "grabVideoFrames:    False" >> $xscrfile
        echo "chooseRandomImages: False" >> $xscrfile
        echo "imageDirectory:	/usr/share/backgrounds/images" >> $xscrfile
        echo "" >> $xscrfile
        echo "mode:		off" >> $xscrfile
        echo "selected:	-1" >> $xscrfile
        echo "" >> $xscrfile
        echo "textMode:	literal" >> $xscrfile
        echo "textLiteral:	Прикоснитесь для оплаты" >> $xscrfile
        echo "textFile:	" >> $xscrfile
        echo "textProgram:	" >> $xscrfile
        echo "textURL:	" >> $xscrfile
        echo "" >> $xscrfile
        echo "pointerPollTime:    0:00:05" >> $xscrfile
        echo "pointerHysteresis:  10" >> $xscrfile
        echo "windowCreationTimeout:0:00:30" >> $xscrfile
        echo "initialDelay:	0:00:00" >> $xscrfile
        echo "GetViewPortIsFullOfLies:False" >> $xscrfile
        echo "procInterrupts:	True" >> $xscrfile
        echo "xinputExtensionDev: False" >> $xscrfile
        echo "overlayStderr:	True" >> $xscrfile
        chmod 644 $xscrfile
        chown terminal.terminal $xscrfile
        exit 1
    fi

    subfile="$ADDIR/subfile"
    echo -n > $subfile
    echo "1" >> $subfile
    echo "00:00:00,000 --> 23:00:00,999" >> $subfile
    echo "Прикоснитесь для оплаты" >> $subfile
    chmod 644 $subfile
    chown terminal.terminal $subfile

    xscrfile="/home/terminal/.xscreensaver"
    echo -n > $xscrfile
    echo "timeout:	0:05:00" >> $xscrfile
    echo "cycle:		0:10:00" >> $xscrfile
    echo "lock:		False" >> $xscrfile
    echo "lockTimeout:	0:00:00" >> $xscrfile
    echo "passwdTimeout:	0:00:15" >> $xscrfile
    echo "visualID:	default" >> $xscrfile
    echo "installColormap:    True" >> $xscrfile
    echo "verbose:	False" >> $xscrfile
    echo "timestamp:	True" >> $xscrfile
    echo "splash:		False" >> $xscrfile
    echo "splashDuration:	0:00:05" >> $xscrfile
    echo "demoCommand:	xscreensaver-demo" >> $xscrfile
    echo "prefsCommand:	xscreensaver-demo -prefs" >> $xscrfile
    echo "nice:		10" >> $xscrfile
    echo "memoryLimit:	0" >> $xscrfile
    echo "fade:		True" >> $xscrfile
    echo "unfade:		False" >> $xscrfile
    echo "fadeSeconds:	0:00:03" >> $xscrfile
    echo "fadeTicks:	20" >> $xscrfile
    echo "captureStderr:	False" >> $xscrfile
    echo "ignoreUninstalledPrograms:True" >> $xscrfile
    echo "font:		*-medium-r-*-140-*-m-*" >> $xscrfile
    echo "dpmsEnabled:	False" >> $xscrfile
    echo "dpmsQuickOff:	False" >> $xscrfile
    echo "dpmsStandby:	2:00:00" >> $xscrfile
    echo "dpmsSuspend:	2:00:00" >> $xscrfile
    echo "dpmsOff:	4:00:00" >> $xscrfile
    echo "grabDesktopImages:  False" >> $xscrfile
    echo "grabVideoFrames:    False" >> $xscrfile
    echo "chooseRandomImages: False" >> $xscrfile
    echo "imageDirectory:	/usr/share/backgrounds/images" >> $xscrfile
    echo "" >> $xscrfile
    echo "mode:		one" >> $xscrfile
    echo "selected:	0" >> $xscrfile
    echo "" >> $xscrfile
    echo "textMode:	literal" >> $xscrfile
    echo "textLiteral:	Прикоснитесь для оплаты" >> $xscrfile
    echo "textFile:	" >> $xscrfile
    echo "textProgram:	" >> $xscrfile
    echo "textURL:	" >> $xscrfile
    echo "" >> $xscrfile
    echo "programs:								      \\" >> $xscrfile
    echo "	       \"advertisement\" 	mplayer -fs -zoom -loop 0 -vo x11 -ao	      \\" >> $xscrfile
    echo "				  null -framedrop -fixed-vo -shuffle	      \\" >> $xscrfile
    echo "				  -really-quiet -nostop-xscreensaver -wid     \\" >> $xscrfile
    echo "				  \$XSCREENSAVER_WINDOW -sub		      \\" >> $xscrfile
    echo "				  $ADDIR/subfile		      \\" >> $xscrfile
    echo "				  -subpos 99				      \\" >> $xscrfile
    echo "				  -subcp utf8 -playlist			      \\" >> $xscrfile
    echo "				  $ADDIR/playlist	    \\n\\" >> $xscrfile
    echo "" >> $xscrfile
    echo "pointerPollTime:    0:00:05" >> $xscrfile
    echo "pointerHysteresis:  10" >> $xscrfile
    echo "windowCreationTimeout:0:00:30" >> $xscrfile
    echo "initialDelay:	0:00:00" >> $xscrfile
    echo "GetViewPortIsFullOfLies:False" >> $xscrfile
    echo "procInterrupts:	True" >> $xscrfile
    echo "xinputExtensionDev: False" >> $xscrfile
    echo "overlayStderr:	True" >> $xscrfile
    chmod 644 $xscrfile
    chown terminal.terminal $xscrfile
fi
