#!/usr/bin/env sh

# shellcheck source=/dev/null
. "$(realpath "$(dirname "$0")")/shell_line_editor.sh"

replacement_file="$(mktemp)"

trap cleanup INT
trap "cleanup; remove_self" EXIT
cleanup() {
    rm "$replacement_file"
}
avoid_cleanup="false"
remove_self() {
    if [ "$avoid_cleanup" = "false" ]; then
        rm "$(realpath "$0")"
        rm "$(realpath "$(dirname "$0")")/shell_line_editor.sh"
        [ -f "$(realpath "$(dirname "$0")")/init.local" ] && rm "$(realpath "$(dirname "$0")")/init.local"
    fi
}
require() {
    program="$1"
    if ! command -v "$program" >/dev/null; then
        avoid_cleanup="true"
        echo "Please install '$program' for this init script to work." 1>&2
        exit 1
    fi
}
die() {
    printf "init: ERROR: %s\n" "$1"
    exit 1
}

# Prompt the user for a specific variable.
# ## Args:
# [1]: Name of the variable to populate the answer to
# [2]: An optional description
# [3]: An optionally suggested answer
# [4]: If this is set, the user is not even asked.
prompt() {
    pr_variable_upper="$(echo "$1" | sed 's/\([a-z]\)/\U\1/')"
    pr_description="$2"
    pr_suggested_answer="$3"
    pr_ask="$4"

    if [ -n "$pr_ask" ]; then
        REPLY="$pr_suggested_answer"
    else
        printf "\033[94;1mEnter %s\033[0m" "$pr_variable_upper"
        if [ -n "$pr_description" ]; then
            printf " (\033[93;1m%s\033[0m):\n" "$pr_description"
        else
            printf ":\n"
        fi

        # LE "> " 0 " " "$pr_suggested_answer" "yes_please_produce_debug_output"
        LE "> " 0 " " "$pr_suggested_answer" ""
    fi

    pr_new_variable="$(printf '%s="%s"' "$pr_variable_upper" "$REPLY")"

    eval "$pr_new_variable"
    printf "%s\n" "$pr_new_variable" >>"$replacement_file"
}

require git

require jq
require curl

require fd
require mv
require sed
require chmod

# necessary meta data
prompt APPLICATION_NAME "The name of the application" "$(basename "$PWD")"
prompt APPLICATION_NAME_STYLIZED "The stylized name of the application (for documentation)" "$(echo "$APPLICATION_NAME" | sed 's/[_-]/ /g' | sed 's/^\(\w\)/\U\1/g' | sed 's/ \(\w\)/ \U\1/g')"
prompt APPLICATION_NAME_CAPITALIZED_MAN_PART "The capitalized name of the application (for documentation also with a man section part)" "$(echo "$APPLICATION_NAME" | sed 's/_/-/g' | sed 's/\(.*\)/\U\1(1)/')" "dont_ask"
prompt APPLICATION_VERSION "The version of this program, without the prefix" "0.1.0"

prompt AUTHOR_NAME "The name of the author (or authors)" "$(git config --get user.name)"
prompt AUTHOR_EMAIL "The email of the author (or authors)" "$(git config --get user.email)"

# cog change-log variables
prompt REMOTE "The remote, this project will be pushed to" "git.vhack.eu"
prompt REPOSITORY "The path of the repository on the remote" "$APPLICATION_NAME"

owner1="$(printf "%.1s" "$AUTHOR_NAME")"
owner2="${AUTHOR_NAME#* }"
if [ "$owner2" = "$AUTHOR_NAME" ]; then
    owner_build="$(echo "$AUTHOR_NAME" | tr '[:upper:]' '[:lower:]')"
else
    owner_build="$(echo "$owner1$owner2" | tr '[:upper:]' '[:lower:]')"
fi
prompt OWNER "The name of owner of the repository" "$owner_build"

