#!/bin/bash

version=5.2.1

#Install Depends
enable_module fuse || exit 1

#Download Open-Typer build from repo
case "$arch" in
  "64") wget -O /tmp/Open-Typer.AppImage https://github.com/Open-Typer/Open-Typer/releases/download/v${version}/Open-Typer-aarch64.AppImage || error "Failed to download AppImage!" ;;
  "32") wget -O /tmp/Open-Typer.AppImage https://github.com/Open-Typer/Open-Typer/releases/download/v${version}/Open-Typer-armhf.AppImage || error "Failed to download AppImage!" ;;
  *) error "arch variable is not set, can not continue" ;;
esac

#Move Open-Typer.AppImage to /opt
sudo mv /tmp/Open-Typer.AppImage /opt || error "Failed to move from /tmp to /opt!"

#Mark Open-Typer.AppImage as executable
sudo chmod +x /opt/Open-Typer.AppImage || error "Failed to mark as executable!"

#Create Menu Button
status "Creating Menu Button"
sudo mkdir -p /usr/local/share/applications
echo "[Desktop Entry]
Name=Open-Typer
Comment=A free and open source typing tutor program
Icon=$(dirname "$0")/icon-64.png
Exec=/opt/Open-Typer.AppImage %f
Type=Application
Terminal=false
Categories=Education;" | sudo tee /usr/local/share/applications/Open-Typer.desktop >/dev/null || error "Failed to create menu button!"
