#!/bin/bash

if tput colors &> /dev/null && [ "$(tput colors)" -ge 8 ]; then
    RED=$(tput setaf 1)
    GREEN=$(tput setaf 2)
    YELLOW=$(tput setaf 3)
    BLUE=$(tput setaf 4)
    MAGENTA=$(tput setaf 5)
    CYAN=$(tput setaf 6)
    WHITE=$(tput setaf 7)
    RESET=$(tput sgr0)
else
    RED=""
    GREEN=""
    YELLOW=""
    BLUE=""
    MAGENTA=""
    CYAN=""
    WHITE=""
    RESET=""
fi

echo "${CYAN}        _                   "
echo "  __   | |_____     _   _   "
echo " |  |  |_|     |___| |_| |_ "
echo " |  |__  | | | | .'|  _|   |"
echo " |_____| |_|_|_|__,|_| |_|_|${RESET}"
echo "                            "
echo "${CYAN}L'Math Penguin Installer (c) Roni Lehto 2024${RESET}"

LMATH_BASE="${LMATH_BASE:-$HOME/.lmath}"
LMATH_APPIMAGE="$LMATH_BASE/LMath.AppImage"
LMATH_DESKTOP_ENTRY="$LMATH_BASE/lmath.desktop"
LMATH_ICON_FILE="$LMATH_BASE/icon.png"
DESKTOP_ENTRY_DIR="${DESKTOP_ENTRY_DIR:-$HOME/.local/share/applications}"
LMATH_INSTALL_PROXY_URL="https://api.lehtodigital.fi/lmath/installscript/"

# Find the AppImage (get the latest link from Github's API)
echo "Searching for the latest release..."
LMATH_INFO_RAW="$(wget -qO - $LMATH_INSTALL_PROXY_URL)"

if [[ $(echo "$LMATH_INFO_RAW" | head -n 1) != "SUCCESS" ]]; then
    echo "${RED}[!] Server did not respond with a success message:"
    echo "$LMATH_INFO_RAW${RESET}"
    exit 1
fi

LMATH_NEWEST_APPIMAGE=$(echo "$LMATH_INFO_RAW" | grep "AppImage")
LMATH_VERSION_INFO=$(echo "$LMATH_INFO_RAW" | tail -n +3)
LMATH_VERSION=$(echo "$LMATH_INFO_RAW" | grep "Version:" | sed 's/Version: *r\?\(.*\)/\1/')

echo ""
echo "${GREEN}----------------------------------------------------------------------"
echo "${GREEN} This script will download and install the latest L'Math AppImage."
echo "${GREEN} Desktop entries will be created (= the application should show up in"
echo "${GREEN} your desktop environment's menu, and can be selected to open files)${RESET}"
echo "${GREEN}----------------------------------------------------------------------${RESET}"

echo ""
echo "Installation destination:    $LMATH_BASE"
echo "Desktop entry destination:   $DESKTOP_ENTRY_DIR"
echo "Download URL:                $LMATH_NEWEST_APPIMAGE"

echo ""
echo "${YELLOW}Release details${RESET}"
echo "$LMATH_VERSION_INFO"

echo ""
echo "${YELLOW}End User License Agreement (EULA)${RESET}"
echo " -> https://lehtodigital.fi/lmath/license/${RESET}"
echo ""

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

# Base dir
mkdir -p $LMATH_BASE
echo "Found: $LMATH_NEWEST_APPIMAGE"

# Download
echo "Downloading..."
wget -qO "$LMATH_APPIMAGE" "$LMATH_NEWEST_APPIMAGE"
chmod +x "$LMATH_APPIMAGE"

# Download icon
wget -qO "$LMATH_ICON_FILE" "https://lehtodigital.fi/f/RPpe5"

# Create desktop entry
echo "Creating desktop entry..."
echo "[Desktop Entry]" > "$LMATH_DESKTOP_ENTRY"
echo "Version=$LMATH_VERSION" >> "$LMATH_DESKTOP_ENTRY"
echo "Type=Application" >> "$LMATH_DESKTOP_ENTRY"
echo "Name=L'Math" >> "$LMATH_DESKTOP_ENTRY"
echo "Comment=Matikkaeditori YO-treenaamiseen" >> "$LMATH_DESKTOP_ENTRY"
echo "Exec=${LMATH_APPIMAGE} %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

# Done!
echo ""
echo "${GREEN}All done!${RESET} L'Math should now be installed."
echo "You may have to reload your menu system to see the application's entry."
