#!/bin/zsh
# Copyright (C) 2023-2024 Dyne.org Foundation
#
# Designed, written and maintained by Denis Roio <jaromil@dyne.org>
#
# This source code is free software; you can redistribute it and/or
# modify it under the terms of the GNU Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This source code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	Please refer
# to the GNU Public License for more details.
#
# You should have received a copy of the GNU Public License along with
# this source code; if not, , see <https://www.gnu.org/licenses/>.

source /etc/boot.d/_dyne_utils
mm "dyne-mount-flatpak: `date`"

[[ -r /run/live/dynetab ]] || {
   mm "dynetab not found"
   exit 0
}

eval `awk '$4 ~ /flatpak/ { print "MOUNT="$1; print "DEV="$2 }' /run/live/dynetab`

[[ "$DEV" == "" ]] && {
	mm "flatpak not found in dynetab"
	exit 0
}

mm "flatpak found in $DEV"

[[ "$MOUNT" == "none" ]] && {
	eval `lsblk -P -o LABEL ${DEV}`
	[[ "$LABEL" == "" ]] && LABEL=`basename $DEV`
	mm "mount $DEV on /media/$LABEL"
	mkdir -p /media/"${LABEL}"
	mount ${DEV} /media/"$LABEL"
	MOUNT=/media/"$LABEL"
	mm "mount $DEV on $MOUNT"
}

[[ -r "$MOUNT"/dyne/flatpak ]] || {
	mm "flatpak not found in mounted $DEV"
	[[ "$LABEL" == "" ]] || umount /media"$LABEL"
	exit 0
}

mm "activate flatpak in $MOUNT"
mkdir -p /var/lib/flatpak
mount -o bind "$MOUNT"/dyne/flatpak /var/lib/flatpak
flatpak remote-add --if-not-exists \
		flathub https://flathub.org/repo/flathub.flatpakrepo
date > /var/lib/flatpak/.dyne
