#!/bin/zsh

sym_link() {
    [[ ! -e "$1" ]] && echo "skipping link, target does not exist: $1" && return
    [[ -h "$2" ]] && rm $2
    [[ -f "$2" || -d "$2" ]] && rm -rf $2
    ln -s $1 $2
}

echo "---- configuring themes ----------------"
. ./src_files/shell/profile

echo "copying theme imports into config dirs"
omarchy_themes=(`find src_files/imports/themes-omarchy-* -mindepth 1 -maxdepth 1 -type d`)
for dir in $omarchy_themes; do
    target_theme_dir="$DIR_THEME_SETTINGS/$(basename $dir)"
    [[ ! -d "$target_theme_dir/wallpaper" ]] && mkdir -p "$target_theme_dir/wallpaper"
    dir_theme_files=(`find $dir -mindepth 1 -maxdepth 1 -type f`)
    [[ -d "$dir/backgrounds" ]] && cp -RPp $dir/backgrounds/* $target_theme_dir/wallpaper/
    for f in $dir_theme_files; do cp -RPp $f $target_theme_dir/; done
done

nvim_themes_dir=$XDG_CONFIG_HOME/nvim/themes
[[ ! -d "$nvim_themes_dir/pina/colors" ]] && mkdir -p "$nvim_themes_dir/pina/colors"
cp -p src_files/imports/themes-omarchy-extra/pina/pina.nvim/colors/pina.vim $nvim_themes_dir/pina/colors/

echo "setting sym-links for theme-switching"
sym_link $DIR_THEME_SETTINGS/$DEFAULT_THEME_NAME $DIR_THEME_SETTINGS/.current-theme
sym_link $DIR_THEME_SETTINGS/.current-theme/kitty.conf $XDG_CONFIG_HOME/kitty/theme.conf
sym_link $DIR_THEME_SETTINGS/.current-theme/tmux.conf $XDG_CONFIG_HOME/tmux/theme.conf
sym_link $DIR_THEME_SETTINGS/.current-theme/neovim.lua $XDG_CONFIG_HOME/nvim/current-theme
# TODO: set additional links per theme-set script

echo "setting the default theme: $DEFAULT_THEME_NAME"
$DIR_SCRIPTS/theme-set $DEFAULT_THEME_NAME

