#!/bin/bash

version="0.15.0"

BIN=/usr/local/bin/en-croissant
ICON=/usr/local/share/icons/en-croissant.png
DESKTOP=$HOME/.local/share/applications/en-croissant.desktop

install_packages \
unzip \
libwebkit2gtk-4.1-0 \
libgtk-3-0 \
libsoup-3.0-0 \
libjavascriptcoregtk-4.1-0 \
libgdk-pixbuf-2.0-0 \
libglib2.0-0 \
libcairo2 \
libpango-1.0-0 \
libatk1.0-0 \
libx11-6 \
libxrandr2 \
libxcomposite1 \
libxdamage1 \
libxfixes3 \
libxext6 \
libxrender1 \
libxi6 \
libxtst6 \
libxkbcommon0 \
libwayland-client0 \
libwayland-egl1 \
libegl1 \
libgl1 \
libgstreamer1.0-0 \
libgstreamer-plugins-base1.0-0 || exit 1

wget -O "/tmp/en-croissant-rpi.zip" "https://github.com/ferranox/en-croissant-rpi/archive/refs/tags/v${version}.zip" || error "failed to download!"
unzip "/tmp/en-croissant-rpi.zip" -d /tmp || error "failed to extract!"

cd "/tmp/en-croissant-rpi-${version}" || error "failed to enter extracted directory!"

sudo install arm64/en-croissant "$BIN" || error "failed to install binary!"

sudo mkdir -p /usr/local/share/icons || error "failed to create icon directory!"
sudo cp icon.png "$ICON" || error "failed to create icon!"

mkdir -p "$HOME/.local/share/applications" || error "failed to create desktop entry directory!"
cp en-croissant.desktop "$DESKTOP" || error "failed to create desktop directory!"
chmod +x "$DESKTOP" || error "failed to make desktop entry executable!"

rm -rf "/tmp/en-croissant-rpi.zip" "/tmp/en-croissant-rpi-${version}" || error "failed to cleanup!"
