#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2024 ArchR (https://github.com/archr-linux/Arch-R)
. /etc/profile.d/001-functions
SPECIALIZATIONS="undervolt-cpu undervolt-gpu driver-gpu"

check_valid_specialization() {
    for spec in $SPECIALIZATIONS; do
        if [ "$spec" = "${1}" ]; then
            return
        fi
    done
    echo "Unknown specialization: ${1}"
    echo "Valid specializations are: ${SPECIALIZATIONS}"
    exit 1
}

get_current_custom_overlay() {
    fdtoverlaysline=$(grep FDTOVERLAYS /flash/extlinux/extlinux.conf)
    overlays=${fdtoverlaysline##  FDTOVERLAYS}
    for ol in $overlays; do
        match=0
        for spec in $SPECIALIZATIONS; do
            if [[ $ol == *"$spec"* ]]; then
               match=1
               break
            fi
        done
        if [ $match = 0 ]; then
            custom=$ol
            break
        fi
    done
    if [ $custom ]; then
        echo $custom | cut -d '/' -f 3
    else
        echo "None"
    fi
}

get_current_specialized_overlay() {
    fdtoverlaysline=$(grep FDTOVERLAYS /flash/extlinux/extlinux.conf)
    overlays=${fdtoverlaysline##  FDTOVERLAYS}
    for ol in $overlays; do
        if [[ $ol == *"$1"* ]]; then
            specialized="${ol} "
            break
        fi
    done
    if [ $specialized ]; then
        echo $specialized | cut -d '/' -f 3
    else
        echo "None"
    fi

}

list_custom_overlays() {
    for ol in /flash/overlays/*.dtbo; do
        for spec in $SPECIALIZATIONS; do
            if [[ $ol == *"$spec"* ]]; then
                ol=""
        break
            fi
        done
        if [ $ol ]; then
            echo -n $ol | cut -d '/' -f 4 | tr -d '\n'
            echo -n " "
        fi
    done
    echo
}

list_specialized_overlays() {
    for ol in /flash/overlays/*.dtbo; do
        if [[ $ol == *"$1"* ]]; then
            echo -n $ol | cut -d '/' -f 4 | tr -d '\n'
            echo -n " "
        fi
    done
    echo
}

write_dtbo() {
    PRE=/overlays/
    for spec in $SPECIALIZATIONS custom_dtbo; do
	ol=$(get_setting system.$spec)
        if [ "$ol" != "None" ] && [[ ! -z $ol ]]; then
            dtbos+="${PRE}${ol} "
        fi
    done
    mount -o remount,rw /flash
    if [ "$dtbos" ]; then
        if grep --quiet FDTOVERLAYS /flash/extlinux/extlinux.conf; then
            sed -i "/FDTOVERLAYS/c \ \ FDTOVERLAYS ${dtbos}" /flash/extlinux/extlinux.conf
        else
            sed -i "/FDT/a \ \ FDTOVERLAYS ${dtbos}" /flash/extlinux/extlinux.conf
        fi
    else
        sed -i "/FDTOVERLAYS/d" /flash/extlinux/extlinux.conf
    fi
    mount -o remount,ro /flash
}

case "$1" in
    "ls")
        case "$2" in
            "custom")
                list_custom_overlays
                exit 0
            ;;
            *)
                check_valid_specialization $2
                list_specialized_overlays $2
                exit 0
            ;;
        esac
    ;;
    "get")
        case "$2" in
            "custom")
                get_current_custom_overlay
                exit 0
            ;;
            *)
                check_valid_specialization $2
                get_current_specialized_overlay $2
                exit 0
            ;;
        esac
    ;;
   "set")
        case "$2" in
            "custom")
                for ol in $(list_custom_overlays); do
		            if [ "$3" = "$ol" ] || [ $3 = None ]; then
                        set_setting system.custom_dtbo $3
                        write_dtbo
                        exit 0
		            fi
		        done
                echo "dtbo doesn't exist: $3"
                exit 1
            ;;
            *)
                check_valid_specialization $2
	            for ol in $(list_specialized_overlays $2); do
		            if [ "$3" = "$ol" ] || [ $3 = None ]; then
                        set_setting system.$2 $3
                        write_dtbo
                        exit 0
		            fi
		        done
                echo "dtbo doesn't exist: $3"
                exit 1
            ;;
        esac
    ;;
    *)
        echo "Unknown operation: ${1}"
        exit 1
    ;;
esac
