#!/bin/sh
# shellcheck disable=SC1090,SC2154

# Usage statement
usage() {
	printf '%s\n' "usage: ${0##*/} theme"
}

# Convert hex colors into rgb
hex2rgb() {
	hex=$1
	hex_r=${hex%????}
	hex_g=${hex#??}; hex_g=${hex_g%??}
	hex_b=${hex#????}

	printf '%d,%d,%d' "0x$hex_r" "0x$hex_g" "0x$hex_b"
}

# Set directory variables
[ "$THM_CONFIG_DIR" ] \
	&& conf_dir="$THM_CONFIG_DIR" \
	|| conf_dir="${XDG_CONFIG_HOME:-$HOME/.config}/thm"

[ "$THM_DEST_DIR" ] \
	&& dest_dir="$THM_DEST_DIR" \
	|| dest_dir="${XDG_STATE_HOME:-$HOME/.local/state}/thm"

err() {
	[ "$*" ] && err_msg="$*" || err_msg="error"
	printf '%s%b' "${0##*/}: " "$err_msg\n" 1>&2
	exit 1
}

# Ensure the theme file exist and source it
case $1 in
	-h|h|--help|help ) usage; exit 0 ;;
	* ) [ "$1" ] || err "missing argument"
	    [ -f "$conf_dir/themes/$1.theme" ] || err "$1: theme not found"
	    theme_file="$conf_dir/themes/$1.theme"
	    . "$theme_file" ;;
esac

# Create RGB colors from the hex colors
bg_color_rgb="$(hex2rgb "$bg_color")"
fg_color_rgb="$(hex2rgb "$fg_color")"
color0_rgb="$(hex2rgb "$color0")"
color1_rgb="$(hex2rgb "$color1")"
color2_rgb="$(hex2rgb "$color2")"
color3_rgb="$(hex2rgb "$color3")"
color4_rgb="$(hex2rgb "$color4")"
color5_rgb="$(hex2rgb "$color5")"
color6_rgb="$(hex2rgb "$color6")"
color7_rgb="$(hex2rgb "$color7")"
color8_rgb="$(hex2rgb "$color8")"
color9_rgb="$(hex2rgb "$color9")"
color10_rgb="$(hex2rgb "$color10")"
color11_rgb="$(hex2rgb "$color11")"
color12_rgb="$(hex2rgb "$color12")"
color13_rgb="$(hex2rgb "$color13")"
color14_rgb="$(hex2rgb "$color14")"
color15_rgb="$(hex2rgb "$color15")"

# Make sure the dest_dir and dest_dir/thm_old exist
[ -d "$dest_dir/thm_old" ] || mkdir -p "$dest_dir/thm_old"

# Try and empty the dest_dir before populating it
mv "$dest_dir/"* "$dest_dir/thm_old" 2> /dev/null

# Make a file with the name of the new theme
printf '%s\n' "$theme_file" > "$dest_dir/current_thm"

# Repalce template syntax with the actual colors
for t in "${conf_dir}/templates/"*".template"
do
	file_name=${t##*/}
	file_name=${file_name%.template}

	sed "
	s/{bg_color}/$bg_color/g
	s/{fg_color}/$fg_color/g
	s/{color0}/$color0/g
	s/{color1}/$color1/g
	s/{color2}/$color2/g
	s/{color3}/$color3/g
	s/{color4}/$color4/g
	s/{color5}/$color5/g
	s/{color6}/$color6/g
	s/{color7}/$color7/g
	s/{color8}/$color8/g
	s/{color9}/$color9/g
	s/{color10}/$color10/g
	s/{color11}/$color11/g
	s/{color12}/$color12/g
	s/{color13}/$color13/g
	s/{color14}/$color14/g
	s/{color15}/$color15/g
	s/{bg_color.rgb}/$bg_color_rgb/g
	s/{fg_color.rgb}/$fg_color_rgb/g
	s/{color0.rgb}/$color0_rgb/g
	s/{color1.rgb}/$color1_rgb/g
	s/{color2.rgb}/$color2_rgb/g
	s/{color3.rgb}/$color3_rgb/g
	s/{color4.rgb}/$color4_rgb/g
	s/{color5.rgb}/$color5_rgb/g
	s/{color6.rgb}/$color6_rgb/g
	s/{color7.rgb}/$color7_rgb/g
	s/{color8.rgb}/$color8_rgb/g
	s/{color9.rgb}/$color9_rgb/g
	s/{color10.rgb}/$color10_rgb/g
	s/{color11.rgb}/$color11_rgb/g
	s/{color12.rgb}/$color12_rgb/g
	s/{color13.rgb}/$color13_rgb/g
	s/{color14.rgb}/$color14_rgb/g
	s/{color15.rgb}/$color15_rgb/g
	" "$t" > "${dest_dir}/${file_name}"
done

# This allows for different configuration for different color schemes.
# Configuration for one theme may not work very well for another theme. To use
# this set theme_type in the theme file to any string and place theme specific
# configuration in `${XDG_CONFIG_HOME:-$HOME/.config}/thm/templates/$theme_type`.
# If not specified, only templates in the base template directory will be generated.
# This will overwrite previously generated template files.
[ "$theme_type" ] && [ -d "${conf_dir}/templates/$theme_type" ] && \
	for t in "${conf_dir}/templates/$theme_type/"*".template"
	do
		file_name=${t##*/}
		file_name=${file_name%.template}

		sed "
		s/{bg_color}/$bg_color/g
		s/{fg_color}/$fg_color/g
		s/{color0}/$color0/g
		s/{color1}/$color1/g
		s/{color2}/$color2/g
		s/{color3}/$color3/g
		s/{color4}/$color4/g
		s/{color5}/$color5/g
		s/{color6}/$color6/g
		s/{color7}/$color7/g
		s/{color8}/$color8/g
		s/{color9}/$color9/g
		s/{color10}/$color10/g
		s/{color11}/$color11/g
		s/{color12}/$color12/g
		s/{color13}/$color13/g
		s/{color14}/$color14/g
		s/{color15}/$color15/g
		s/{bg_color.rgb}/$bg_color_rgb/g
		s/{fg_color.rgb}/$fg_color_rgb/g
		s/{color0.rgb}/$color0_rgb/g
		s/{color1.rgb}/$color1_rgb/g
		s/{color2.rgb}/$color2_rgb/g
		s/{color3.rgb}/$color3_rgb/g
		s/{color4.rgb}/$color4_rgb/g
		s/{color5.rgb}/$color5_rgb/g
		s/{color6.rgb}/$color6_rgb/g
		s/{color7.rgb}/$color7_rgb/g
		s/{color8.rgb}/$color8_rgb/g
		s/{color9.rgb}/$color9_rgb/g
		s/{color10.rgb}/$color10_rgb/g
		s/{color11.rgb}/$color11_rgb/g
		s/{color12.rgb}/$color12_rgb/g
		s/{color13.rgb}/$color13_rgb/g
		s/{color14.rgb}/$color14_rgb/g
		s/{color15.rgb}/$color15_rgb/g
		" "$t" > "${dest_dir}/${file_name}"
	done

# Run extra user scripts
[ -d "$conf_dir/scripts" ] || exit 0
for i in "$conf_dir/scripts/"*
do [ -x "$i" ] && $i > /dev/null 2>&1 &
done
