#!/bin/sh

find_it() {
  WHICH1=`which $1 2> /dev/null`
  if [ $? -eq 0 ]
  then
    LOC=`which $1 2> /dev/null | head -n 1`
  else
    [ -x /usr/local/sbin/$1 ] && LOC=/usr/local/sbin/$1
    [ -z "$LOC" -a -x /usr/local/bin/$1 ] && LOC=/usr/local/bin/$1
    [ -z "$LOC" -a -x /usr/sbin/$1 ] && LOC=/usr/sbin/$1
    [ -z "$LOC" -a -x /usr/bin/$1 ] && LOC=/usr/bin/$1
    [ -z "$LOC" -a -x /sbin/$1 ] && LOC=/sbin/$1
    [ -z "$LOC" -a -x /bin/$1 ] && LOC=/bin/$1
  fi
  echo "$LOC"
}


check_status() {
  # first check for systemd startup method
  ps -C systemd > /dev/null
  if [ $? -eq 0 -a -d /lib/systemd/system ]
  then
    SYSTEMCTL=`find_it systemctl`
    if [ -n "$SYSTEMCTL" ]
    then
      $SYSTEMCTL is-enabled resetmsmice.service > /dev/null 2>&1
      if [ $? -eq 0 ] 
      then
        echo "ENABLED"
        return 0
      fi
    fi
  fi

  # check systemd scripts
  if [ -x /etc/init.d/resetmsmice ] 
  then
    [ -f /etc/rc2.d/S17resetmsmice ] && SYSV_ENABLED=1
    [ -f /etc/rc3.d/S17resetmsmice ] && SYSV_ENABLED=1
    [ -f /etc/rc4.d/S17resetmsmice ] && SYSV_ENABLED=1
    [ -f /etc/rc5.d/S17resetmsmice ] && SYSV_ENABLED=1
    if [ -n "$SYSV_ENABLED" ]
    then
      echo "ENABLED"
      return 0
    fi
  fi

  # check for upstart scripts
  if [ -d /etc/init ]
  then
    INITCTL=`find_it initctl`
    if [ -n "$INITCTL" ]
    then
      temp_file=`mktemp`
      $INITCTL list > $temp_file
      if [ $? -eq 0 ]
      then
        line_count=`wc -l $temp_file | cut -d' ' -f1`
        rm $temp_file
        if [ "$line_count" -ge 5 ]
        then
          start_on=`$INITCTL show-config resetmsmice | grep -q "^  start on"`
          if [ $? -eq 0 ]
          then
            echo "ENABLED"
            return 0
          fi
        fi
      fi
    fi
  fi
  
  echo "DISABLED"
  return 0
}


enable_auto() {
  disable_all --quiet
  # first check for systemd startup method
  ps -C systemd > /dev/null
  if [ $? -eq 0 -a -d /lib/systemd/system ]
  then
    enable_systemd
    return $?
  fi

  if [ -d /etc/init.d ]
  then
    enable_sysv
    return $?
  fi

  # check for upstart scripts
  if [ -d /etc/init ]
  then
    INITCTL=`find_it initctl`
    if [ -n "$INITCTL" ]
    then
       temp_file=`mktemp`
       $INITCTL list > $temp_file
       if [ $? -eq 0 ]
       then
          line_count=`wc -l $temp_file | cut -d' ' -f1`
          rm $temp_file
          if [ "$line_count" -ge 5 ]
          then
             enable_upstart
             return $?
          fi
       fi
    fi
  fi
}


enable_upstart() {
	RESETMSMICE=`find_it resetmsmice`
	if [ ! -d /etc/init ]
	then
		echo "Error: Cannot find upstart directory '/etc/init'"
		echo "Are you sure your system uses upstart?"
		echo ""
		exit 1
	fi
	touch /etc/init/resetmsmice.conf
	if [ "$?" -ne 0 ]
	then
		echo "Error: Cannot write configuration file '/etc/init/resetmsmice.conf'"
		echo "Are you sure you have permissions to write to this file and directory?"
		echo "Hint: Try running as root."
		echo ""
		exit 2
	fi
	echo "Installing resetmsmice upstart scripts..."
	echo "# resetmsmice
# 
# repairs abnormally fast vertical scroll wheel issues with
# certain Microsoft wireless mice and X.org.
# run 'resetmsmice --help' for more info.

description \"Resetting any Wireless Microsoft mice that are known to cause scroll wheel issues with X.org\"

author \"paulrichards321@gmail.com\"

start on runlevel [2345] 

exec $RESETMSMICE -u

console output" > /etc/init/resetmsmice.conf
	if [ "$?" -eq 0 ]
	then
		echo "Complete!"
		return 0
	else
		echo "Installation failed writing to '/etc/init/resetmsmice.conf'!"
		exit 3
	fi
}


enable_systemd() {
	RESETMSMICE=`find_it resetmsmice`
	disable_systemd
	if [ ! -d /lib/systemd/system ]
	then
		echo "Error: Cannot find systemd directory '/lib/systemd/system'"
		echo "Are you sure your system uses systemd startup scripts?"
		exit 1
	fi
	touch /lib/systemd/system/resetmsmice.service
	if [ "$?" -ne 0 ]
	then
		echo "Error: Cannot write configuration file '/lib/systemd/system/resetmsmice.service'"
		echo "Are you sure you have permissions to write to this file?"
		echo "Hint: Try running as root."
		exit 2
	fi
	echo Installing resetmsmice systemd startup scripts...
	echo "[Unit]
Description=Fixes scroll wheel issues with certain Wireless Microsoft mice in X.org
After=syslog.target

[Service]
ExecStart=-$RESETMSMICE
Type=oneshot

[Install]
WantedBy=graphical.target" > /lib/systemd/system/resetmsmice.service
  if [ $? -eq 0 ]
  then
    SYSTEMCTL=`find_it systemctl`
    if [ -n "$SYSTEMCTL" ]
    then
      $SYSTEMCTL enable resetmsmice.service
      if [ $? -eq 0 ] 
      then
        echo "Complete!"
        return 0
      else
        echo "Failed to setup resetmsmice service on startup."
        exit 3
     fi
  fi
  else
    echo "Installation failed writing to '/lib/systemd/system/resetmsmice.service'!"
    exit 3
  fi
}


