#!/usr/bin/env bash

live_state_directory=/tmp
live_state_default_config_directory=/etc/big-default-config
live_state_legacy_theme_path=/etc/default-theme-biglinux
live_state_legacy_desktop_path=/etc/big_desktop_changed

live_state_path() {
	case "$1" in
	language) printf '%s\n' "$live_state_directory/big_language" ;;
	keyboard) printf '%s\n' "$live_state_directory/big_keyboard" ;;
	desktop) printf '%s\n' "$live_state_directory/big_desktop_changed" ;;
	gnome-layout) printf '%s\n' "$live_state_directory/big_gnome_layout" ;;
	gnome-settings) printf '%s\n' "$live_state_directory/big_gnome_settings" ;;
	desktop-theme) printf '%s\n' "$live_state_directory/big_desktop_theme" ;;
	enable-jamesdsp) printf '%s\n' "$live_state_directory/big_enable_jamesdsp" ;;
	improve-display) printf '%s\n' "$live_state_directory/big_improve_display" ;;
	*) return 1 ;;
	esac
}

require_live_state_directory() {
	[[ -d $live_state_directory && ! -L $live_state_directory ]] || {
		printf 'biglinux-livecd: missing safe live state directory: %s\n' "$live_state_directory" >&2
		return 1
	}
}

write_live_state() {
	local name=$1 content=$2 temporary_file target
	case "$name" in
	language | keyboard | desktop | gnome-layout | gnome-settings | desktop-theme | enable-jamesdsp | improve-display) ;;
	*)
		printf 'biglinux-livecd: unsupported live state name: %s\n' "$name" >&2
		return 1
		;;
	esac
	require_live_state_directory || return 1
	target=$(live_state_path "$name") || return 1
	[[ ! -L $target ]] || return 1
	temporary_file=$(mktemp -- "$live_state_directory/.${name}.XXXXXX") || return 1
	if ! printf '%s' "$content" >"$temporary_file"; then
		rm -f -- "$temporary_file"
		return 1
	fi
	chmod 0600 -- "$temporary_file"
	sync -f -- "$temporary_file"
	mv -fT -- "$temporary_file" "$target"
	sync -f -- "$live_state_directory"
}

initialize_default_live_state() {
	write_live_state language en_US
	write_live_state desktop-theme breeze
	write_live_state desktop 'startkde-biglinux classic'
}

install_live_state_defaults() {
	local desktop_state theme_state
	require_live_state_directory || return 1
	theme_state=$(live_state_path desktop-theme) || return 1
	desktop_state=$(live_state_path desktop) || return 1
	[[ -f $theme_state && ! -L $theme_state ]] || return 1
	[[ -f $desktop_state && ! -L $desktop_state ]] || return 1

	sudo -n install -d -m 0755 -- "$live_state_default_config_directory"
	sudo -n install -m 0644 -- "$theme_state" "$live_state_default_config_directory/theme"
	sudo -n install -m 0644 -- "$desktop_state" "$live_state_default_config_directory/desktop"
	sudo -n install -m 0644 -- "$theme_state" "$live_state_legacy_theme_path"
	sudo -n install -m 0644 -- "$desktop_state" "$live_state_legacy_desktop_path"

	if [[ -f $(live_state_path enable-jamesdsp) && ! -L $(live_state_path enable-jamesdsp) ]]; then
		sudo -n install -m 0644 -- "$(live_state_path enable-jamesdsp)" "$live_state_default_config_directory/jamesdsp"
	else
		sudo -n rm -f -- "$live_state_default_config_directory/jamesdsp"
	fi
	if [[ -f $(live_state_path improve-display) && ! -L $(live_state_path improve-display) ]]; then
		sudo -n install -m 0644 -- "$(live_state_path improve-display)" "$live_state_default_config_directory/display-profile"
	else
		sudo -n rm -f -- "$live_state_default_config_directory/display-profile"
	fi
}
