From 38ebf64b07f99de9464905ee5f8aaee1148b6a4f Mon Sep 17 00:00:00 2001
From: Timmy Keller <tjk@tjkeller.xyz>
Date: Thu, 7 Jul 2022 15:55:54 -0500
Subject: tons of changes from when website was down

---
 keyboard/mounter       |  2 +-
 keyboard/screenshot    | 17 +++++++---
 launch/launch          | 92 ++++++++++++++++++++++++--------------------------
 misc/passwdgen         |  2 +-
 misc/seasonalwallpaper | 10 +++---
 misc/verify            | 45 +++++++++++++++++-------
 sync/pull              |  2 +-
 video/ffconcat         | 30 ++++++++++++++++
 video/rectape          | 18 +++++-----
 video/rectapehevc      | 15 ++++++++
 video/rectapevaapi     | 12 +++----
 video/screenrec        |  4 +++
 video/slideshow        | 26 +++++++-------
 video/vaapiencode      |  9 +++++
 x11/xkeyconf           | 15 +++-----
 x11/xsudo              |  5 +--
 16 files changed, 190 insertions(+), 114 deletions(-)
 create mode 100755 video/ffconcat
 create mode 100755 video/rectapehevc
 create mode 100755 video/vaapiencode

diff --git a/keyboard/mounter b/keyboard/mounter
index 90c6caf..e0a8fac 100755
--- a/keyboard/mounter
+++ b/keyboard/mounter
@@ -1,6 +1,6 @@
 #!/bin/sh
 # TODO: add support for mounting discs
-sudo=doas
+sudo=${SUDO:-sudo}
 mps=/media/mnt # User should have ownership of this directory
 notify='notify-send --urgency low'
 
diff --git a/keyboard/screenshot b/keyboard/screenshot
index f0d0fff..25e99cd 100755
--- a/keyboard/screenshot
+++ b/keyboard/screenshot
@@ -1,5 +1,13 @@
 #!/bin/sh
-printusage() { echo "Usage: $(basename "$0") [-cx]\n\nOPTIONS:\n  -c\tInteractively crop screenshot\n  -x\tCopy screenshot to clipboard with xclip rather than saving it" && exit; }
+printusage() {
+cat << HELPDOC
+Usage: $(basename "$0") [-cx]
+
+OPTIONS:
+  -c\tInteractively crop screenshot
+  -x\tCopy screenshot to clipboard with xclip rather than saving it
+HELPDOC
+}
 
 # Scrot and xclip opts
 fmt=$(date '+%m-%d-%4Y_%I:%M:%S_%p.png')
@@ -29,11 +37,10 @@ while getopts "cxh" arg; do
 	case "$arg" in
 		c) opt=$cropopt ;;
 		x) path=$tmppath; exp=$xclipexp; summ=$xclipsumm; body=$xclipbody ;;
-		h) printusage ;;
-		*) printusage ;;
+		h|*) printusage && exit ;;
 	esac
 done
 
 scrot $opt $path --exec "$exp" \
-&& notify-send $notifyopts -i $path "$summ" "$body" \
-|| notify-send $notifyopts "Screenshot Failed!"
+	&& notify-send $notifyopts -i $path "$summ" "$body" \
+	|| notify-send $notifyopts "Screenshot Failed!"
diff --git a/launch/launch b/launch/launch
index b4c1c7a..b8757e6 100755
--- a/launch/launch
+++ b/launch/launch
@@ -7,8 +7,8 @@ Usage:
   $(me): [-gh] [COMMAND] [COMMAND ARGS]...
 
 Args:
-  -g: Force generate cache
-  -h: show this help message
+  -g, --gen-cache : Force generate cache
+  -h, --help      : show this help message
 
 Config Syntax:
   +host [HOSTNAME]
