#!/bin/bash

    # This program is free software: you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation, either version 3 of the License, or
    # (at your option) any later version.

    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.

    # You should have received a copy of the GNU General Public License
    # along with this program.  If not, see <http://www.gnu.org/licenses/>.

# README for dependencies
# -----------------------

# Builds theme files for all items in the Tempus Themes collection and
# places them in their corresponding location inside my dotfiles.  For
# this to work, there are two prerequisites.
#
# 1. My dotfiles are in the home directory.  See
#    https://gitlab.com/protesilaos/dotfiles
# 2. The Tempus Themes generator is also at the home dir.  See:
#    https://gitlab.com/protesilaos/tempus-themes-generator
# 
# The utility of this script is only realised when I modify the source
# of the existing Tempus Themes and/or add a new theme (new templates
# would require modifications to this script).  It makes it easy to
# incorporate those changes to my dots.
#
# Note that this script only builds files that are included with my
# dotfiles.  The Tempus Themes generator has templates for programs that
# I do not actively use, such as Tilix and Konsole. If you wish to
# contribute to the Tempus Themes project, see the generator's
# CONTRIBUTING.md.
#
# Last full review on 2019-05-07

# The generator path
generator_dir="$HOME/tempus-themes-generator"

# The generator script inside the generator directory
generator="$generator_dir/tempus-themes-generator.sh"

# Define the array with all the available schemes
schemes=()
while IFS=  read -r -d $'\0' item; do
	schemes+=("${item##*/}")
done < <(find "$generator_dir/schemes" -type f -print0)

# Destination path used by some functions
local_col_dir="$HOME/.local/share/my_colours"

[ -d "$generator_dir" ] || { echo "The Tempus themes generator is not in the home dir!"; exit 1; }
[ -d "$HOME/dotfiles" ] && [ -d "$local_col_dir" ] || { echo "Could not find destination directories!"; exit 1; }

# This is the generator's command.  The script accepts two arguments.
# The first is the name of the scheme.  The second is the template.  For
# example, the following will output the Winter colours for Vim to
# stdout:
#
# ./tempus-themes-generator.sh winter vim
#
# This can then be redirected into a file.
#
# As such, the `invoke_generator()` accepts a total of four arguments
# from the subsequent functions.  In order, these pass the following
# information:
#
# 1. Scheme name
# 2. Template name
# 3. Destination path
# 4. Name of output file
invoke_generator() {
	# The generator's script can only be invoked from inside the
	# generator's directory.  This is to ensure that all relative paths
	# point to the right location.
	cd "$generator_dir" || exit 1
    "$generator" "$1" "$2" > "$3"/tempus_"$4"
}

# As there are a number of Tempus schemes, we will run a loop that
# iterates over each of them.  This executes the `invoke_generator()`,
# passing to it the scheme name which is needed for that function's
# arguments 1 and 4 (see its description).
#
# The `invoke_generic_loop()` accepts threee arguments from subsequent
# functions.  These define the following:
#
# 1. Template name
# 2. Destination path
# 3. File type extension
invoke_generic_loop() {
    for i in ${schemes[*]}; do
        invoke_generator "$i" "$1" "$2" "$i"."$3"
    done
}

# Function to build all Tempus themes for GTK{3,4} source view (syntax
# highlighting for GNOME Builder, Gedit, Pluma, Xed, Mousepad, etc.).
# It starts by invoking the generic loop that iterates over each scheme
# file.  Then we pass to it the following arguments:
#
# 1. The 'gtksourceview{3,4}' template name
# 2. Destination path for $HOME/.local/share/gtksourceview-{3.0,4}/styles
# 3. File type extension for tempus_$i.xml
build_gtksourceview3() {
    local dest_dir="$HOME/.local/share/gtksourceview-3.0/styles"
    mkdir -p "$dest_dir"
    invoke_generic_loop gtksourceview3 "$dest_dir" xml
}

build_gtksourceview4() {
    local dest_dir="$HOME/.local/share/gtksourceview-4/styles"
    mkdir -p "$dest_dir"
    invoke_generic_loop gtksourceview4 "$dest_dir" xml
}

# Function to build all Tempus themes for the shell.  It starts by
# invoking the generic loop that iterates over each scheme file.  Then
# we pass to it the following arguments:
#
# 1. The 'shell-variables' template name
# 2. Destination path for $HOME/.local/share/my_colours/shell
# 3. File type extension for tempus_$i.sh
build_shell() {
    local dest_dir="$local_col_dir/shell"
    mkdir -p "$dest_dir"
    invoke_generic_loop shell-variables "$dest_dir" sh
}

# Function to build all Tempus themes for Vim.  It starts by
# invoking the generic loop that iterates over each scheme file.  Then
# we pass to it the following arguments:
#
# 1. The 'vim' template name
# 2. Destination path for $HOME/.vim/colors
# 3. File type extension for tempus_$i.vim
build_vim() {
    local dest_dir="$HOME/.vim/colors"
    mkdir -p "$dest_dir"
    invoke_generic_loop vim "$dest_dir" vim
}

# Function to build all Tempus themes for XTerm.  It starts by
# invoking the generic loop that iterates over each scheme file.  Then
# we pass to it the following arguments:
#
# 1. The 'xterm' template name
# 2. Destination path for $HOME/.local/share/my_colours/xcolors
# 3. File type extension for tempus_$i.Xcolors
build_xterm() {
    local dest_dir="$local_col_dir/xcolors"
    mkdir -p "$dest_dir"
    invoke_generic_loop xterm "$dest_dir" Xcolors
}

# return results
build_gtksourceview3
build_gtksourceview4
build_shell
build_vim
build_xterm
