about summary refs log tree commit diff stats
path: root/common/files/init
blob: 3498d56f1b597d45b45339bee37eb58a5c1a7ef9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/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"
}
remove_self() {
    rm "$(realpath "$0")"
    rm "$(realpath "$(dirname "$0")")/shell_line_editor.sh"
}

# 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"
}

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)" "$(echo "$APPLICATION_NAME" | sed 's/\([a-z]*\)/\u\1/')"
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/\([a-z]*\)/\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" "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]"
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"

# LICENSE.spdx data (source: https://github.com/david-a-wheeler/spdx-tutorial)
if [ -e ./lpm.toml ]; then
    # Use a different default license in latex projects.
    init_default_license="CC-BY-SA-4.0"
else
    init_default_license="GPL-3.0-or-later"
fi
prompt SPDX_LICENSE_IDENTIFIER "THE SPDX identifer of your choosen license" "$init_default_license"
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 "Downloading license .."
case "$SPDX_LICENSE_IDENTIFIER" in
"AGPL-3.0-or-later")
    default_license_url="https://www.gnu.org/licenses/agpl-3.0.txt"
    curl "$default_license_url" >COPYING
    ;;
"GPL-3.0-or-later")
    default_license_url="https://www.gnu.org/licenses/gpl-3.0.txt"
    curl "$default_license_url" >COPYING
    ;;
"LGPL-3.0-or-later")
    default_license_url="https://www.gnu.org/licenses/lgpl+gpl-3.0.txt"
    curl "https://www.gnu.org/licenses/gpl-3.0.txt" >COPYING
    curl "https://www.gnu.org/licenses/lgpl-3.0-standalone.html" >COPYING.LESSER
    ;;

"Apache-2.0")
    default_license_url="https://www.apache.org/licenses/LICENSE-2.0.txt"
    curl "$default_license_url" >LICENSE
    ;;

"CC-BY-SA-4.0")
    default_license_url="https://creativecommons.org/licenses/by-sa/4.0/legalcode.txt"
    curl "$default_license_url" >LICENSE
    ;;

*)
    default_license_url="file:///dev/null"
    echo "    -> No license found for your identifier: '$SPDX_LICENSE_IDENTIFIER'"
    ;;
esac

prompt LICENSE_URL "The url of the license" "$default_license_url" "dont_ask"

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|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

# vim: ft=sh