#!/bin/sh

. /etc/control.d/functions
CONFDIR="/etc/lightdm"
PROFILES_DIR="$CONFDIR/profile.d"
SUMMARY="${SUMMARY:-LightDM profile selector}"

. shell-version
. shell-ini-config
shell_ini_config_prefix=''

new_summary "$SUMMARY"

read_desc() {
	local profile="$1"; shift
	sed -n -e '/^#/ {
		s/^#[[:space:]]*//;
		p;
		q
	}' -- "$profile"
}

for_each_profile() {
	local func="$1"; shift
	if [ -d $PROFILES_DIR ]; then
		for p in ${PROFILES_DIR%/}/*; do
			case "${p##*/}" in
				*~|.*|*.rpmnew|*.rpmold)
					;;
				*)
					"$func" "$p" "$@" || return $?
					;;
			esac
		done
	fi
}

register_profile() {
	new_help "${1##*/}" "$(read_desc "$1")"
}

_trim() {
    echo "$*" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
}

apply_profile() {
    local profile="$1"; shift
    local func="$1"; shift
    
    while read ln; do
        case "$ln" in
            \#*)
                continue
                ;;
            \[*\]*)
                ln="${ln%%\]*}"
                ln="${ln#*\[}"
                file="$CONFDIR/${ln%%:*}"
                sec="${ln#*:}"
                ;;
            *=*)
                if [ -z "$file" ]; then
                    echo "The profile doesn't set a file name" >&2
                    exit 1
                fi
                if [ ! -e "$file" ]; then
                    echo "File not found: $file" >&2
                    exit 2
                fi
                if [ -z "$sec" ]; then
                    echo "The profile doesn't set a section name" >&2
                    exit 3
                fi
                "$func" "$file" "$sec" "$(_trim "${ln%%=*}")" "$(_trim "${ln#*=}")" "$@" || return $?
                ;;
        esac
    done <"$profile"
}

_last_file=; _last_stats=
_restore_stats() {
    if [ -n "$_last_file" ]; then
        chmod "${_last_stats%%:*}" "$_last_file"
        chown "${_last_stats#*:}" "$_last_file"
    fi
}

_set_val() {
    local file="$1"; shift
    local sec="$1"; shift
    local var="$1"; shift
    local val="$1"; shift

    if [ "$_last_file" != "$file" ]; then
        _restore_stats
        _last_file="$file"
        _last_stats="$(stat -c '%04a:%u:%g' "$file")"
    fi
    ini_config_set "$file" "$sec" "$var" "$val"
    
}

use_profile() {
	local profile="${PROFILES_DIR%/}/$1"; shift
	if [ -e "$profile" ]; then
        _last_file=; _last_stats=
        apply_profile "$profile" _set_val
        _restore_stats
	else
		echo "ERROR: Profile not found: $1" >&2
		return 1
	fi
}

check_value() {
    local file="$1"; shift
    local sec="$1"; shift
    local var="$1"; shift
    local val0="$1"; shift
    
    local val="$(ini_config_get "$file" "$sec" "$var")"
    [ "$val0" = "$val" ]
}

test_profile () {
    local profile="$1"; shift
	if apply_profile "$profile" check_value "$@"; then
        echo "${profile##*/}"
        return 1 # break for_each_profile
    fi
}

read_current() {
    for_each_profile test_profile && echo 'custom' ||:
}

## Main

REQUEST="$*"

for_each_profile register_profile

case "$REQUEST" in
	help|'help '*)
		control_help "${REQUEST#help}"
		;;
	list)
		control_list
		;;
	summary)
		control_summary
		;;
	status)
		read_current
		;;
	*)
		use_profile "$REQUEST"
		;;
esac