@@ -22,51 +22,51 @@ HELPDOC
 
 gencache() {
 awk '
-	BEGIN {
-		print "run=\"$1\"; shift"
-		print "case \"$run\" in"
-	}
-	{
-		sub(/#.*/, "") # Remove comments
-		if (!/^\s*$/) {
-			if (/^\+host/) {
-				sub(/\s*\+host\s*/, "")
-				h++
-				hosts[h] = $0
+BEGIN {
+	print "run=\"$1\"; shift"
+	print "case \"$run\" in"
+}
+{
+	sub(/#.*/, "") # Remove comments
+	if (!/^\s*$/) {
+		if (/^\+host/) {
+			sub(/\s*\+host\s*/, "")
+			h++
+			hosts[h] = $0
+		}
+		else {
+			if (!/^\t/) {
+				hostnum = 1
+				o++
+				opts[o] = $0
 			}
 			else {
-				if (!/^\t/) {
-					hostnum = 1
-					o++
-					opts[o] = $0
-				}
-				else {
-					sub(/^\t/, "")
-					if ($0 == "^")
-						$0 = cache
-					else if ($0 ~ /^[0-9]*$/)
-						$0 = cmds[hosts[$0]":"opts[o]]
-					cmds[hosts[hostnum]":"opts[o]] = cache = $0
-					hostnum++
-				}
+				sub(/^\t/, "")
+				if ($0 == "^")
+					$0 = cache
+				else if ($0 ~ /^[0-9]*$/)
+					$0 = cmds[hosts[$0]":"opts[o]]
+				cmds[hosts[hostnum]":"opts[o]] = cache = $0
+				hostnum++
 			}
 		}
 	}
-	END {
-		for (host in hosts) {
-			if (hosts[host] != "'`hostname`'")
-				continue
-			for (cmd in cmds) {
-				if (cmd ~ hosts[host] && cmds[cmd] && cmds[cmd] != "-") {
-					i = cmd
-					sub(/.*:/, "", cmd)
-					print "\t"cmd") "cmds[i]" \"$@\" & ;;"
-				}
+}
+END {
+	for (host in hosts) {
+		if (hosts[host] != "'`hostname`'")
+			continue
+		for (cmd in cmds) {
+			if (cmd ~ hosts[host] && cmds[cmd] && cmds[cmd] != "-") {
+				i = cmd
+				sub(/.*:/, "", cmd)
+				print "\t"cmd") "cmds[i]" \"$@\" & ;;"
 			}
-			print "\t*) command -v \"$run\" >/dev/null && exec \"$run\" \"$@\" || echo \"\\`$run${@:+ $@}\\` Does not exist or exited with an error\" ;;"
-			print "esac"
 		}
+		print "\t*) command -v \"$run\" >/dev/null && exec \"$run\" \"$@\" || echo \"\\`$run${@:+ $@}\\` Does not exist or exited with an error\" ;;"
+		print "esac"
 	}
+}
 ' $conf > $cache
 chmod +x $cache
 }
@@ -77,10 +77,9 @@ conf=$confdir/config
 if [ ! -e $conf ]; then
 	mkdir -p $confdir
 	echo "#HOSTS\n+host $(hostname)\n\n#OPTS\ntest\n\techo \"Tested!\"" > $conf
-	echo "Config file created at $conf! Exiting"
-	echo
+	echo "No config file exists! new config file created at $conf! Exiting\n"
 	printhelp
-	exit 0
+	exit 1
 fi
 
 # Cache file setup
@@ -91,10 +90,7 @@ cache=$cachedir/cache
 
 # Command line options
 case "$1" in
-	-g) echo "Generating new cache: $cache"; gencache && cat $cache; exit ;;
-	-h) printhelp && exit ;;
-	'') printhelp && exit ;;
+	-g|--gen-cache) echo "Generating new cache: $cache"; gencache && cat $cache ;;
+	-h|--help|'') printhelp ;;
+	*) exec $cache "$@" ;; # Run
 esac
-
-# Run
-exec $cache "$@"
diff --git a/misc/passwdgen b/misc/passwdgen
index ce67c92..8e20ba7 100755
--- a/misc/passwdgen
+++ b/misc/passwdgen
@@ -2,7 +2,7 @@
 [ -n "`echo $1$2 | tr -d '[:digit:]'`" ] && echo "Usage: $0 [Itterations] [Length] [Character Set]" && exit
 [ -z "$1" ] && itt=5  || itt=$1
 [ -z "$2" ] && len=20 || len=$2
-[ -z "$3" ] && set='[:graph:]' || set=$3
+[ -z "$3" ] && set="[:alnum:],.<>/?\'!@#$%^&*()[]{};:-_=+" || set=$3
 genpasswd() { tr -cd "$3" < /dev/urandom | fold -w$1 | head -n$2; }
 genpasswd $len $itt $set
 
diff --git a/misc/seasonalwallpaper b/misc/seasonalwallpaper
index 525bfb0..f816136 100755
--- a/misc/seasonalwallpaper
+++ b/misc/seasonalwallpaper
@@ -1,12 +1,12 @@
 #!/bin/sh
 case $1 in
-	spring|summer|fall|winter) dir=~/.local/share/wallpaper/$1/ ; xwallpaper --zoom "$dir`ls $dir | shuf -n1`" ;;
+	spring|summer|fall|winter) dir=~/.local/share/wallpaper/$1 ; xwallpaper --zoom "$dir/`ls $dir | shuf -n1`" ;;
 	*)
 		case `date +%b` in
