#!/bin/bash

# Variables. Feel free to customise!
LMATH_DOWNLOAD_URL="https://static.lehtodigital.fi/lmath/latest/linux/LMath.AppImage"
LMATH_BASE="/opt/lmaths"
DESKTOP_ENTRY_DIR="/usr/share/applications"

if [[ $* == *--help* || $* == *-h* ]]; then
    echo "=== L'Math Penguin Installer ==="
    echo ""
    echo "This script will automatically download and install L'Math for organizations."
    echo "By default, the program will be installed globally to /opt/ and a desktop entry"
    echo "will be created in /usr/share/applications - however, you can use --local to"
    echo "only install for the current user to ~/.lmaths"
    echo ""
    echo "   Usage:"
    echo "               -y      Proceed automatically without asking"
    echo "     --help    -h      Show this help index"
    echo "     --local           Install locally (only for current user)"
    echo ""
    exit
fi

echo "        _                   "
echo "  __   | |_____     _   _   "
echo " |  |  |_|     |___| |_| |_ "
echo " |  |__  | | | | .'|  _|   |"
echo " |_____| |_|_|_|__,|_| |_|_|"
echo "                            "

if [[ $* == *--local* ]]; then
    LMATH_BASE="$HOME/.lmaths" 
    DESKTOP_ENTRY_DIR="$HOME/.local/share/applications"
fi

LMATH_APPIMAGE_FILE="${LMATH_BASE}/LMath.AppImage"
LMATH_ICON_FILE="${LMATH_BASE}/icon.png"
LMATH_DESKTOP_ENTRY="${LMATH_BASE}/lmaths.desktop"

echo "=== L'MATH FOR ORGANIZATIONS ==="
echo "  www.lehtodigital.fi (c) 2023"
echo ""
echo "This script will install L'Math to  $LMATH_BASE"
echo "and add a desktop file entry to     $DESKTOP_ENTRY_DIR"
echo "to add the program to most desktop menus."
echo "The latest AppImage version will be downloaded. In order to use this software,"
echo "you or your organization needs to have a valid license key."
echo ""
echo "You will need the permissions to modify $LMATH_BASE and $DESKTOP_ENTRY_DIR"
echo ""

# Check if there could be a previous installation
if test -f "$LMATH_APPIMAGE_FILE"; then
    echo "+-------------------------------------------------------------+"
    echo "  (!) Seems that L'Math is already installed in ${LMATH_BASE}"
    echo "      This script will update the software to latest version"
    echo "      and update the desktop entry for all users.           "
    echo "+-------------------------------------------------------------+"
    echo ""
fi

# Confirm the installation
while true; do
    if [[ $* == *-y* ]]; then
        echo "(Auto-accepting the installation, since -y is set)"
        break
    fi
    read -p "Do you wish to proceed? (y/n)" yn
    case $yn in
        [yY] ) echo "Yay! We will now begin the magic.";
            break;;
        [nN] ) echo "Exiting.";
            exit;;
        * ) echo "Invalid response";;
    esac
done

# Download the actual file
mkdir -p "$LMATH_BASE"
wget -O "$LMATH_APPIMAGE_FILE" "$LMATH_DOWNLOAD_URL"
chmod a+x "$LMATH_APPIMAGE_FILE"

# Download icon
wget -O "$LMATH_ICON_FILE" "https://lehtodigital.fi/f/FuTXW"

# Create desktop file
echo "[Desktop Entry]" > "$LMATH_DESKTOP_ENTRY"
echo "Version=1.9" >> "$LMATH_DESKTOP_ENTRY"
echo "Type=Application" >> "$LMATH_DESKTOP_ENTRY"
echo "Name=LMathS" >> "$LMATH_DESKTOP_ENTRY"
echo "Comment=Matikkaeditori YO-treenaamiseen" >> "$LMATH_DESKTOP_ENTRY"
echo "Exec=${LMATH_APPIMAGE_FILE} %f" >> "$LMATH_DESKTOP_ENTRY"
echo "Icon=${LMATH_ICON_FILE}" >> "$LMATH_DESKTOP_ENTRY"
echo "Actions=Editor" >> "$LMATH_DESKTOP_ENTRY"
echo "Terminal=false" >> "$LMATH_DESKTOP_ENTRY"
echo "Categories=GNOME;GTK;Education;Office;Science;" >> "$LMATH_DESKTOP_ENTRY"

# Copy menu entry for all users
cp "$LMATH_DESKTOP_ENTRY" "$DESKTOP_ENTRY_DIR"
xdg-desktop-menu forceupdate

echo "Done! (Hopefully!)"
