summaryrefslogtreecommitdiff
path: root/bin/sign.sh
blob: 92fd207c98302fa5a516bbba904c9dc7192d5573 (plain)
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
#!/bin/sh
# Exit codes:
# 0 - Success
# 1 - Missing args
# 2 - File not found

set -e

usage() {
    echo "Usage: $(basename "$0") [-u KEY_ID] <file-to-sign>" >&2
    echo "\t-u KEY_ID\tUse a specific GPG key to sign with (optional)" >&2
    exit 1
}

USER_KEY=""

while getopts ":u:" opt; do
    case "$opt" in
        u) USER_KEY="$OPTARG" ;;
        *) usage ;;
    esac
done

shift $((OPTIND - 1))

if [ $# -ne 1 ]; then
    echo "Error: Missing file to sign." >&2
    usage
fi

FILE="$1"

if [ ! -f "$FILE" ]; then
    echo "Error: File '$FILE' not found." >&2
    exit 2
fi

GPG_CMD="gpg --include-key-block --openpgp -b"

if [ -n "$USER_KEY" ]; then
    GPG_CMD="$GPG_CMD --local-user $USER_KEY"
fi

eval "$GPG_CMD \"$FILE\"" && echo "Signature created: ${FILE}.sig"