chore(user/bkphotorights):Note Bash version used
[BK-2020-03.git] / user / ots-git-gpg-wrapper-wait.sh
... / ...
CommitLineData
1#!/usr/bin/env bash
2# Desc: Wrapper for ots-git-gpg-wrapper with `--wait` option
3# Note: Required because git's gpg.program option doesn't allow you to set
4# command line options.
5# Usage: git -c gpg.program=<path to ots-git-gpg-wrapper-wait.sh> commit -S
6# Input: file: $HOME/.local/share/ots/jsonrpc_url.txt # first line is JSON RPC
7# url for local bitcoin node containing login credentials. Example:
8# http://user:35z8deadbeefhrdeadbeef5rk3@192.168.86.1:8332/
9# Depends: ots 0.7.0 (see https://github.com/opentimestamps/opentimestamps-client )
10# Version: 0.0.6
11# Ref/Attrib: [1] “How can I test if a particular alias is defined?”. https://unix.stackexchange.com/a/288513
12# [2] “OpenTimestamps Git Integration”. (2016-10-13). https://github.com/opentimestamps/opentimestamps-client/blob/master/doc/git-integration.md
13
14declare -a args calendars
15
16# Specify default calendars
17calendars+=("https://finney.calendar.eternitywall.com");
18calendars+=("https://btc.calendar.catallaxy.com");
19calendars+=("https://alice.btc.calendar.opentimestamps.org");
20calendars+=("https://bob.btc.calendar.opentimestamps.org");
21
22# Check if gpg is alias. See [1].
23if alias gpg 2>/dev/null; then
24 ## Get gpg alias command
25 gpg_cmd="$(type gpg)"; # get raw alias definition
26 gpg_cmd="${gpg_cmd#*=\'}"; # trim chars before and including first apostrophe
27 gpg_cmd="${gpg_cmd%\'*}"; # trim chars after and including last apostrophe
28else
29 gpg_cmd="$(which gpg)";
30fi;
31
32# Assemble args array
33## Specify '--wait' option
34args+=("--wait");
35
36## Specify bitcoin node jsonrpc url if available
37path_jsonrpc_url="$HOME/.local/share/ots/jsonrpc_url.txt";
38if [[ -f "$path_jsonrpc_url" ]]; then
39 jsonrpc_url="$(head -n1 "$path_jsonrpc_url")";
40 args+=("--bitcoin-node");
41 args+=("$jsonrpc_url");
42fi;
43
44## Pick random calendar
45### Get calendars array size
46cal_size="${#calendars[@]}";
47cal_rnd_idx="$(shuf -i 1-"$cal_size" -n 1)";
48cal_rnd_idx="$((cal_rnd_idx - 1))"; # bash array is zero-indexed
49url_cal_random="${calendars[$cal_rnd_idx]}";
50args+=("--whitelist");
51args+=("$url_cal_random");
52args+=("--no-default-whitelist");
53
54## Specify '--gpg-program' option
55args+=("--gpg-program");
56args+=("$gpg_cmd");
57
58## Passthrough positional parameters
59args+=("--"); # mark end of ots-git-wrapper options
60for param in "$@"; do
61 args+=("$param");
62done;
63
64# Run command with args
65# pseudocode: ots-git-gpg-wrapper --wait $jsonrpc_option $calendar_option --gpg-program $gpg_cmd -- "$@"
66ots-git-gpg-wrapper "${args[@]}";