enable_sysv() {
	RESETMSMICE=`find_it resetmsmice`
	if [ ! -d /etc/init.d ]
	then
		echo "Error: Cannot find System V directory '/etc/init.d' are you sure your system uses System V startup scripts?"
		exit 1
	fi
	touch /etc/init.d/resetmsmice
	if [ $? -ne 0 ]
	then
		echo "Error: Cannot write configuration file '/etc/init.d/resetmsmice.sh'"
		echo "Are you sure you have permissions to write to this file?"
		echo "Hint: Try running as root."
		exit 2
	fi
	echo "Installing resetmsmice System V startup scripts..."
    echo "#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          resetmsmice 
# Required-Start:    \$syslog \$remote_fs
# Required-Stop:     \$syslog \$remote_fs 
# Should-Start:      resetmsmice
# Should-Stop:       
# Default-Start:     2 3 4 5
# Default-Stop:   
# Short-Description: reset erratic scrolling for Microsoft mice
# Description:       reset erratic scrolling for Microsoft mice after dual booting between Microsoft Windows and running X.org
### END INIT INFO

# import the LSB init functions
. /lib/lsb/init-functions

PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin

case \"\$1\" in
  start)
    $RESETMSMICE
  ;;

  *)
    echo \"Usage: /etc/init.d/resetmsmice {start}\"
    exit 1
    ;;
esac
" > /etc/init.d/resetmsmice
	chmod 755 /etc/init.d/resetmsmice
	if [ $? -eq 0 ]; then \
		ln -s /etc/init.d/resetmsmice /etc/rc2.d/S17resetmsmice
		ln -s /etc/init.d/resetmsmice /etc/rc3.d/S17resetmsmice
		ln -s /etc/init.d/resetmsmice /etc/rc4.d/S17resetmsmice
		ln -s /etc/init.d/resetmsmice /etc/rc5.d/S17resetmsmice
	fi
	if [ $? -eq 0 ]
	then
		echo "Complete!"
		return 0
	else
		echo "Installation failed creating links to '/etc/init.d/resetmsmice'!"
		exit 3
	fi
}


disable_all() {
	disable_systemd
	rm -f /etc/init/resetmsmice.conf
	rm -f /etc/init.d/resetmsmice
	rm -f /etc/rc2.d/99resetmsmice
	rm -f /etc/rc2.d/S99resetmsmice
	rm -f /etc/rc3.d/99resetmsmice
	rm -f /etc/rc3.d/S99resetmsmice
	rm -f /etc/rc4.d/99resetmsmice
	rm -f /etc/rc4.d/S99resetmsmice
	rm -f /etc/rc5.d/99resetmsmice
	rm -f /etc/rc5.d/S99resetmsmice
	rm -f /etc/init.d/resetmsmice.sh
	rm -f /etc/rc2.d/99resetmsmice.sh
	rm -f /etc/rc2.d/S99resetmsmice.sh
	rm -f /etc/rc3.d/99resetmsmice.sh
	rm -f /etc/rc3.d/S99resetmsmice.sh
	rm -f /etc/rc4.d/99resetmsmice.sh
	rm -f /etc/rc4.d/S99resetmsmice.sh
	rm -f /etc/rc5.d/99resetmsmice.sh
	rm -f /etc/rc5.d/S99resetmsmice.sh
	rm -f /etc/rc2.d/S17resetmsmice
	rm -f /etc/rc3.d/S17resetmsmice
	rm -f /etc/rc4.d/S17resetmsmice
	rm -f /etc/rc5.d/S17resetmsmice
	if [ ! "$1" = "--quiet" ] 
    then
        echo "Full uninstall of resetmsmice system scripts complete!"
    fi
}


disable_systemd() {
	if [ -f /etc/systemd/system/resetmsmice.service -o -f /lib/systemd/system/resetmsmice.service ]
	then
    SYSTEMCTL=`find_it systemctl`
    if [ -n "$SYSTEMCTL" ]
    then
      $SYSTEMCTL disable resetmsmice.service
      rm -f /etc/systemd/system/resetmsmice.service
      rm -f /lib/systemd/system/resetmsmice.service
    fi
	fi
}


program_syntax() {
  echo "Usage: resetmsmice-enable-boot [option]"
	echo "Needs just one of the following two options:"
  echo "  --enable        enable resetmsmice to run each time linux boots up"
  echo "                  this script will automatically detect what type of"
  echo "                  startup scripts your system uses"
  echo "  --disable       stop resetting the mouse on bootup"
  echo "  --status        check to see if resetmsmice is set to run on boot"
}


case "$1" in

--enable) enable_auto 
    errors=$?
    if [ "$errors" -eq 0 -a ! "$2" = "--quiet" ]
    then
        echo "If you want to fix your mouse right now, instead of waiting to reboot your PC, just run: resetmsmice or resetmsmice-gui"
        echo ""
    fi 
    exit $errors
    ;;

--status) check_status ;;

--upstart) enable_upstart ;;

--systemd) enable_systemd ;;

--sysv) enable_sysv ;;

--disable) disable_all $2 ;;

*) program_syntax ;;

esac

