diff options
| author | la-ninpre <leobrekalini@gmail.com> | 2021-07-12 16:27:52 +0300 |
|---|---|---|
| committer | la-ninpre <leobrekalini@gmail.com> | 2021-07-12 16:27:52 +0300 |
| commit | e3bbee81de559c1b4dea7a83d46da3b4e00cc2a8 (patch) | |
| tree | 697bca3add8a534bd6ed6a82308ef5869cc78393 /bin | |
| parent | 2d5fa70da78d05147a4055c51c983b93a4bc32dd (diff) | |
| download | dotfiles-e3bbee81de559c1b4dea7a83d46da3b4e00cc2a8.tar.gz dotfiles-e3bbee81de559c1b4dea7a83d46da3b4e00cc2a8.zip | |
move to GNU stow approach on managing dotfiles
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/.local/bin/fossil-update | 95 | ||||
| -rwxr-xr-x | bin/.local/bin/keylookup | 8 | ||||
| -rwxr-xr-x | bin/.local/bin/nimi_sewi | 10 | ||||
| -rwxr-xr-x | bin/.local/bin/paperbackup | 165 | ||||
| -rwxr-xr-x | bin/.local/bin/print_codes | 97 | ||||
| -rwxr-xr-x | bin/.local/bin/rand | 18 |
6 files changed, 393 insertions, 0 deletions
diff --git a/bin/.local/bin/fossil-update b/bin/.local/bin/fossil-update new file mode 100755 index 0000000..5ea4d1e --- /dev/null +++ b/bin/.local/bin/fossil-update @@ -0,0 +1,95 @@ +#!/bin/sh + +SUDO=doas +_fossil_co=$(fossil all ls -c | grep fossil) # fossil checkout location +_ask_upgrade=1 +_ask_all=1 +_clean_build=0 +_make_jobs=8 + +usage() { + echo "usage: fossil-update [OPTIONS]" + echo "options:" + echo " -y upgrade without asking" + echo " -a upgrade and turn off the fossil service(if it's running) without asking" + echo " -c clean build, run 'make distclean' before building" + echo " -h print this help message" +} + +check_service() { + _fsl_pid=$(pgrep -u root -f "$(which fossil)") && export _fsl_pid + [ -n "$_fsl_pid" ] && { + [ "$_ask_all" -ne 0 ] && { + echo "fossil service is active. stop it? (Y/y)" + read -r _stop_fsl + case $_stop_fsl in + [Yy] ) stop_service && return 0 + ;; + * ) echo "upgrade is not possible" && exit 2 + ;; + esac + } + stop_service + } +} + +stop_service() { + # i am using systemd service to run fossil server + $SUDO systemctl stop fossil +} + +compile() { + echo "configuring..." && \ + ./configure >/dev/null && \ + echo "building..." && make -j "$_make_jobs" >/dev/null \ + && echo "build done" +} + +install() { + check_service + $SUDO make install >/dev/null && echo "upgrade done" + [ -n "$_fsl_pid" ] && echo "don't forget to enable fossil service back" \ + || return 0 +} + +main() { + cd "$_fossil_co" || return + [ $_clean_build -gt 0 ] && { + echo "cleaning previous build" + [ -f Makefile ] && make distclean >/dev/null + } + fossil up trunk + [ $_ask_all -eq 0 ] || [ $_ask_upgrade -eq 0 ] && { + compile && install + exit + } + echo "upgrade? (Y/y)" && \ + read -r _upgrade && \ + case $_upgrade in + [Yy] ) compile && install + ;; + * ) exit 1 + ;; + esac +} + +while getopts yach opts +do + case "$opts" in + y) + _ask_upgrade=0 + ;; + a) + _ask_all=0 + ;; + c) + _clean_build=1 + ;; + h|*) + usage && exit 1 + ;; + esac +done + +main + diff --git a/bin/.local/bin/keylookup b/bin/.local/bin/keylookup new file mode 100755 index 0000000..90e3a45 --- /dev/null +++ b/bin/.local/bin/keylookup @@ -0,0 +1,8 @@ +#!/bin/sh + +main() { + curl "http://hkps.pool.sks-keyservers.net/pks/lookup?op=get&search=0x$1" +} + +main "$@" + diff --git a/bin/.local/bin/nimi_sewi b/bin/.local/bin/nimi_sewi new file mode 100755 index 0000000..c21c40f --- /dev/null +++ b/bin/.local/bin/nimi_sewi @@ -0,0 +1,10 @@ +#!/bin/sh + +# generate random tokipona phrase(see nimisewi for details), +# copy it to the clipboard and send notification with it + +_nimi_sewi=$(nimisewi) + +echo "$_nimi_sewi" | xclip -sel clipboard + +notify-send -a nimi_sewi -t 2000 "nimi sewi" "$_nimi_sewi" diff --git a/bin/.local/bin/paperbackup b/bin/.local/bin/paperbackup new file mode 100755 index 0000000..4300241 --- /dev/null +++ b/bin/.local/bin/paperbackup @@ -0,0 +1,165 @@ +#!/bin/sh -e + +# Copyright (c) 2021, la-ninpre +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# +# ---------------------------------------------------------------------- # +# +# small script to backup secret gpg keys using paperkey and qrencode. +# +# basic usage: +# +# $ paperbackup -u user@email.com +# +# by default exports minimized public key and paperkeyed secret key +# to qr-encoded images, which then are combined into one image for +# easy printing. +# +# dependencies: +# - gpg (obviously) +# - paperkey +# - qrencode +# - imagemagick (optional, use -n to disable montage) + +usage() { + echo "usage: ${0##*/} [OPTIONS]" + echo + echo "options:" + echo " -u, --uid {UID}" + echo " gpg keyid or uid (this is mandatory)" + echo " -l, --level {L,M,Q,H}" + echo " qrencode correction level" + echo " -t, --term" + echo " output to terminal instead of png image" + echo " -n, --nomontage" + echo " disable composing qrcodes to one image" + echo " -h, --help" + echo " print usage information" + echo +} + +check_uid() { + [ -z "$1" ] && echo "uid could not be empty" && exit 1 + + if ! gpg -K "$1" >/dev/null + then + exit 1 + fi + + PB_UID="$1" +} + +check_corr_lvl() { + case "$1" in + l|L|m|M|q|Q|h|H) + PB_CORR_LVL="$1" + ;; + *) + echo "correction level should be one of L,M,Q,H" + usage + exit 1 + ;; + esac +} + +qr_encode() { + PB_QRENCODE_FLAGS="-8 -l $PB_CORR_LVL -t $PB_TYPE" + + case "$PB_TYPE" in + png) + PB_QRENCODE_FLAGS="$PB_QRENCODE_FLAGS -o $1" + ;; + UTF8) + ;; + *) + exit 1 + ;; + esac + + # i don't know if it is safe to do so, but it's very convenient + # shellcheck disable=SC2086 + qrencode $PB_QRENCODE_FLAGS +} + +get_pubkey() { + [ -n "$1" ] && gpg --export --export-options export-minimal "$1" \ + | qr_encode "$1-pubkey.png" +} + +get_seckey() { + [ -n "$1" ] && gpg --export-secret-key "$1" \ + | paperkey --output-type raw | qr_encode "$1-seckey.png" +} + +montage_keys() { + montage \ + -pointsize 18 \ + -title "\nsecret key backup for $PB_UID\n$PB_DATE" \ + -label pubkey "$PB_UID-pubkey.png" \ + -label seckey "$PB_UID-seckey.png" \ + -mode concatenate \ + "$PB_UID-keys.png" + + rm -f "$PB_UID-pubkey.png" "$PB_UID-seckey.png" +} + +main() { + PB_TYPE="png" + PB_CORR_LVL="H" + PB_DATE=$(date "+%F %T") + PB_MONTAGE=1 + + while [ -n "$1" ] + do + case "$1" in + --uid|-u) + shift + check_uid "$1" + ;; + --level|-l) + shift + check_corr_lvl "$1" + ;; + --term|-t) + PB_TYPE="UTF8" + ;; + --nomontage|-n) + PB_MONTAGE=0 + ;; + --help|-h) + echo "backup secret gpg key using paperkey and qrencode" + echo + usage + exit + ;; + *) + usage + exit 1 + ;; + esac + shift + done + + [ -z "$PB_UID" ] && echo "please specify uid or keyid" && exit 1; + + get_pubkey "$PB_UID" && get_seckey "$PB_UID" + + [ "$PB_TYPE" = "png" ] \ + && [ "$PB_MONTAGE" -eq 1 ] \ + && montage_keys + + exit 0 +} + +main "$@" diff --git a/bin/.local/bin/print_codes b/bin/.local/bin/print_codes new file mode 100755 index 0000000..bb8b708 --- /dev/null +++ b/bin/.local/bin/print_codes @@ -0,0 +1,97 @@ +#!/bin/sh + +# Copyright (c) 2021, la-ninpre +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + + +# small script to create page with qr-coded recovery tokens for otp +# uses: qrencode, imagemagick(convert, montage) +# +# file with codes should be in following format: +# - recovery codes should be on separate lines +# - every line beginning with # is ignored + +QRENCODE_ERR_CORR_LEVEL=H +CODES_KEEP_PNG=0 +CODES_MAKE_PDF=0 + +usage() { + echo "create qr-coded recovery tokens for easy printing" + echo + echo "USAGE:" + echo " $0 [OPTIONS] <text-file>" + echo + echo "OPTIONS:" + echo " -k keep png files with qrcodes" + echo " -l {L,M,Q,H}" + echo " set qrencode error correction level" + echo " -p produce pdf instead of image" + echo " -h print this help message" + echo +} + +while getopts kl:ph arg +do + case "$arg" in + k) CODES_KEEP_PNG=1 + ;; + l) case $OPTARG in + [lL]) QRENCODE_ERR_CORR_LEVEL=L ;; + [mM]) QRENCODE_ERR_CORR_LEVEL=M ;; + [qQ]) QRENCODE_ERR_CORR_LEVEL=Q ;; + [hH]) QRENCODE_ERR_CORR_LEVEL=H ;; + *) echo "error correction level should be one of L, M, Q or H" + exit 1 ;; + esac + ;; + p) CODES_MAKE_PDF=1 + ;; + h|*) usage && exit 1 + ;; + esac +done + +shift $((OPTIND - 1)) + +[ $# -ne 1 ] && usage && exit 1 + +CODES_FILE="$1" +CODES_BASENAME="${CODES_FILE%%.*}" +CODES_EXT="${CODES_FILE##*.}" +CODES_DIR="$PWD/${CODES_BASENAME}_${CODES_EXT}_dir" +CODES_DATE="$(date +'%Y-%m-%d %H:%M')" +CODES_OUTPUT="${CODES_BASENAME}_qrcodes" +CODES_OUT_IMG="${CODES_OUTPUT}.png" +CODES_OUT_PDF="${CODES_OUTPUT}.pdf" + +rm -f "$CODES_OUT_IMG" "$CODES_OUT_PDF" +[ -d "$CODES_DIR" ] && rm -rf "$CODES_DIR" +mkdir "$CODES_DIR" + +sed -e '/^#.*/ d' -e '/^\s*$/ d' < "$CODES_FILE" \ + | xargs -I{} \ + qrencode -l "$QRENCODE_ERR_CORR_LEVEL" -t png -d 150 \ + -o "$CODES_DIR/{}-code.png" {} + +montage -title "\n$CODES_FILE\n$CODES_DATE" \ + "$CODES_DIR/*-code.png" -page a4 -geometry 128x128\> "$CODES_OUT_IMG" + +[ "$CODES_MAKE_PDF" -ne 0 ] && { + convert \( -size 1240x1754 xc: \) "$CODES_OUT_IMG" \ + -composite "$CODES_OUT_PDF" + [ "$CODES_KEEP_PNG" -eq 0 ] && rm -f "$CODES_OUT_IMG" + } + +[ "$CODES_KEEP_PNG" -eq 0 ] && rm -rf "$CODES_DIR" || exit 0 + diff --git a/bin/.local/bin/rand b/bin/.local/bin/rand new file mode 100755 index 0000000..795885b --- /dev/null +++ b/bin/.local/bin/rand @@ -0,0 +1,18 @@ +#!/bin/sh + +# rand (inspired by https://rgz.ee/random.html) +# +# generate random string from charset as first argument and length as second +# +# example: +# $ rand '1-9a-f' 10 +# > 329402aa42 +# +# defaults: +# charset -- all printable characters and space +# length -- 25 + +[ -z $1 ] && charset=' -~' || charset=$1 +[ -z $2 ] && length=25 || length=$2 + +tr -cd "$charset" < /dev/urandom | fold -w "$length" | head -n 1 |
