#!/bin/bash

#############################################################
# Zenity interface for G203 (Prodigy & Ligthsync) driver    #
# Last change: 07-07-2021                                   #
# Author: q3aql                                             #
# Contact: q3aql@protonmail.ch                              #
# License: GPL v2.0                                         #
#############################################################
VERSION="1.1"
M_DATE="070721"

# Variables
iconPath="/usr/share/icons/logitech/logitech-black.png"
imagesPath="/usr/share/icons/logitech/images"

# Function to check admin rights.
function rootMessage() {
  mkdir -p /etc/root &> /dev/null
  administrador=$?
  if [ ${administrador} -eq 0 ] ; then
    rm -rf /etc/root
  else
    zenity --title "zenidrv-g203-prodigy ${VERSION} (${M_DATE})" --window-icon=${iconPath} --warning \
    --width=340 --text "Administrator permissions are required"
    echo ""
    echo "* zenidrv-g203-prodigy ${VERSION} (${M_DATE}) (GPL v2.0)"
    echo ""
    echo "* Administrator permissions are required"
    echo ""
    exit
  fi
}

# Show menu with options.
showMenu=0
rootMessage
while [ ${showMenu} -eq 0 ] ; do
  clear
  opcion=$(zenity --width=280 --height=365 --list --title "zenidrv-g203-prodigy ${VERSION} ($M_DATE)" \
  --window-icon=${iconPath} --imagelist --column "Icon" --column "Effect/Option" ${imagesPath}/solid.png "Solid" \
  ${imagesPath}/cycle.png "Cycle" ${imagesPath}/breathe.png "Breathe" ${imagesPath}/intro-on.png \
  "Intro (On)" ${imagesPath}/intro-off.png "Intro (Off)" ${imagesPath}/mouse-dpi.png "Set DPI" \
  ${imagesPath}/light-off.png "Light off" ${imagesPath}/exit.png "Exit")
  if [ "${opcion}" == "${imagesPath}/solid.png" ] ; then
    color_selected=$(zenity --list --width=260 --height=570 --title "zenidrv-g203-prodigy ${VERSION} ($M_DATE)" \
    --window-icon=${iconPath} --imagelist --column "Color" --column "Name color" ${imagesPath}/black.png Black \
    ${imagesPath}/white.png White ${imagesPath}/red.png Red ${imagesPath}/lime.png Lime ${imagesPath}/blue.png Blue \
    ${imagesPath}/yellow.png Yellow ${imagesPath}/cyan.png Cyan ${imagesPath}/magenta.png Magenta \
    ${imagesPath}/silver.png Silver ${imagesPath}/gray.png Gray ${imagesPath}/maroon.png Maroon \
    ${imagesPath}/olive.png Olive ${imagesPath}/green.png Green ${imagesPath}/purple.png Purple ${imagesPath}/teal.png Teal \
    ${imagesPath}/navy.png Navy)
    canceled=$?
    if [ ${canceled} -eq 0 ] ; then
      if [ "${color_selected}" == "${imagesPath}/black.png" ] ; then
        color_apply="000000"
      elif [ "${color_selected}" == "${imagesPath}/white.png" ] ; then
        color_apply="FFFFFF"
      elif [ "${color_selected}" == "${imagesPath}/red.png" ] ; then
        color_apply="FF0000"
      elif [ "${color_selected}" == "${imagesPath}/lime.png" ] ; then
        color_apply="00FF00"
      elif [ "${color_selected}" == "${imagesPath}/blue.png" ] ; then
        color_apply="0000FF"
      elif [ "${color_selected}" == "${imagesPath}/yellow.png" ] ; then
        color_apply="FFFF00"
      elif [ "${color_selected}" == "${imagesPath}/cyan.png" ] ; then
        color_apply="00FFFF"
      elif [ "${color_selected}" == "${imagesPath}/magenta.png" ] ; then
        color_apply="FF00FF"
      elif [ "${color_selected}" == "${imagesPath}/silver.png" ] ; then
        color_apply="C0C0C0"
      elif [ "${color_selected}" == "${imagesPath}/gray.png" ] ; then
        color_apply="808080"
      elif [ "${color_selected}" == "${imagesPath}/maroon.png" ] ; then
        color_apply="800000"
      elif [ "${color_selected}" == "${imagesPath}/olive.png" ] ; then
        color_apply="808000"
      elif [ "${color_selected}" == "${imagesPath}/green.png" ] ; then
        color_apply="008000"
      elif [ "${color_selected}" == "${imagesPath}/purple.png" ] ; then
        color_apply="800080"
      elif [ "${color_selected}" == "${imagesPath}/teal.png" ] ; then
        color_apply="008080"
      elif [ "${color_selected}" == "${imagesPath}/navy.png" ] ; then
        color_apply="000080"
      fi
      sleep 2 | zenity --progress --title "zenidrv-g203-prodigy ${VERSION} (${M_DATE})" --text "Applying Solid effect" \
      --window-icon=${iconPath} --pulsate --no-cancel --auto-close
      g203-led.py solid "${color_apply}"
      device_error=$?
      if [ ${device_error} -eq 0 ] ; then
        echo > /dev/null
      else
        zenity --title "zenidrv-g203-prodigy ${VERSION} ($M_DATE)" --error --window-icon=${iconPath} --width=280 \
        --text "Device not found or connected"
      fi
      color_apply="none"
    else
      color_apply="none"
    fi
  elif [ "${opcion}" == "${imagesPath}/cycle.png" ] ; then
  sleep 2 | zenity --progress --title "zenidrv-g203-prodigy ${VERSION} (${M_DATE})" --text "Applying Cycle effect" \
  --window-icon=${iconPath} --pulsate --no-cancel --auto-close
    g203-led.py cycle
    device_error=$?
    if [ ${device_error} -eq 0 ] ; then
      echo > /dev/null
    else
      zenity --title "zenidrv-g203-prodigy ${VERSION} ($M_DATE)" --error --window-icon=${iconPath} --width=280 \
      --text "Device not found or connected"
    fi
  elif [ "${opcion}" == "${imagesPath}/breathe.png" ] ; then
    color_selected=$(zenity --list --width=260 --height=565 --title "zenidrv-g203-prodigy ${VERSION} ($M_DATE)" \
    --window-icon=${iconPath} --imagelist --column "Color" --column "Name color" ${imagesPath}/black.png Black \
    ${imagesPath}/white.png White ${imagesPath}/red.png Red ${imagesPath}/lime.png Lime ${imagesPath}/blue.png Blue \
    ${imagesPath}/yellow.png Yellow ${imagesPath}/cyan.png Cyan ${imagesPath}/magenta.png Magenta \
    ${imagesPath}/silver.png Silver ${imagesPath}/gray.png Gray ${imagesPath}/maroon.png Maroon \
    ${imagesPath}/olive.png Olive ${imagesPath}/green.png Green ${imagesPath}/purple.png Purple ${imagesPath}/teal.png Teal \
    ${imagesPath}/navy.png Navy)
    canceled=$?
    if [ ${canceled} -eq 0 ] ; then
      if [ "${color_selected}" == "${imagesPath}/black.png" ] ; then
        color_apply="000000"
      elif [ "${color_selected}" == "${imagesPath}/white.png" ] ; then
        color_apply="FFFFFF"
      elif [ "${color_selected}" == "${imagesPath}/red.png" ] ; then
        color_apply="FF0000"
      elif [ "${color_selected}" == "${imagesPath}/lime.png" ] ; then
        color_apply="00FF00"
      elif [ "${color_selected}" == "${imagesPath}/blue.png" ] ; then
        color_apply="0000FF"
      elif [ "${color_selected}" == "${imagesPath}/yellow.png" ] ; then
        color_apply="FFFF00"
      elif [ "${color_selected}" == "${imagesPath}/cyan.png" ] ; then
        color_apply="00FFFF"
      elif [ "${color_selected}" == "${imagesPath}/magenta.png" ] ; then
        color_apply="FF00FF"
      elif [ "${color_selected}" == "${imagesPath}/silver.png" ] ; then
        color_apply="C0C0C0"
      elif [ "${color_selected}" == "${imagesPath}/gray.png" ] ; then
        color_apply="808080"
      elif [ "${color_selected}" == "${imagesPath}/maroon.png" ] ; then
        color_apply="800000"
      elif [ "${color_selected}" == "${imagesPath}/olive.png" ] ; then
        color_apply="808000"
      elif [ "${color_selected}" == "${imagesPath}/green.png" ] ; then
        color_apply="008000"
      elif [ "${color_selected}" == "${imagesPath}/purple.png" ] ; then
        color_apply="800080"
      elif [ "${color_selected}" == "${imagesPath}/teal.png" ] ; then
        color_apply="008080"
      elif [ "${color_selected}" == "${imagesPath}/navy.png" ] ; then
        color_apply="000080"
      fi
      sleep 2 | zenity --progress --title "zenidrv-g203-prodigy ${VERSION} (${M_DATE})" --text "Applying Breathe effect" \
      --window-icon=${iconPath} --pulsate --no-cancel --auto-close
      g203-led.py breathe "${color_apply}"
      device_error=$?
      if [ ${device_error} -eq 0 ] ; then
        echo > /dev/null
      else
        zenity --title "zenidrv-g203-prodigy ${VERSION} ($M_DATE)" --error --window-icon=${iconPath} --width=280 \
        --text "Device not found or connected"
      fi
      color_apply="none"
    else
      color_apply="none"
    fi
  elif [ "${opcion}" == "${imagesPath}/intro-on.png" ] ; then
    sleep 2 | zenity --progress --title "zenidrv-g203-prodigy ${VERSION} (${M_DATE})" --text "Enabling startup effect" \
    --window-icon=${iconPath} --pulsate --no-cancel --auto-close
    g203-led.py intro on
    device_error=$?
    if [ ${device_error} -eq 0 ] ; then
      echo > /dev/null
    else
      zenity --title "zenidrv-g203-prodigy ${VERSION} ($M_DATE)" --error --window-icon=${iconPath} --width=280 \
      --text "Device not found or connected"
    fi
  elif [ "${opcion}" == "${imagesPath}/intro-off.png" ] ; then
    sleep 2 | zenity --progress --title "zenidrv-g203-prodigy ${VERSION} (${M_DATE})" --text "Disabling startup effect" \
    --window-icon=${iconPath} --pulsate --no-cancel --auto-close
    g203-led.py intro off
    device_error=$?
    if [ ${device_error} -eq 0 ] ; then
      echo > /dev/null
    else
      zenity --title "zenidrv-g203-prodigy ${VERSION} ($M_DATE)" --error --window-icon=${iconPath} --width=280 \
      --text "Device not found or connected"
    fi
  elif [ "${opcion}" == "${imagesPath}/mouse-dpi.png" ] ; then
    dpi_number=$(zenity --entry --title "Set DPI (from 200 to 8000)" --window-icon=${iconPath} --text "Enter DPI number (from 200 to 8000):" --entry-text "800")
    sleep 2 | zenity --progress --title "zenidrv-g203-prodigy ${VERSION} (${M_DATE})" --text "Setting DPI to ${dpi_number}" \
    --window-icon=${iconPath} --pulsate --no-cancel --auto-close
    g203-led.py dpi ${dpi_number}
    device_error=$?
    if [ ${device_error} -eq 0 ] ; then
      echo > /dev/null
    else
      zenity --title "zenidrv-g203-prodigy ${VERSION} ($M_DATE)" --error --window-icon=${iconPath} --width=280 \
      --text "Device not found or connected"
    fi
  elif [ "${opcion}" == "${imagesPath}/light-off.png" ] ; then
    sleep 2 | zenity --progress --title "zenidrv-g203-prodigy ${VERSION} (${M_DATE})" --text "Turning off the light on the mouse" \
    --window-icon=${iconPath} --pulsate --no-cancel --auto-close
    g203-led.py breathe 00FFFF 1000 0
    device_error=$?
    if [ ${device_error} -eq 0 ] ; then
      echo > /dev/null
    else
      zenity --title "zenidrv-g203-prodigy ${VERSION} ($M_DATE)" --error --window-icon=${iconPath} --width=280 \
      --text "Device not found or connected"
    fi
  elif [ "${opcion}" == "${imagesPath}/exit.png" ] ; then
    showMenu=1
  else
    zenity --title "zenidrv-g203-prodigy ${VERSION} (${M_DATE})" --window-icon=${iconPath} --warning \
    --width=300 --text "Choose one of the menu options"
  fi
done