# nice meta data
prompt DESCRIPTION "The description of this project" ""
prompt CURRENT_DATE "The stylized version of the current date" "$(date +'%b %Y')"
prompt YEAR "The year the work on this has begun (for copyright reasons)" "$(date +'%Y')"
prompt APPLICATION_SOURCE_CODE_REPOSITORY "The package's source code repository URL" "https://$REMOTE/$OWNER/$REPOSITORY"
prompt HOME_PAGE "The home page URL of the project" "https://$REPOSITORY.org/"
prompt BUG_URL "The URL people should report bugs to" "$APPLICATION_SOURCE_CODE_REPOSITORY/issues"

# git stuff
prompt PUSH_URL "The url used to push this project" "git@$REMOTE:$OWNER/$REPOSITORY.git"
prompt CLONE_URL "The url used to clone this project" "git@$REMOTE:$OWNER/$REPOSITORY.git"
prompt MAIN_BRANCH "The name of the main branch" "$(git config init.defaultbranch)"

if [ -e ./watch.sh ]; then
    # Use a different default license in latex projects.
    init_default_license="CC-BY-SA-4.0"
else
    init_default_license="AGPL-3.0-or-later"
fi
prompt SPDX_LICENSE_IDENTIFIER "THE SPDX identifer of your choosen license" "$init_default_license"

default_license_url="$(curl --silent --show-error "https://spdx.org/licenses/$SPDX_LICENSE_IDENTIFIER.json" | jq --raw-output '.seeAlso[0]')"

# Prefer possible text versions of the license
if curl --fail --silent --show-error "$default_license_url.txt" >/dev/null; then
    default_license_url="$default_license_url.txt"
fi
prompt LICENSE_URL "The url of the license" "$default_license_url"

if [ -e ./.reuse/templates/default.jinja2 ]; then
    if [ -n "$DESCRIPTION" ]; then
        description_str=" - $DESCRIPTION"
    else
        description_str=""
    fi

    cat <<EOF | fmt --uniform-spacing --width=85 --split-only >./.reuse/templates/default.jinja2
{% for copyright_line in copyright_lines %}
{{ copyright_line }}
{% endfor %}
{% for contributor_line in contributor_lines %}
SPDX-FileContributor: {{ contributor_line }}
{% endfor %}
{% for expression in spdx_expressions %}
SPDX-License-Identifier: {{ expression }}
{% endfor %}

This file is part of $APPLICATION_NAME_STYLIZED$description_str.

You should have received a copy of the License along with this program.
If not, see <$LICENSE_URL>.
EOF
fi

# Allow templates to add template specific prompts
[ -e init.local ] && . ./init.local

git init --initial-branch "$MAIN_BRANCH" --template="./git_template"
rm --recursive ./git_template

while read -r var; do
    var_name="${var%=*}"
    var_value="${var#*=\"}"
    var_value="${var_value%\"}"

    fd . --hidden --type file --exec sed --in-place "s|%\bINIT_$var_name\b|$var_value|g"

    # Replace the variable in file paths
    fd "%INIT_$var_name" . --hidden | while read -r file_path; do
        new_file_path="$(echo "$file_path" | sed "s|%INIT_$var_name|$var_value|g")"
        mv "$file_path" "$new_file_path"
    done
done <"$replacement_file"

# HACK: Re-add the executable permissions to files, which the nix template has somehow
# removed <2024-04-02>
chmod +x scripts/*
chmod +x update.sh
[ -f ./build.sh ] && chmod +x build.sh
[ -f ./watch.sh ] && chmod +x build.sh

# Do the first run of reuse annotate
reuse="$(nix build nixpkgs#reuse.out --no-link --print-out-paths)/bin/reuse"
[ "$(echo "$reuse" | wc -l)" -ne 1 ] && die "Something is wrong with the nixpkgs#reuse derivation"

git add .

cp "$replacement_file" ".git/init_variables_$(date --iso-8601=date)"

git diff --name-only --cached |
    xargs -I {} "$reuse" annotate \
        --copyright "$AUTHOR_NAME <$AUTHOR_EMAIL>" \
        --copyright-style string-c \
        --template default \
        --license "$SPDX_LICENSE_IDENTIFIER" "{}"

# vim: ft=sh