#!/usr/bin/env bash

# Adapted from https://github.com/ThePrimeagen/dev/blob/master/dev-env
#
# Copies configuration files to their appropriate places.

dry_run="0"

if [ -z "$XDG_CONFIG_HOME" ]; then
	echo "no xdg config home"
	echo "using ~/.config"
	XDG_CONFIG_HOME=$HOME/.config
fi

if [ -z "$XDG_DATA_HOME" ]; then
	echo "no xdg data home"
	echo "using ~/.local/share"
	XDG_DATA_HOME=$HOME/.local/share
fi

if [ -z "$DEV_ENV" ]; then
	echo "env var DEV_ENV needs to be present"
	exit 1
fi

if [[ $1 =~ ^--dry ]]; then
	dry_run="1"
fi

log() {
	if [[ $dry_run == "1" ]]; then
		echo "[DRY_RUN]: $1"
	else
		echo "$1"
	fi
}

log "env: $DEV_ENV"

# Removes a destination directory and copies all files to the destination.
update_dirs() {
	log "copying over files from: $1"
	pushd $1 &>/dev/null
	(
		# Copy everything except systemd, share, and zsh folders, they need treated differently.
		configs=$(
			find . -mindepth 1 -maxdepth 1 -type d \
				\( -name "systemd" -o -name "zsh" -o -name "share" -o -name "scripts" -o -name "kanata" \) \
				-prune -o -type d -print
		)
		for c in $configs; do
			directory=${2%/}/${c#./}
			log "    removing: rm -rf $directory"

			if [[ $dry_run == "0" ]]; then
				rm -rf $directory
			fi

			log "    copying env: cp $c $2"
			if [[ $dry_run == "0" ]]; then
				cp -r ./$c $2
			fi
		done

	)
	popd &>/dev/null
}

# Removes a destination file and copies a single file to the destination.
copy() {
	log "removing: $2"
	if [[ $dry_run == "0" ]]; then
		rm $2 &>/dev/null
	fi
	log "copying: $1 to $2"
	if [[ $dry_run == "0" ]]; then
		cp $1 $2
	fi
}

# Copy all files from a directory into another directory.
copy_files() {
	log "copying over files from: $1"
	pushd "$1" &>/dev/null || exit 1
	(
		for f in $(find . -mindepth 1 -maxdepth 1 -type f); do

			declare dest
			dest="$2/$(basename "$f")"

			if [[ $dry_run == "0" ]]; then
				rm -rf $dest >/dev/null 2>&1
			fi

			log "    copying env: cp $f $dest"
			if [[ $dry_run == "0" ]]; then
				cp $f $dest
			fi
		done
	)
	popd &>/dev/null || exit 1
}

email() {
	if command -v mailctl; then
		log "Updating using mailctl..."
		[[ $dry_run == "0" ]] && mailctl update && mailctl config
	else
		log "Installing mailctl..."
		[[ $dry_run == "0" ]] && . "$DEV_ENV/mail/install"
	fi
}

############################## MAIN ##############################

# CONFIG
update_dirs $DEV_ENV/env/.config $XDG_CONFIG_HOME
update_dirs $DEV_ENV/env/.local $HOME/.local

# SCRIPTS
mkdir -p ~/.local/scripts/{hypr,utils} >/dev/null 2>&1
mkdir -p ~/.local/scripts/utils/{kanatactl,hpa} >/dev/null 2>&1
update_dirs $DEV_ENV/env/.local/scripts/hypr $HOME/.local/scripts/hypr
copy_files $DEV_ENV/env/.local/scripts/hypr $HOME/.local/scripts/hypr
copy_files "$DEV_ENV/env/.local/scripts/utils/kanatactl" "$HOME/.local/scripts/utils/kanatactl"
copy_files "$DEV_ENV/env/.local/scripts/utils/hpa" "$HOME/.local/scripts/utils/hpa"
copy_files $DEV_ENV/env/.local/scripts $HOME/.local/scripts

# SYSTEMD
mkdir -p $XDG_CONFIG_HOME/systemd/user >/dev/null 2>&1
copy_files $DEV_ENV/env/.config/systemd/user $XDG_CONFIG_HOME/systemd/user

# ZSH
# NOTE: This keeps from clobbering zsh history and plugins vs. wiping the entire directory and
#       copying configuration.
copy $DEV_ENV/env/.zshenv $HOME/.zshenv
mkdir -p $XDG_CONFIG_HOME/zsh >/dev/null 2>&1
update_dirs $DEV_ENV/env/.config/zsh $XDG_CONFIG_HOME/zsh # copies sup-directories.
copy_files $DEV_ENV/env/.config/zsh $XDG_CONFIG_HOME/zsh  # copies files in the root directory.

# TMUX
copy $DEV_ENV/env/.tmux.conf $HOME/.tmux.conf

# GPG
mkdir $HOME/.gnupg >/dev/null 2>&1
copy_files $DEV_ENV/env/.gnupg $HOME/.gnupg

# Wallpapers
mkdir $HOME/wallpapers >/dev/null 2>&1
copy_files $DEV_ENV/env/wallpapers $HOME/wallpapers

# MISC
mkdir $HOME/Pictures >/dev/null 2>&1
mkdir -p $XDG_DATA_HOME/clipse/tmp_files
copy $DEV_ENV/dev-env $HOME/.local/scripts/dev-env
copy $DEV_ENV/env/.markdownlint.jsonc $HOME/.makrdownlint.jsonc
copy $DEV_ENV/env/.prettierrc.yaml $HOME/.prettierrc.yaml

mkdir -p $XDG_DATA_HOME/applications/icons
copy_files $DEV_ENV/env/.local/share/applications $XDG_DATA_HOME/applications
copy_files $DEV_ENV/env/.local/share/applications/icons $XDG_DATA_HOME/applications/icons

# Custom package builds.
mkdir $HOME/pkgbuilds
update_dirs $DEV_ENV/env/pkgbuilds $HOME/pkgbuilds

# Email.
email

if [[ $dry_run == "0" ]]; then
	systemctl --user daemon-reload
	hyprctl reload
	espanso service restart
	exec zsh -l
fi
