#!/usr/bin/env bash

set -e

. /usr/share/dosis2/bin/shell-support-functions

enforce_root

calibration_file="`mktemp`"
function cleanup() {
    rm -f "${calibration_file}"
}
trap cleanup EXIT

# get the device to calibrate (xinput calibrator normally calibrates the last
# one in the list, so that's the one we will grab until we have problems)
device_id="`xinput_calibrator --list | tail -1 | sed -rnE 's/.*Device.*id=([0-9]+)/\1/p'`"

# run xinput calibrator
echo "Starting Calibration..."
minmax="`xinput_calibrator --device \"${device_id}\" --output-type xorg.conf.d \
    --misclick 15 --output-filename \"${calibration_file}\" \
    | sed -rnE 's/.*min_x=(-?[0-9]+).*max_x=(-?[0-9]+).*min_y=(-?[0-9]+).*max_y=(-?[0-9]+)/\1:\2:\3:\4/p'`"

# fetch our screen's min and max variables and calculate screen width
ts_minx="`echo ${minmax} | cut -d: -f1`"
ts_maxx="`echo ${minmax} | cut -d: -f2`"
ts_miny="`echo ${minmax} | cut -d: -f3`"
ts_maxy="`echo ${minmax} | cut -d: -f4`"
ts_width="`python -c \"print ${ts_maxx} - ${ts_minx}\"`"
ts_height="`python -c \"print ${ts_maxy} - ${ts_miny}\"`"

# fetch configuration variables
conf_minx="`cat \"${calibration_file}\" | sed -rn 's/^\s+Option\s+"MinX"\s+"(-?[0-9]+)"$/\1/p'`"
conf_maxx="`cat \"${calibration_file}\" | sed -rn 's/^\s+Option\s+"MaxX"\s+"(-?[0-9]+)"$/\1/p'`"
conf_miny="`cat \"${calibration_file}\" | sed -rn 's/^\s+Option\s+"MinY"\s+"(-?[0-9]+)"$/\1/p'`"
conf_maxy="`cat \"${calibration_file}\" | sed -rn 's/^\s+Option\s+"MaxY"\s+"(-?[0-9]+)"$/\1/p'`"
conf_touch_width="`python -c \"print ${conf_maxx} - ${conf_minx}\"`"
conf_touch_height="`python -c \"print ${conf_maxy} - ${conf_miny}\"`"
c0=`python -c "print float(${ts_width}) / float(${conf_touch_width})"`
c1=`python -c "print -float(${conf_minx}) / float(${conf_touch_width})"`
c2=`python -c "print float(${ts_height}) / float(${conf_touch_height})"`
c3=`python -c "print -float(${conf_miny}) / float(${conf_touch_height})"`

# output something to the screen
echo "Touchscreen Configuration"
echo "  Touch Panel MinX:    ${ts_minx}"
echo "  Touch Panel MaxX:    ${ts_maxx}"
echo "  Touch Panel MinY:    ${ts_miny}"
echo "  Touch Panel MaxY:    ${ts_maxy}"
echo "  Touch Panel Width:   ${ts_width}"
echo "  Touch Panel Height:  ${ts_height}"
echo ""
echo "Calibration Configuration"
echo "  Config MinX:         ${conf_minx}"
echo "  Config MaxX:         ${conf_maxx}"
echo "  Config MinY:         ${conf_miny}"
echo "  Config MaxY:         ${conf_maxy}"
echo "  Config Touch Width:  ${conf_touch_width}"
echo "  Config Touch Height: ${conf_touch_height}"
echo ""
echo "Matrix Configuration"
printf "  %7.4f %7.4f %7.4f\n" ${c0} 0 ${c1}
printf "  %7.4f %7.4f %7.4f\n" 0 ${c2} ${c3}
printf "  %7.4f %7.4f %7.4f\n" 0 0 1

c0fmt="`printf \"%.4f\" ${c0}`"
c1fmt="`printf \"%.4f\" ${c1}`"
c2fmt="`printf \"%.4f\" ${c2}`"
c3fmt="`printf \"%.4f\" ${c3}`"
echo "ACTION==\"add|change\", KERNEL==\"event[0-9]*\", ENV{ID_VENDOR_ID}==\"0eef\", ENV{ID_MODEL_ID}==\"0001\", \
    ENV{LIBINPUT_CALIBRATION_MATRIX}=\"${c0fmt} 0 ${c1fmt} 0 ${c2fmt} ${c3fmt}\"" \
    > "/etc/udev/rules.d/99-evdev-libinput-calibration.rules"

# set temporarily
xinput set-prop "${device_id}" 'libinput Calibration Matrix' ${c0fmt} 0 ${c1fmt} 0 ${c2fmt} ${c3fmt} 0 0 1

# reload udevadm rules
udevadm control --reload-rules
udevadm trigger
