# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2025 ALTEMU (https://www.altemu.ru)
# Based on automount script from ROCKNIX project:
# https://github.com/ROCKNIX/distribution

#!/bin/bash

function test_storage() {
UNMOUNTED_PARTITIONS=$(lsblk -l -o NAME,MOUNTPOINT | awk '$2 == ""' | grep "$1"p[0-9])
for DEV in ${UNMOUNTED_PARTITIONS}; do
    ROOTDEV=$(echo ${DEV} | sed -e "s#^/.*/##g" -e "s#p[0-9].*\$##g")
    if [ -L "/sys/class/block/${ROOTDEV}boot0" ]; then
    echo "Device ${ROOTDEV} might be Android, ignoring."
    # Assume this is an android boot device and ignore it.
    continue
    fi
    FSTYPE=$(blkid -o value -s TYPE /dev/"$DEV")
    case $FSTYPE in
    ext4|EXT4)
    mount /dev/"$DEV" /mnt
    HAVE_ROMS=$(ls /mnt/roms)
    if [[ -z $"HAVE_ROMS" ]]; then
	echo "Partition $"DEV" have no roms! Skipping..."
	umount /mnt
    else
	echo "Storage with EXT4 found! Mounting $DEV as external storage!"
	umount /mnt
	mount -o rw /dev/"$DEV" /storage/games-external
    fi
    ;;
    ntfs|NTFS)
    echo "Storage with NTFS found! Mounting $DEV as internal storage!"
    mount -t ntfs /dev/"$DEV" /storage/games-internal
    ;;
    *)
    echo "External storage not found. Mounting directory from home..."
    esac
done
}

function unmount(){
    umount -f "${1}"
}

function make_mark() {
    touch /storage/roms/PLACE_ROMS_HERE!.txt

}
function create_roms_folders() {
    while IFS= read -r line; do
        mkdir -p /storage/roms/$line
    done < /usr/share/altemu/dir-list
}

function overlayfs() {
EXTERNAL_ROMS="$(cat /proc/mounts | grep "/storage/")"
if [[ "$EXTERNAL_ROMS" =~ "external" ]]; then
    echo "SD Card with games is mounted as external"
    if [ ! -d /storage/games-external/.tmp/games-workdir ]; then
	mkdir -p /storage/games-external/.tmp/games-workdir
    fi
    mount overlay -t overlay -o lowerdir=/storage/games-internal/roms:/usr/share/roms:/home/altlinux/storage/roms,upperdir=/storage/games-external/roms,workdir=/storage/games-external/.tmp/games-workdir /storage/roms
elif [[ "$EXTERNAL_ROMS" =~ "internal" ]]; then
    echo "SD Card with games is mounted as internal"
    if [ ! -d /storage/games-external/.tmp/games-workdir ]; then
	mkdir -p /storage/games-external/.tmp/games-workdir
    fi
    mount overlay -t overlay -o lowerdir=/storage/games-internal/roms:/usr/share/roms:/home/altlinux/storage/roms,upperdir=/storage/games-external/roms,workdir=/storage/games-external/.tmp/games-workdir /storage/roms
else
    echo "No external storage... Mount home directory in /storage "
    mount --bind /home/altlinux/storage /storage/
fi
}

function access_rights() {
    chown -R altlinux:altlinux /storage
}

function overlay_device() {
    mkdir -p /storage/games-external/.tmp/games-workdir
}

OVERLAY_OK="$(mount | grep overlay)"

unmount /storage/roms
unmount /storage/games-external
unmount /storage/games-internal

test_storage
overlayfs
access_rights
if [[ -z "$OVERLAY_OK" ]]; then
    echo "Ooops! Huston, we got a problem..."
    exit 1
else
    echo "Overlay is ready!"
    create_roms_folders
exit 0
fi