#!/bin/bash

rulefile="/etc/udev/rules.d/99-autotoggle-touchpad.rules"
scriptfile="/usr/local/bin/toggleTouchpad"

if [ -f "$rulefile" ]; then
    kdialog --yesnocancel "I have found a previous rule\nWould you like to start again?\nYes = Starts from scratch\nNo = Remove scripts only (uninstall)\nCancel = Quit the script"
    case $? in
        0)
            tdesu rm "$rulefile"
            tdesu rm "$scriptfile"
            tdesu "udevadm control --reload-rules"
            ;;
        1)
            tdesu rm "$rulefile"
            tdesu rm "$scriptfile"
            tdesu "udevadm control --reload-rules"
            kdialog --msgbox "I have removed the rule and script" &
            exit
            ;;
        2)
            kdialog --msgbox "No changes made" &
            exit
            ;;
    esac
fi

if $(which xinput >/dev/null); then
    :
else
    kdialog --yesno "I cannot find the package xinput\nShould I install it for you?"
    if [ $? == 0 ]; then
        tdesu "apt-get -y install xinput"
    else
        kdialog --msgbox "I need this to run, please allow installation\nor install package 'xinput' before trying again." &
        exit
    fi
fi

function test_in {
    if [ -f /tmp/tempenv_in ];then return 0; else return 1; fi
}

function test_out {
    if [ -f /tmp/tempenv_out ];then return 0; else return 1; fi
}

function rem_in {
    if test_in; then tdesu rm /tmp/tempenv_in; fi
}

function rem_out {
    if test_out; then tdesu rm /tmp/tempenv_out; fi
}

function create_rule {
    cat >/tmp/myrule.rules <<EOL
ACTION=="add", SUBSYSTEM=="usb", RUN+="/usr/local/bin/toggleTouchpad 'off'"
ACTION=="remove", SUBSYSTEM=="usb", RUN+="/usr/local/bin/toggleTouchpad 'on'"
EOL
    tdesu mv /tmp/myrule.rules "$rulefile"
}

function create_test_script {
    cat >/tmp/myscript.sh <<EOL
#!/bin/bash
if [ "\$1" == "on" ];then
    env >> /tmp/tempenv_out
fi
if [ "\$1" == "off" ];then
    env >> /tmp/tempenv_in
fi
EOL
    tdesu mv /tmp/myscript.sh "$scriptfile"
    tdesu chmod +x "$scriptfile"
}

function create_full_script {
    cat >/tmp/fullscript.sh <<EOL
#!/bin/bash
if [ "\$$thekey" == "$thevalue" ]; then
    DISPLAY=":0.0"
    XAUTHORITY=$HOME/.Xauthority
    export DISPLAY XAUTHORITY
    TOUCHPAD_NAME="TouchPad"
    part1=\$(xinput list | grep -i \$TOUCHPAD_NAME)
    tpID=\$(echo \${part1#*id=} | awk '{ print \$1 }')
    case "\$1" in
        on)
        /usr/bin/xinput --enable \$tpID
        ;;
        off)
        /usr/bin/xinput --disable \$tpID
        ;;
    esac
fi
exit

EOL
    tdesu rm "$scriptfile"
    tdesu mv /tmp/fullscript.sh "$scriptfile"
    tdesu chmod +x "$scriptfile"
}

if [ -f "$rulefile" ]; then
    :
else
    create_rule
fi

if [ -f "$scriptfile" ]; then
    :
else
    create_test_script
fi

tdesu "udevadm control --reload-rules"

if test_in || test_out; then
    kdialog --warningyesno "Previous environment files found in /tmp\nDo you want me to delete these files?"
    if [ $? = 1 ]; then
        kdialog --sorry "Previous environment files must be removed\nPlease allow the script to remove them or\nremove them before running this script again" &
        exit
    else
        rem_in
        rem_out
    fi
fi

kdialog --msgbox "Please insert your usb mouse and then press OK."
if test_in; then
    kdialog --msgbox "Environment found\nPlease remove your usb mouse and then press ok."
else
    kdialog --msgbox "Environment not found\nDid you insert a usb mouse?\nPlease re-run the script" &
    exit
fi

if test_out; then
    kdialog --msgbox "Environment found\nPress ok to continue"
else
    kdialog --msgbox "Environment not found\nDid you remove the mouse?\nPlease re-run the script" &
    exit
fi

id_serial=`grep -f /tmp/tempenv_in /tmp/tempenv_out | sort -u | grep ^ID_SERIAL`

thekey=${id_serial%=*}
thevalue=${id_serial#*=}

create_full_script
tdesu "udevadm control --reload-rules"

rem_in
rem_out

kdialog --msgbox "I have configured a udev rule that will disable your touchpad when you plug in your mouse (as defined by the setup procedure) If you want to remove this rule or go through the setup for a different mouse run the setup script again and follow the instructions" 2
exit
