#! /bin/bash --
# ex: set ts=4 sw=4 ft=sh

# Keymap
setxkbmap -layout us

# Enable Touchpad While Typing
# xinput |
# 	grep Touchpad |
# 	awk '{print $6}' |
# 	sed "s/id=//" - |
# 	{
# 		read iid; xinput list-props $iid |
# 		grep "Typing Enabled (" |
# 		awk '{gsub(/\(|\)|:/,""); print $6}' |
# 		xargs -I pid xinput set-prop "$iid" pid 0;
# 	}

pathvarprepend () {
	# prepending paths to pathvar denoted by the expansion of the PATHVAR parameter
	# if it's already in the PATH, move it to the end
	# POSIX compliant version

	test -n "$2" ||
		{ echo "Usage: pathvarprepend PATHVAR PATH_TO_ADD [PATH_TO_ADD...]";
		echo "Example: pathvarprepend LD_LIBRARY_PATH '$HOME/.local/lib' '/usr/local/lib'";
		return 2; }

	pathvar=$1
	shift

	case $pathvar in (*[!abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]*|""|[0123456789]*) false;; esac ||
		{ echo 'Expanded pathvar is not a valid name/variable identifier'; return 3; }

	if [ "$pathvar" = "PATH" ]; then
		test "${-#*r}" = $- ||
			{ echo 'Restricted shell, cannot change PATH'; return 4; }
	fi

	path_prepend_error=0

	# Thanks Stephane
	code="set -- dummy"
	n=$#
	while [ "$n" -gt 0 ]; do
		code="$code \"\${$n}\""
		n=$((n - 1))
	done
	eval "$code"

	while shift; [ $# -gt 0 ]; do
		norm_path_to_add=$1

		test "${norm_path_to_add#*:}" = "$norm_path_to_add" ||
			{ echo 'Cannot add path with colon'; path_prepend_error=1; continue; }

		test -d "$norm_path_to_add" ||
			{ echo "path_to_add ('$norm_path_to_add') not a directory"; path_prepend_error=1; continue; }

		norm_path=$(printf %s ":$(eval "printf %s "'"'"\$$pathvar"'"'):" | head -n 1 | sed 's|/\+|/|g; s/\/$//; s/:/::/g') # fence with colons, ensure one line, deduplicate slashes, trim trailing, duplicate colons
		norm_path_to_add=$(printf %s "$norm_path_to_add" | head -n 1 | sed 's|/\+|/|g; s/\/$//') # ensure one line, deduplicate slashes, trim trailing
		exec 3<<- 'EOF'
			# escape BRE meta-characters
			s/\\/\\./g # backslash first
			s/\./\\./g
			s/\^/\\^/g
			s/\$/\\$/g
			s/\*/\\*/g
			s/\[/\\[/g
			s|/|\\/|g # escape delimiter for outer sed
		EOF
		norm_path=$(printf %s "$norm_path" | sed "s/:$(printf %s "$norm_path_to_add" | sed -f /proc/self/fd/3 3<&3)://g") # remove all instances of PATH_TO_ADD from PATH
		exec 3<&-
		norm_path=$(printf %s "$norm_path" | sed 's/:\+/:/g; s/^://; s/:$//') # deduplicate colons, trim leading and trailing
		eval "$pathvar=\$norm_path_to_add\${norm_path:+:\$norm_path}" # prepend with colon
	done
	return $path_prepend_error
}

pathvarappend () {
	# appending paths to pathvar denoted by the expansion of the PATHVAR parameter
	# if it's already in the PATH, move it to the end
	# POSIX compliant version

	test -n "$2" ||
		{ echo "Usage: pathappend PATHVAR PATH_TO_ADD [PATH_TO_ADD...]";
		echo "Example: pathappend LD_LIBRARY_PATH '$HOME/.local/lib' '/usr/local/lib'";
		return 2; }

	pathvar=$1

	case $pathvar in (*[!abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]*|""|[0123456789]*) false;; esac ||
		{ echo 'Expanded pathvar is not a valid name/variable identifier'; return 3; }

	if [ "$pathvar" = "PATH" ]; then
		test "${-#*r}" = $- ||
			{ echo 'Restricted shell, cannot change PATH'; return 4; }
	fi

	path_append_error=0

	while shift; [ $# -gt 0 ]; do
		norm_path_to_add=$1

		test "${norm_path_to_add#*:}" = "$norm_path_to_add" ||
			{ echo 'Cannot add path with colon'; path_append_error=1; continue; }

		test -d "$norm_path_to_add" ||
			{ echo "path_to_add ('$norm_path_to_add') not a directory"; path_append_error=1; continue; }

		norm_path=$(printf %s ":$(eval "printf %s "'"'"\$$pathvar"'"'):" | head -n 1 | sed 's|/\+|/|g; s/\/$//; s/:/::/g') # fence with colons, ensure one line, deduplicate slashes, trim trailing, duplicate colons
		norm_path_to_add=$(printf %s "$norm_path_to_add" | head -n 1 | sed 's|/\+|/|g; s/\/$//') # ensure one line, deduplicate slashes, trim trailing
		exec 3<<- 'EOF'
			# escape BRE meta-characters
			s/\\/\\./g # backslash first
			s/\./\\./g
			s/\^/\\^/g
			s/\$/\\$/g
			s/\*/\\*/g
			s/\[/\\[/g
			s|/|\\/|g # escape delimiter for outer sed
		EOF
		norm_path=$(printf %s "$norm_path" | sed "s/:$(printf %s "$norm_path_to_add" | sed -f /proc/self/fd/3 3<&3)://g") # remove all instances of PATH_TO_ADD from PATH
		exec 3<&-
		norm_path=$(printf %s "$norm_path" | sed 's/:\+/:/g; s/^://; s/:$//') # deduplicate colons, trim leading and trailing
		eval "$pathvar=\${norm_path:+\$norm_path:}\$norm_path_to_add" # append with colon
	done
	return $path_append_error
}

path_append () {
	pathvarappend PATH "$@"
}

ld_lib_path_append () {
	pathvarappend LD_LIBRARY_PATH "$@"
}

cdpath_append () {
	pathvarappend CDPATH "$@"
}

path_prepend () {
	pathvarprepend PATH "$@"
}

ld_lib_path_prepend () {
	pathvarprepend LD_LIBRARY_PATH "$@"
}

cdpath_prepend () {
	pathvarprepend CDPATH "$@"
}

# shellcheck disable=SC2155
export XDG_RUNTIME_DIR="/run/user/$(id -u)"
export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/ssh-agent.socket"
# export SUDO_ASKPASS="/usr/bin/sudo-askpass"
# export SSH_ASKPASS="/usr/bin/sudo-askpass"
# export SSH_ASKPASS_REQUIRE="prefer"
export GIT_CONFIG_GLOBAL="$HOME/.gitconfig"
# shellcheck disable=SC2155
export GPG_TTY="$(tty)"
export _JAVA_AWT_WM_NONREPARENTING="1"

export USER42="tischmid"
export EMAIL42="timo42@proton.me"
export MAIL="timo42@proton.me"

export GOPATH="$HOME/go"

# ld_lib_path_append \
# 	"${HOME}/.local/lib"
# export LD_LIBRARY_PATH

path_prepend \
	"/bin" \
	"/sbin" \
	"/usr/bin" \
	"/usr/sbin" \
	"/usr/local/bin" \
	"/usr/local/sbin" \
	"/usr/games" \
	"/usr/local/games" \
	"/snap/bin" \

path_append \
	"${HOME}/bin" \
	"${HOME}/.bin" \
	"${HOME}/.local/bin" \
	"${HOME}/.local/sbin" \
	"${HOME}/.brew/bin" \
	"${GOPATH}/bin"
export PATH

# add cargo bin to path
if [ -f "$HOME/.cargo/env" ] || [ -r "$HOME/.cargo/env" ]; then
	# shellcheck disable=SC1091
	. "$HOME/.cargo/env"
fi

export NVM_DIR="$HOME/.nvm"
# shellcheck disable=SC1091
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
# shellcheck disable=SC1091
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"

# xrandr --setprovideroutputsource 1 0 --setprovideroutputsource 2 0

# Configure Displays
three-mon
# xrandr --newmode "1600x900_60.00"  119.00  1600 1696 1864 2128  900 901 904 932  -HSync +Vsync
# xrandr --addmode DVI-I-1-1 "1600x900_60.00"
# xrandr --above DP-1 --output DVI-I-1-1 --auto --mode "1600x900_60.00" --left-of DP-1

# Wallpaper Diashows
# wp-dia hor eDP-1 10m
# wp-dia wp-repo-1 HDMI-1 10m
# wp-dia wp-repo-1 DP-1 10m

updatebar

picom &

nohup startlxde 2>/dev/null 1>&2 & disown
while true ; do
	dwm 2>>/home/tosuman/.xinit.log 1>&2
	sleep 0.5
done
