about summary refs log tree commit diff stats
path: root/common/init
diff options
context:
space:
mode:
Diffstat (limited to 'common/init')
-rwxr-xr-xcommon/init71
1 files changed, 71 insertions, 0 deletions
diff --git a/common/init b/common/init
new file mode 100755
index 0000000..787b663
--- /dev/null
+++ b/common/init
@@ -0,0 +1,71 @@
+#!/usr/bin/env sh
+
+. "$(realpath "$(dirname "$0")")/shell_line_editor.sh"
+
+replacement_file="$(mktemp)"
+
+trap cleanup INT
+trap "cleanup; remove" EXIT
+cleanup() {
+    rm "$replacement_file"
+}
+remove() {
+    rm "$(realpath "$0")"
+    rm "$(realpath "$(dirname "$0")")/shell_line_editor.sh"
+    git add .
+}
+
+# Prompt the user for a specific variable. The variable is the first
+# input, the second is an optional description.
+# The third argument can be a suggested answer, already pre populated.
+prompt() {
+    pr_variable_upper="$(echo "$1" | sed 's/\([a-z]\)/\U\1/')"
+    pr_description="$2"
+    pr_suggested_answer="$3"
+
+    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" ""
+
+    pr_new_variable="$(printf '%s="%s"' "$pr_variable_upper" "$REPLY")"
+
+    eval "$pr_new_variable"
+    printf "%s\n" "$pr_new_variable" >> "$replacement_file"
+}
+
+git init
+
+# 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)" "$(basename "$PWD" | sed 's/\([a-z]\)/\u\1/')"
+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" "codeberg.org"
+prompt REPOSITORY "The name of the repository in the remote" "$APPLICATION_NAME"
+prompt OWNER "The name of owner of the repository" "$AUTHOR_NAME"
+
+# nice meta data
+prompt DESCRIPTION "The description of this project" "[can be empty]"
+
+# LICENSE.spdx data (source: https://github.com/david-a-wheeler/spdx-tutorial)
+prompt APPLICATION_ORIGINATOR "The person or organization from whom the package originally came" "$AUTHOR_NAME"
+prompt APPLICATION_HOME_PAGE "The package's home page URL" "https://$REMOTE/$OWNER/$REPOSITORY"
+
+echo "$DESCRIPTION" > .git/description
+
+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|"
+done < "$replacement_file"
+# vim: ft=sh