#!/bin/bash
#Name: ds-mouse
#Version:
#Depends: ds-mouse.py, xset
#Version: 3.0
#Author: Dave (david@daveserver.info), 2017
#License: GPL v.3
#Purpose: Configure mouse on per user basis for a session. This is the gui frontend

TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=ds-mouse

if [ ! -f "/usr/lib/ds-mouse/ds-mouse-config-check" ]; then 
    echo $"Cannot find configuration check script. Exiting";
    exit 1;
else
    . /usr/lib/ds-mouse/ds-mouse-config-check ;
fi

if [ ! -f "$user_config" ] || [ ! -d "$user_dts_dir" ]; then 
    echo $"User configuration file or directory missing: file:$user_config directory:$user_dts_dir" >&2; 
    exit 1;
fi

function acceleration() {
    acceleration="$(cat $user_config | grep '^ACCELERATION' |cut -d '=' -f2 |cut -d ' ' -f2)"
    threshold="$(cat $user_config | grep '^THRESHOLD' |cut -d '=' -f2 |cut -d ' ' -f2)"
    if [ "$acceleration" -gt -1 -a "$acceleration" -lt 17 ] && [ "$threshold" -gt -1 -a "$threshold" -lt 101 ]; then
       xset m "$acceleration" "$threshold"
    else
       echo $"out-of-range value for acceleration or threshold, so applying xset default values"
       xset m default
    fi
}

function buttonorder() {
    buttonorder="$(cat $user_config | grep '^BUTTONORDER' |cut -d '=' -f2 |cut -d ' ' -f2)"
    case $buttonorder in
        0)    xmodmap -e 'pointer = 1 2 3 4 5' > /dev/null 2>&1 ;;
        1)    xmodmap -e 'pointer = 3 2 1 4 5' > /dev/null 2>&1 ;;
        *)    xmodmap -e 'pointer = 1 2 3 4 5' > /dev/null 2>&1 ;;
    esac
}

function size() {
    size="$(cat $user_config | grep '^SIZE' |cut -d '=' -f2 |cut -d ' ' -f2)"
    file="$HOME/.Xdefaults";
    
    if [ ! -f "$file" ]; then 
        touch $file;
    fi

    if [ "$size" -gt 9 ]; then
        if cat $file |grep "Xcursor.size:"  > /dev/null ; then
            sed -i "s/Xcursor\.size.*/Xcursor\.size:$size/ig" $file
        else
            echo "Xcursor.size:$size" > $file
        fi
    else
        sed -n -i "s/Xcursor\.size*//ig" $file > /dev/null 2>&1
    fi
}

function touchpadlockout() {
    if [ -f "$user_dts_dir/syndaemon-pid" ]; then
        sdpid=$(head -1 $user_dts_dir/syndaemon-pid)
        kill -15 "$sdpid";
    fi
    
    if grep -iq "TOUCHPAD_LOCKOUT=.*true" $user_config; then
        touchpad_lockout_time="$(cat $user_config | grep '^TOUCHPAD_LOCKOUT_TIME' |cut -d '=' -f2 |cut -d ' ' -f2)"
        if [ "$touchpad_lockout_time" -gt -1 ] && [ "$touchpad_lockout_time" -lt 501 ]; then
            syndaemon -d -t $touchpad_lockout_time -p $user_dts_dir/syndaemon-pid &
        fi
    fi
}

function touchpad() {
    if grep -iq "TOUCHPAD=.*false" $user_config; then
        synclient TouchpadOff=1
    else
        synclient TouchpadOff=0
    fi
}

function reversescrolling() {
    VertScrollDelta=$(synclient -l |grep "VertScrollDelta" |cut -d "=" -f2|tr -d " -")
    if ! [ "$VertScrollDelta" -eq "$VertScrollDelta" ] > /dev/null 2>&1; then 
        echo $"Could not retrieve vertical scroll value. $VertScrollDelta";
        break;
    fi
    
    if grep -iq "REVERSE_SCROLLING=.*true" $user_config; then
        synclient VertScrollDelta="-$VertScrollDelta"
    else
        synclient VertScrollDelta="$VertScrollDelta"
    fi
}

function multiclick() {
    disp=${DISPLAY#:}
    disp=${disp%.[0-9]}
    if [ ! -z "$DESKTOP_SESSION_WM" ]; then
        wm="$DESKTOP_SESSION_WM";
    elif [ -f "$HOME/.desktop-session/desktop-code.$disp" ]; then
        wm=$(cat $HOME/.desktop-session/desktop-code.$disp|cut -d '-' -f2);
    else
        wm=$(wmctrl -m |grep "Class" |awk '{print $2}')
    fi
    
    clicktime="$(cat $user_config | grep '^CLICK_TIME' |cut -d '=' -f2 |cut -d ' ' -f2)"
    if [ "$clicktime" -gt 5000 ] && [ "$clicktime" -lt 0 ]; then
        echo $"Click Time is out of range. $clicktime"
        break;
    fi
    
    case $wm in 
        icewm)
            if [ -f "$HOME/.icewm/preferences" ]; then
                sed -i 's/^.*MultiClickTime=.*/MultiClickTime='$clicktime'/' $HOME/.icewm/preferences ;
            else 
                echo $"Icewm preferences file missing: $HOME/.icewm/preferences" >&2; 
                break ;
            fi
            ;;
        fluxbox) 
            if [ -f "$HOME/.fluxbox/init" ]; then
                sed -i 's/^.*session.doubleClickInterval:.*/session.doubleClickInterval: '$clicktime'/' $HOME/.fluxbox/init ;
            else 
                echo $"Fluxbox init file missing: $HOME/.fluxbox/init" >&2; 
                break ;
            fi
            ;;
        jwm)
            if [ -f "$HOME/.jwm/preferences" ]; then
                sed -i 's/^.*<DoubleClickSpeed>.*<\/DoubleClickSpeed>/<DoubleClickSpeed>'$clicktime'<\/DoubleClickSpeed>/' $HOME/.jwm/preferences ;
            else
                echo $"JWM preferences file missing: $HOME/.jwm/preferences" >&2; 
                break ;
            fi
            ;;
        *) echo $"No window manager found for double click speed";;
    esac
}

function help() {
    echo $"Help:";
    echo $"-a   | set mouse motion (acceleration and threshold)";
    echo $"-b   | set button order for left and right hand";
    echo $"-s   | set cursor size";
    echo $"-c   | set multiple click speed";
    echo $"-t   | Enable / Disable touchpad";
    echo $"-tl  | Enable / Disable touchpad lockout";
    echo $"-rs  | Enable / Disable reverse scrolling";
    echo $"-all | set mouse motion, button order, and cursor size";
    echo $"-h   | show this help dialog";
    echo $"No option start settings gui";
}

action="$1" && shift;
case  $action in 
    -a)	acceleration ;;
    -o) buttonorder  ;;
    -s) size         ;;
    -all)
        acceleration;
        buttonorder;
        size;
        multiclick;
        touchpad;
        touchpadlockout;
    ;;
    -t) touchpad ;;
    -tl) touchpadlockout ;;
    -rs) reversescrolling ;;
    -c) multiclick   ;;
    -h) help         ;;
    *)  /usr/local/bin/ds-mouse.py ;;
esac