-			Dec|Jan|Feb)	$0 winter	;;
-			Mar|Apr|May)	$0 spring	;;
-			Jun|Jul|Aug)	$0 summer	;;
-			Sep|Oct|Nov)	$0 fall		;;
+			Dec|Jan|Feb) $0 winter	;;
+			Mar|Apr|May) $0 spring	;;
+			Jun|Jul|Aug) $0 summer	;;
+			Sep|Oct|Nov) $0 fall	;;
 		esac
 	;;
 esac
diff --git a/misc/verify b/misc/verify
index 5f0430c..1f0d4ab 100755
--- a/misc/verify
+++ b/misc/verify
@@ -4,11 +4,18 @@ CYN="\033[0;36m"
 GRN="\033[0;32m"
 RED="\033[0;31m"
 
+me="$(basename "$0")"
+
+concat="pv -EE -F'%r [%b] [%t] [%e] %p'"
+
 printhelp() {
 cat << HELPDOC
-Usage: $(basename "$0") [options] [file/directory]...
+Usage: $me [options] [file/directory]...
 
 OPTIONS:
+  -o, --stdout             Output checksum to STDOUT
+  -S, --supress            Supress all warnings and progress notes (default with -o)
+  -n, --no-progress        Don't output progress bar (default with -o & -S)
   -m, --md5                Create/verify md5 checksum
   -s[mode], --sha[mode]    Create/verify sha[mode] checksum
   -x[mode], --xxh[mode]    Create/verify xxh[mode] checksum (default)
@@ -21,6 +28,9 @@ HELPDOC
 crypt=xxh
 while true; do
 	case "$1" in
+		-o|--stdout)	stdout=1; nowarn=1; concat="cat" ;;
+		-S|--supress)	nowarn=1; concat="cat" ;;
+		-n|--no-progressbar)	concat="cat" ;;
 		-m|--md5)	crypt=md5 ;;
 		-s*|--sha*)	crypt=sha$(echo "$1" | tr -cd [:digit:]) ;;
 		-x*|--xxh*)	crypt=xxh$(echo "$1" | tr -cd [:digit:]) ;;
@@ -35,6 +45,13 @@ done
 hashalg=${crypt}sum
 command -v $hashalg >/dev/null || (echo "Hashing algorithm '$hashalg' does not exist!" && exit 2)
 
