#!/bin/bash

# set -x

PARAM=$#
MHWDXORGCONF="/etc/X11/xorg.conf.d/90-mhwd.conf"
CHECKCONFIG="false"
SETXORGCONF=""
SETMOD=""
ARCH=$(uname -m)
MODULES_LOAD="/etc/modules-load.d"

# param 1: modules to load
# param 2: blacklisted modules
set_modules() {
    echo "##" > "/etc/modprobe.d/mhwd-gpu.conf"
    echo "## Generated by mhwd - Manjaro Hardware Detection" >> "/etc/modprobe.d/mhwd-gpu.conf"
    echo "##" >> "/etc/modprobe.d/mhwd-gpu.conf"
    echo " " >> "/etc/modprobe.d/mhwd-gpu.conf"

    for module in $2
    do
        echo "blacklist ${module}" >> "/etc/modprobe.d/mhwd-gpu.conf"
        # Unload module if X is not running
        if [ ! "$(pgrep X)" ];    then
            rmmod -f ${module}
        fi
    done

    echo "##" > "${MODULES_LOAD}/mhwd-gpu.conf"
    echo "## Generated by mhwd - Manjaro Hardware Detection" >> "${MODULES_LOAD}/mhwd-gpu.conf"
    echo "##" >> "${MODULES_LOAD}/mhwd-gpu.conf"
    echo " " >> "${MODULES_LOAD}/mhwd-gpu.conf"

    for module in $1
    do
        echo "${module}" >> "${MODULES_LOAD}/mhwd-gpu.conf"
        # Load module if X is not running
        if [ ! "$(pgrep X)" ];    then
            modprobe ${module}
        fi
    done
}

# param 1: Xorg configuration file
set_xorg()
{
    if [ -e "${MHWDXORGCONF}" ]; then
        rm "${MHWDXORGCONF}"
    fi

    if [ -e "$1" ]; then
        ln -sf "$1" "${MHWDXORGCONF}"
        echo "xorg configuration file: '$1'"
    else
        echo "warning: could not find '$1'!"
    fi
}

print_link_destination_if_exists()
{
    if [ -e "$1" ]; then
        echo "$2 '$(readlink "$1")'"
    else
        echo "warning: could not find '$1'!"
    fi
}

print_status()
{
    echo ":: status"

    print_link_destination_if_exists "${MHWDXORGCONF}" "  xorg configuration file:"
}

print_help()
{
    echo "mhwd-gpu [OPTION] [...]"
    echo ""
    echo "   --help                      show help"
    echo "   --status                    show current status"
    echo "   --check                     check for invalid symlinks and repair"
    echo "   --setxorg [PATH]            set xorg configuration file"
    echo "   --setmod [nvidia/catalyst]  set modules"
    echo ""
}

if [ "${PARAM}" -lt 1 ]; then
    print_status
    exit 0
fi

for (( I=1; $I <= $PARAM; I++ ));do
    case "$1" in
        --help)
            print_help
            exit 0
        ;;
        --status)
            print_status
            exit 0
        ;;
        --check)
            CHECKCONFIG="true"
        ;;
        --setxorg)
            shift
            SETXORGCONF="$1"
        ;;
        --setmod)
            shift
            SETMOD="$1"
        ;;
        "") ;;
        *)
            echo "error: invalid argument: $1"
            echo ""
            print_help
            exit 1
        ;;
    esac

    shift
done

# Check root
if [[ $EUID -ne 0 ]]; then
   echo "error: you cannot perform this operation unless you are root." 1>&2
   exit 1
fi

# Set Modules
if [ "${SETMOD}" == "nvidia" ]; then
    set_modules "nvidia" "nouveau ttm drm_kms_helper drm"
elif [ "${SETMOD}" == "catalyst" ] || [ "${SETMOD}" == "ati" ]; then
    set_modules "fglrx" "radeon"
elif [ "${SETMOD}" != "" ]; then
    echo "error: invalid argument '${SETMOD}'"
    exit 1
fi

# Set xorg configuration file
if [ "${SETXORGCONF}" != "" ]; then
    set_xorg "${SETXORGCONF}"
fi

# Check config
if [ "${CHECKCONFIG}" == "true" ]; then
    if [ -L "${MHWDXORGCONF}" -a ! -e "${MHWDXORGCONF}" ]; then
        echo "'${MHWDXORGCONF}' symlink is invalid! Removing it..."
        rm "${MHWDXORGCONF}"
    elif [ -e "${MHWDXORGCONF}" ]; then
        echo "xorg configuration symlink valid..."
    fi
fi
