#!/bin/bash

printf "Touchegg full installer\n"

if [ "$1" == "-h" ]
then
    printf "Usage:\n-h            Print this help and exit\n--uninstall   Uninstall Touchégg GCE\n<no argument> Install Touchégg GCE"
elif [ "$1" == "--uninstall" ]
then
    printf "\nRemoving Touchégg GCE...\n"
    sudo rm -rvf "/usr/share/touchegg-gce" "/usr/share/icons/hicolor/*/apps/touchegg-gce.*" "/usr/bin/touchegg-gce" "$HOME/.config/touchegg/touchegg-gce.conf" "$HOME/.local/share/applications/touchegg-gce.desktop"
else
    printf "\nInstalling base tool...\n"
    sudo apt update
    sudo apt install touchegg -y
    printf "\nInstalling build dependencies for touchegg-gce...\n"
    sudo apt install build-essential libqt4-dev libx11-6 -y
    sudo apt install qtchooser libqt5webkit5-dev gstreamer1.0-plugins-base gstreamer1.0-tools gstreamer1.0-x -y
    printf "\nCreating working dir...\n"
    mkdir -v "touchegg-gce"
    printf "\nOpening working dir...\n"
    cd "touchegg-gce"
    printf "\nDownloading and unpacking touchegg-gce...\n"
    wget "https://github.com/Raffarti/Touchegg-gce/archive/refs/heads/master.zip"
    unzip "master.zip"
    printf "\nCompiling touchegg-gce...\n"
    cd "Touchegg-gce-master"
    qmake
    make
    printf "\nCopying executable to /usr/bin...\n"
    sudo cp -v "touchegg-gce" "/usr/bin/touchegg-gce"
    printf "\nCopying assets...\n"
    sudo mkdir -v "/usr/share/touchegg-gce"
    sudo cp -vr "Dictionaries" "/usr/share/touchegg-gce/"
    sudo cp -vrf "icons" "/usr/share/"
    sudo mkdir -v "$HOME/.config/touchegg"
    sudo cp -v "touchegg-gce.conf" "$HOME/.config/touchegg"
    printf "\nExiting working dir...\n"
    cd "../.."
    printf "\nCreating application menu shortcut...\n"
    cat > "$HOME/.local/share/applications/touchegg-gce.desktop" << '**EOF**'
#!/usr/bin/env xdg-open
[Desktop Entry]
Name=Touchégg GCE
Comment=GUI tool for Touchégg
Icon=touchegg-gce
Type=Application
Exec=touchegg-gce %u %f
**EOF**

    printf "\nStarting touchegg-gce..."
    touchegg-gce
fi
printf "\nWaiting 10 seconds before quitting...\n"
sleep 10
