#!/bin/bash
#
# Macroscop: Flatpak for RPM Systems

readonly SCRIPT_URL="https://packages.macroscop.com/flatpak/install/flatpak-offline-macroscop.sh"
readonly SCRIPT_FILE="flatpak-offline.sh"
readonly TARGET_DIR="$HOME/flatpak_depends"

if [ -z "$1" ]; then
  APP_BRANCH="latest"
else
  APP_BRANCH="$1"
fi

mkdir "${TARGET_DIR}"

cd "${TARGET_DIR}" || exit

sudo yum install -y yum-utils > /dev/null
sudo yumdownloader --downloadonly flatpak --resolve > /dev/null

cd "$HOME" || exit

wget "$SCRIPT_URL" -O "$SCRIPT_FILE"
chmod +x "$SCRIPT_FILE"
bash "$SCRIPT_FILE" "$APP_BRANCH"
rm "$SCRIPT_FILE"

tar -czf flatpak.tgz -C "$HOME" flatpak_depends/* flatpak_macroscop/*

rm -rf flatpak_*
