about summary refs log tree commit diff stats
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/mk_key.sh74
-rwxr-xr-xscripts/renew_copyright_header.sh92
2 files changed, 166 insertions, 0 deletions
diff --git a/scripts/mk_key.sh b/scripts/mk_key.sh
new file mode 100755
index 0000000..2c98cc8
--- /dev/null
+++ b/scripts/mk_key.sh
@@ -0,0 +1,74 @@
+#!/usr/bin/env sh
+
+die() {
+    echo "$@";
+    exit 1;
+}
+
+help() {
+    cat << EOF
+A helper script to add an gpg key to the wkd.
+
+USAGE:
+    mk_key KEY_ID KEY_EMAIL_DOMAIN KEY_HASH
+
+OPTIONS:
+    --help | -h
+                    Display this help and exit.
+ARGUMENTS:
+    KEY_ID := [[ gpg --list-keys --with-colons | awk -F: '/^uid:/ { print \$10 }' ]]
+                    The key to use, as specified by the key id.
+
+    KEY_EMAIL_DOMAIN
+                    The email domain of this key. For example 'key@example.org' would
+                    have an KEY_EMAIL_DOMAIN of 'example.org'.
+
+    KEY_HASH
+                    This is the hashed name of the key as specified in the WKD spec.
+                    Get this by running: 'gpg --with-wkd-hash --list-keys <KEY_ID>'.
+EOF
+}
+
+add() {
+    key_id="$1";
+    key_email="$2";
+    key_hash="$3";
+
+    cd "$(git rev-parse --show-toplevel)" || die "No source dir!"
+
+    dir="./src/.well-known/openpgpkey/$key_email/hu"
+
+    full_key_id="$(gpg --list-keys --with-colons "$key_id" | awk -F: '/^uid:/ { print $10 }' | tail -n 1)"
+
+    mkdir --parents "$dir"
+    gpg --no-armor --export "$key_id" > "$dir/$key_hash" &&
+
+    printf "%s%s%s%s\n" '`' "$dir/$key_hash" '`' " -> $full_key_id" >> stored_keys.md &&
+
+    echo "Key export done!";
+}
+
+for arg in "$@"; do
+    case "$arg" in
+        "--help" | "-h")
+            help;
+            exit 0;
+            ;;
+    esac
+done
+
+
+key_id="$1";
+key_email="$2";
+key_hash="$3";
+shift 3
+
+[ -z "$key_id" ] && die "No KEY_ID specified, see '--help'!"
+[ -z "$key_email" ] && die "No KEY_EMAIL_DOMAIN specified, see '--help'!"
+[ -z "$key_hash" ] && die "No KEY_HASH specified, see '--help'!"
+
+[ -n "$*" ] && die "The arguments '$*' are not recognized; see '--help' for a list!"
+
+add "$key_id" "$key_email" "$key_hash"
+
+# vim: ft=sh
diff --git a/scripts/renew_copyright_header.sh b/scripts/renew_copyright_header.sh
new file mode 100755
index 0000000..737435b
--- /dev/null
+++ b/scripts/renew_copyright_header.sh
@@ -0,0 +1,92 @@
+#! /usr/bin/env sh
+
+# NOTE: This is the line length of the .licensure.yml header template **plus** the extra
+# line after the template comment.
+TEMPLATE_LINE_LENGTH=20
+LATEX_TEMPLATE_LINE_LENGTH=9
+
+PROJECT_ROOT="$(git rev-parse --show-toplevel)"
+
+remove() {
+    extension="$1"
+    file="$2"
+
+    # We need to differentiate, when removing the old copyright header, as some
+    # formatters do weird things to the file
+    case "$extension" in
+    # normal '#' comments (these are $TEMPLATE_LINE_LENGTH lines long)
+    "Makefile" | "toml" | "envrc" | "yml" | "gitignore" | "awk" | "pest")
+        sed --in-place "1,${TEMPLATE_LINE_LENGTH}d" "$file"
+        ;;
+    # LaTeX files (or TeX files in general) have a different license, use the
+    # $LATEX_TEMPLATE_LINE_LENGTH variable.
+    "tex")
+        sed --in-place "1,${LATEX_TEMPLATE_LINE_LENGTH}d" "$file"
+        ;;
+    # normal '/* ... */' like comments (these are $TEMPLATE_LINE_LENGTH + 2 lines long)
+    "c" | "h" | "md" | "rs" | "html")
+        length="$((TEMPLATE_LINE_LENGTH + 2))"
+        sed --in-place "1,${length}d;" "$file"
+        ;;
+    # alejandra (the nix formatter) removes the blank line after the comment,
+    # thus only $TEMPLATE_LINE_LENGTH - 1 lines
+    "nix")
+        length="$((TEMPLATE_LINE_LENGTH - 1))"
+        sed --in-place "1,${length}d;" "$file"
+        ;;
+    # Shell needs a shebang on the first line, only after the first line can we
+    # remove the $TEMPLATE_LINE_LENGTH lines
+    "sh")
+        sed --in-place "2,${TEMPLATE_LINE_LENGTH}d;" "$file"
+        licensure --in-place "$file"
+
+        TEMPLATE_LINE_LENGTH_NEW="$(($(yq --raw-output '.licenses | map(.template) | join("")' "$PROJECT_ROOT/.licensure.yml" | wc -l) + $(yq '.comments | last | .commenter.trailing_lines' "$PROJECT_ROOT/.licensure.yml")))"
+
+        # delete the current shebang
+        to="$((TEMPLATE_LINE_LENGTH_NEW + 1))"
+        sed --in-place "${TEMPLATE_LINE_LENGTH_NEW},${to}d;" "$file"
+
+        # add a new one
+        sed --in-place "1i#! /usr/bin/env sh" "$file"
+        ;;
+    *)
+        echo "File '$file' with extension '$extension' is not know yet, please add it!"
+        ;;
+    esac
+}
+
+list() {
+    echo "$extension -> $file"
+}
+
+if [ -f "$1" ]; then
+    file="$(realpath "$1")"
+    filename="$(basename -- "$file")"
+    extension="${filename##*.}"
+    filename="${filename%.*}"
+
+    if [ -n "$DRY_RUN" ]; then
+        list "$extension" "$file"
+    else
+        remove "$extension" "$file"
+    fi
+else
+    fd --type file --hidden . | while read -r file; do
+        if grep --quiet 'SPDX-License-Identifier' "$file"; then
+            filename="$(basename -- "$file")"
+            extension="${filename##*.}"
+            filename="${filename%.*}"
+
+            if [ -n "$DRY_RUN" ]; then
+                list "$extension" "$file"
+            else
+                remove "$extension" "$file"
+            fi
+        fi
+    done
+
+    if [ -z "$DRY_RUN" ]; then
+        licensure --in-place --project
+        nix fmt
+    fi
+fi