#!/bin/bash
# AQ
# 20110907

DEVICE=`xinput_calibrator --list | sed "s/Device \"//;s/\" id.*//"`
if [ -z "$DEVICE" ];then
    echo "Devices not found"
    exit 1
fi

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
	echo "Exit $output"
	MODE=`xrandr | grep -A 3 -e "$output" | grep -e "*" | awk '{print $1}'`
	echo "Mode $MODE"
	ZEROMON=`xrandr | grep -e "+0+0" | awk  '{print  $1}'`
	if [ "$ZEROMON" == "$output" ]; then
	    DEFOUT="$output"
	    DEFMODE="$MODE"
	else
	    SECOUT="$output"
	fi
	
	(( COUNT++ ))
    done
    xrandr --addmode $SECOUT $DEFMODE

#    echo "Device $DEVICE"
#    echo "Disconnecting $SECOUT"
    xrandr --output $SECOUT --off
#    echo "Reset settings"
    xinput set-int-prop "$DEVICE" "Evdev Axis Calibration" 32 0 6000 0 6000
    xinput set-int-prop "$DEVICE" "Evdev Axes Swap" 8 0
    xinput set-prop "$DEVICE" --type=float "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
#    echo "Calibration start"
    xinput="$(xinput_calibrator --output-type xinput | grep 'xinput set-int-prop')"
#    echo "Turning on second monitor"
    xrandr --output $SECOUT --mode $DEFMODE --right-of $DEFOUT
    xrandr --output $DEFOUT --primary
    xinput set-prop "$DEVICE" --type=float "Coordinate Transformation Matrix" 0.5 0 0 0 1 0 0 0 1

#    echo "Writing settings"
    echo "#!/bin/bash" > /home/terminal/bin/calibrate.done
    echo "xrandr --addmode $SECOUT $DEFMODE" >> /home/terminal/bin/calibrate.done
    echo "xrandr --output $SECOUT --mode $DEFMODE --right-of $DEFOUT" >> /home/terminal/bin/calibrate.done
    echo "xrandr --output $DEFOUT --primary" >> /home/terminal/bin/calibrate.done
    echo "$xinput" >> /home/terminal/bin/calibrate.done
    echo "xinput set-prop \"$DEVICE\" --type=float \"Coordinate Transformation Matrix\" 0.5 0 0 0 1 0 0 0 1" >> /home/terminal/bin/calibrate.done
    chmod 755 /home/terminal/bin/calibrate.done
    echo -e "[Desktop Entry]\nType=Application\nName=xinput\nExec=/home/terminal/bin/calibrate.done" > $HOME/.config/autostart/xinput.desktop
else
    echo "single head"
    # reseting current calibration
    xinput set-int-prop "$DEVICE" "Evdev Axis Calibration" 32 0 6000 0 6000
    xinput set-int-prop "$DEVICE" "Evdev Axes Swap" 8 0
    xinput set-prop "$DEVICE" --type=float "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1

    xinput="$(xinput_calibrator --output-type xinput | grep 'xinput set-int-prop')"

    SWAP=`echo $xinput | grep Swap`
    if [ "$SWAP" != "" ];then
	OLD_IFS=$IFS
        IFS=$'\n'
        for line in ${xinput[@]} ; do
            SWAP=`echo $line | grep Swap`
            if [ "$SWAP" != "" ];then
                    xinput_swap="$line"
            fi
        done
        IFS=$OLD_IFS
    
        xinput="$(xinput_calibrator --output-type xinput | grep 'xinput set-int-prop')"
    
        echo "#!/bin/bash" > /home/terminal/bin/calibrate.done
        echo "$xinput" >> /home/terminal/bin/calibrate.done
        echo "$xinput_swap" >> /home/terminal/bin/calibrate.done
        chmod 755 /home/terminal/bin/calibrate.done
        echo -e "[Desktop Entry]\nType=Application\nName=xinput\nExec=/home/terminal/bin/calibrate.done" > $HOME/.config/autostart/xinput.desktop
    else
        echo "#!/bin/bash" > /home/terminal/bin/calibrate.done
        echo "$xinput" >> /home/terminal/bin/calibrate.done
        chmod 755 /home/terminal/bin/calibrate.done
        echo -e "[Desktop Entry]\nType=Application\nName=xinput\nExec=/home/terminal/bin/calibrate.done" > $HOME/.config/autostart/xinput.desktop
    fi
fi

rm -rf $HOME/.config/autostart/GT_service.desktop > /dev/null 2>&1