+# Warn user
+if [ ! "$nowarn" ]; then
+	for file in "$@"; do
+		[ -d "$file" ] && echo "${RED}NOTE:${NORM} getting a checksum from an entire directory is NOT a standard feature in any other checksum-checking program! They can only be checked with this script!" && break
+	done
+fi
+
 while [ -n "$1" ]; do
 	file="$1"
 	[ ! "`echo "$file" | sed "s/.$crypt$//"`" = "$file" ] && shift && continue # Skip hash files
@@ -43,20 +60,24 @@ while [ -n "$1" ]; do
 	hashfile="$file.$crypt"
 	[ -n "$verify" ] && [ ! -e "$hashfile" ] && shift && continue # Skip files without a hash in verify mode
 
-	echo "Getting $crypt checksum of $type '$file'..."
-	hash=`find "$file" -type f -print0 | sort -z | xargs -r0 pv -EE -F'%r [%b] [%t] [%e] %p' | $hashalg | cut -d' ' -f1`
+	[ ! "$nowarn" ] && echo "Getting $crypt checksum of $type '$file'..."
+	hash=`find "$file" -type f -print0 | sort -z | xargs -r0 $concat | $hashalg | cut -d' ' -f1`
 
-	if [ -e "$hashfile" ]; then
-		phash=`cat "$hashfile"`
-		[ $phash = $hash ] \
-			&& echo "${GRN}Success: $type matches saved checksum!" \
-			|| (echo "${RED}Error: $type does not match saved checksum!\nExpected checksum: $phash\nActual checksum: $hash" && ec=1)
+	if [ "$stdout" ]; then
+		echo "$hash  ($crypt)  $file"
 	else
-		echo $hash > "$hashfile" \
-			&& echo "${CYN}$crypt checksum file saved at '$hashfile'!" \
-			|| (echo "${RED}'$hashfile' could not be created!\n$crypt checksum of $type '$file' is $hash" && ec=1)
+		if [ -e "$hashfile" ]; then
+			phash=`cat "$hashfile"`
+			[ $phash = $hash ] \
+				&& echo "${GRN}Success: $type matches saved checksum!" \
+				|| (echo "${RED}Error: $type does not match saved checksum!\nExpected checksum: $phash\nActual checksum: $hash" && ec=1)
+		else
+			echo $hash > "$hashfile" \
+				&& echo "${CYN}$crypt checksum file saved at '$hashfile'!" \
+				|| (echo "${RED}'$hashfile' could not be created!\n$crypt checksum of $type '$file' is $hash" && ec=1)
+		fi
+		printf "$NORM"; [ -n "$2" ] && echo
 	fi
-	printf "$NORM"; [ -n "$2" ] && echo
 	shift
 done
 exit $ec
diff --git a/sync/pull b/sync/pull
index 06cf145..f52ec5e 100755
--- a/sync/pull
+++ b/sync/pull
@@ -31,7 +31,7 @@ pullrepo() {
 makerepo() {
 	if echo "$opts" | grep '\-b'; then
 		printmakemsg
-		doas make clean install \
+		${SUDO:-sudo} make clean install \
 		&& printmakesuccess || printmakeerror
 	fi
 }
diff --git a/video/ffconcat b/video/ffconcat
new file mode 100755
index 0000000..34e6550
--- /dev/null
+++ b/video/ffconcat
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+trap cleanup EXIT
+
+ccf=/tmp/concatfile.
+cleanup() { rm ${ccf}*; }
+concat() {
+	outfile="output$i.mkv"
+	concatfile=$(mktemp ${ccf}XXXXXX)
+	dir="$(pwd)"
+	for video in $vidfiles; do
+		echo "file '$dir/$video'" >> $concatfile
+	done
+	cat $concatfile | sort > ${concatfile}_ # Sort concatfile so videos are ordered correctly
+	ffmpeg -v error -f concat -safe 0 -i ${concatfile}_ -c copy $outfile
+}
+
+for video in "$@"; do
+	codechash=$(ffprobe -v quiet -show_format -show_streams "$video" | \grep 'width\|height\|codec' | md5sum | cut -d' ' -f1)
+	[ -z "$prevhash" ] && prevhash=$codechash
+	if [ $prevhash = $codechash ]; then
+		vidfiles="$video${vidfiles:+ }$vidfiles"
+	else
+		[ -z "$i" ] && i=0
+		concat
+		vidfiles=""
+		i=$((i+1))
+	fi
+done
+concat
diff --git a/video/rectape b/video/rectape
index fa631eb..238f5de 100755
--- a/video/rectape
+++ b/video/rectape
@@ -5,12 +5,12 @@ for mod in blackmagic blackmagic-io; do doas modprobe $mod; done
 #doas rc-service DesktopVideoHelper start
 
 ffmpeg \
--f decklink -format_code ntsc -i "Intensity Pro" \
--c:v libx264 \
--preset medium \
--qmin 6 -qmax 18 -qdiff 4 \
--vf yadif \
--c:a aac -b:a 384k \
--metadata "title"="$1" \
--metadata "comment=tapes.tjkeller.xyz" \
-"$1.mkv"
+	-f decklink -format_code ntsc -i "Intensity Pro" \
+	-c:v libx264 \
+	-preset medium \
+	-qmin 6 -qmax 18 -qdiff 4 \
+	-vf yadif \
+	-c:a aac -b:a 384k \
+	-metadata "title"="$1" \
+	-metadata "comment=tapes.tjkeller.xyz" \
+	"$1.mkv"
diff --git a/video/rectapehevc b/video/rectapehevc
new file mode 100755
index 0000000..c44d6d9
--- /dev/null
+++ b/video/rectapehevc
@@ -0,0 +1,15 @@
+#!/bin/sh
+if [ -z "$1" ]; then $0 untitled_$(ls | wc -l); exit; fi
+
+for mod in blackmagic blackmagic-io; do doas modprobe $mod; done
+#doas rc-service DesktopVideoHelper start
+
+ffmpeg \
+	-f decklink -format_code ntsc -i "Intensity Pro" \
+	-c:v libx265 \
+	-qmin 6 -qmax 18 -qdiff 4 \
+	-vf yadif \
+	-c:a flac \
+	-metadata "title"="$1" \
+	-metadata "comment=tapes.tjkeller.xyz" \
+	"$1.mkv"
diff --git a/video/rectapevaapi b/video/rectapevaapi
index 4ddf868..91c0709 100755
--- a/video/rectapevaapi
+++ b/video/rectapevaapi
@@ -5,9 +5,9 @@ for mod in blackmagic blackmagic-io; do doas modprobe $mod; done
 doas rc-service DesktopVideoHelper start
 
 ffmpeg -hwaccel auto -vaapi_device /dev/dri/renderD128 \
--f decklink -format_code ntsc -i "Intensity Pro" \
--c:v hevc_vaapi \
--vf "yadif,format=nv12,hwupload" \
--c:a libopus -b:a 384k \
--metadata "title"="$1$title" \
-"$1$title.mkv"
+	-f decklink -format_code ntsc -i "Intensity Pro" \
+	-c:v hevc_vaapi \
+	-vf "yadif,format=nv12,hwupload" \
+	-c:a libopus -b:a 384k \
+	-metadata "title"="$1$title" \
+	"$1$title.mkv"
diff --git a/video/screenrec b/video/screenrec
index fa6b628..8f65514 100755
--- a/video/screenrec
+++ b/video/screenrec
@@ -13,7 +13,11 @@ ffmpeg -hide_banner \
 	-framerate $fr \
 	-f x11grab \
 	-i :0.0+0,0 \
+	-f alsa -channels 1 -sample_rate 48000 \
+	-i hw:3 \
 	-c:v libx264 \
+	-c:a libopus \
+	-b:a 128k \
 	-preset ultrafast \
 	-qp 1 \
 	-pix_fmt yuv444p \
diff --git a/video/slideshow b/video/slideshow
index d1a9647..4cd1f61 100755
--- a/video/slideshow
+++ b/video/slideshow
@@ -1,15 +1,15 @@
 #!/bin/sh
 cat "$@" | ffmpeg -y \
--framerate 0.20 -loop 1 \
--f image2pipe \
--i - \
--c:v h264_amf -quality quality -tune stillimage \
--qp_i 12 -qp_p 28 -qp_b 28 \
--vf \
-fps=1,\
-framerate=25:\
-interp_start=0:\
-interp_end=255:\
-scene=100 \
--metadata "title"="$(pwd | sed "s/.*\///")" \
-output.mkv
+	-framerate 0.20 -loop 1 \
+	-f image2pipe \
+	-i - \
+	-c:v libx264 -quality quality -tune stillimage \
+	-qmin 12 -qmax 28 -qdiff 16 \
+	-vf \
+	fps=1,\
+	framerate=25:\
+	interp_start=0:\
+	interp_end=255:\
+	scene=100 \
+	-metadata "title"="$(pwd | sed "s/.*\///")" \
+	output.mkv
diff --git a/video/vaapiencode b/video/vaapiencode
new file mode 100755
index 0000000..ae079f2
--- /dev/null
+++ b/video/vaapiencode
@@ -0,0 +1,9 @@
+#!/bin/sh
+ffmpeg -hwaccel vaapi -hwaccel_output_format vaapi -vaapi_device /dev/dri/renderD128 \
+	-ss 60 \
+	-i "$1" \
+	-t 60 \
+	-c:v h264_vaapi \
+	-b:v 1000k \
+	-c:a libopus -b:a 128k \
+	"h264_$1.mkv"
diff --git a/x11/xkeyconf b/x11/xkeyconf
index cea356f..bc310b6 100755
--- a/x11/xkeyconf
+++ b/x11/xkeyconf
@@ -1,12 +1,5 @@
 #!/bin/sh
-# Faster repeated keys and less delay before repeating
-xset r rate 250 50 &
-
-# Remap caps to super when held
-setxkbmap -option caps:super
-
-# Remap caps to escape when pressed
-killall xcape 2>/dev/null ; xcape -e 'Super_L=Escape'
-
-# Turn off caps lock, just in case it was on prior
-xset -q | grep "Caps Lock:\s*on" && xdotool key Caps_Lock
+xset r rate 250 50 & # Faster repeated keys and less delay before repeating
+setxkbmap -option caps:super # Remap caps to super when held
+killall xcape 2>/dev/null ; xcape -e 'Super_L=Escape' # Remap caps to escape when pressed
+xset -q | grep "Caps Lock:\s*on" && xdotool key Caps_Lock # Turn off caps lock, just in case it was on prior
diff --git a/x11/xsudo b/x11/xsudo
index 5feabb1..db4a82a 100755
--- a/x11/xsudo
+++ b/x11/xsudo
@@ -1,7 +1,8 @@
 #!/bin/sh
-sudo=doas
+sudo=${SUDO:-sudo}
 while ! echo "$pass" | $sudo -n true; do
 	pass="$(dmenu -p 'Password: ' <&-)" # Ask user for password pass is incorrect (skipped if nopass in enabled)
 	[ "$pass" = '' ] && exit 0
 done
-echo "$pass" | $sudo xauth -f /root/.Xauthority add $(xauth list $DISPLAY) && echo "$pass" | $sudo "$@"
+echo "$pass" | $sudo xauth -f /root/.Xauthority add $(xauth list $DISPLAY) \
+	&& echo "$pass" | $sudo "$@"
-- 
cgit v1.2.3