From 0f1df0db903ba576fd17b08197d3066af7a61e5f Mon Sep 17 00:00:00 2001
From: Mattias Andrée <maandree@kth.se>
Date: Sat, 18 Nov 2023 23:23:40 +0100
Subject: A lot of changes
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Mattias Andrée <maandree@kth.se>
---
 bash/Makefile       | 33 ++++++++++++++++-----------------
 bash/aliases-Zenith | 49 ++++++++++++++++++++++++++++++++++++-------------
 bash/bashrc_prompt  | 16 +++++++++-------
 3 files changed, 61 insertions(+), 37 deletions(-)

(limited to 'bash')

diff --git a/bash/Makefile b/bash/Makefile
index 39dd6d3..bb25967 100644
--- a/bash/Makefile
+++ b/bash/Makefile
@@ -1,21 +1,20 @@
 .POSIX:
-include ../common.mk
 
 install:
 	mkdir -p -- ~/.var/cache
 	mkdir -p -- ~/.config/bash
 	touch -- ~/.var/cache/bash_history
-	test ! -d ~/.bash_history
-	test ! -d ~/.bash_logout
-	test ! -d ~/.bash_profile
-	test ! -d ~/.bashrc
-	test ! -d ~/.config/bash/logout
-	test ! -d ~/.config/bash/profile
-	test ! -d ~/.config/bash/bashrc
-	test ! -d ~/.config/bash/bashrc_palette
-	test ! -d ~/.config/bash/bashrc_prompt
-	test ! -d ~/.config/bash/aliases
-	test ! -d ~/.config/bash/aliases-Zenith
+	test ! -e ~/.bash_history || test -L ~/.bash_history
+	test ! -e ~/.bash_logout || test -L ~/.bash_logout
+	test ! -e ~/.bash_profile || test -L ~/.bash_profile
+	test ! -e ~/.bashrc || test -L ~/.bashrc
+	test ! -e ~/.config/bash/logout || test -L ~/.config/bash/logout
+	test ! -e ~/.config/bash/profile || test -L ~/.config/bash/profile
+	test ! -e ~/.config/bash/bashrc || test -L ~/.config/bash/bashrc
+	test ! -e ~/.config/bash/bashrc_palette || test -L ~/.config/bash/bashrc_palette
+	test ! -e ~/.config/bash/bashrc_prompt || test -L ~/.config/bash/bashrc_prompt
+	test ! -e ~/.config/bash/aliases || test -L ~/.config/bash/aliases
+	test ! -e ~/.config/bash/aliases-Zenith || test -L ~/.config/bash/aliases-Zenith
 	ln -sf -- .var/cache/bash_history ~/.bash_history
 	ln -sf -- .config/bash/logout ~/.bash_logout
 	ln -sf -- .config/bash/profile ~/.bash_profile
@@ -28,13 +27,15 @@ install:
 	ln -sf -- ~/.dotfiles/bash/aliases ~/.config/bash/aliases
 	if test "$$(hostname | tr '[[:upper:]]' '[[:lower:]]')" = zenith; then \
 		test "$$(hostname)" = Zenith && \
-		$(CHECK_INSTALLED) asroot && \
-		$(CHECK_INSTALLED) gnupg && \
-		$(CHECK_INSTALLED) cryptsetup && \
+		../check-any-installed asroot asroot-libpassphrase && \
+		../check-installed gnupg && \
+		../check-installed cryptsetup && \
+		../check-installed key2root && \
 		ln -sf -- ~/.dotfiles/bash/aliases-Zenith ~/.config/bash/aliases-Zenith; \
 	fi
 
 uninstall:
+	+! ../check-installed bash
 	-unlink -- ~/.bash_history
 	-unlink -- ~/.bash_logout
 	-unlink -- ~/.bash_profile
@@ -50,5 +51,3 @@ uninstall:
 	-rmdir -- ~/.config/bash/aliases.d
 	-rmdir -- ~/.config/bash/bashrc.d
 	-rmdir -- ~/.config/bash
-
-.PHONY: install uninstall
diff --git a/bash/aliases-Zenith b/bash/aliases-Zenith
index 73db885..e4e7422 100644
--- a/bash/aliases-Zenith
+++ b/bash/aliases-Zenith
@@ -1,21 +1,44 @@
 # -*- shell-script -*-
 
 mount-crypt () {
-	gpg --decrypt /media/Secondary/.crypt/crypt.key | \
-	asroot env XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR}" CRYPT_DIR=~/Crypt sh -ec '
-		device="$(losetup --show -f -P /media/Secondary/.crypt/crypt.img)"
-		printf "%s\n" "${device}" > "${XDG_RUNTIME_DIR}/CryptCrypt"
-		cryptsetup -d - open "${device}" CryptCrypt
-		mkdir -p "${CRYPT_DIR}"
-		mount /dev/mapper/CryptCrypt "${CRYPT_DIR}"
-		'
+	if mountpoint -q -- ~/Crypt; then
+		printf '%s\n' 'mount-crypt: already mounted' >&2
+	elif test -e /dev/mapper/CryptCrypt; then
+		asroot env CRYPT_DIR=~/Crypt sh -ec \
+			'mount -- /dev/mapper/CryptCrypt "${CRYPT_DIR}"'
+	else
+		gpg --decrypt /media/Secondary/.crypt/crypt.key | \
+		key2root -k CryptCrypt env \
+			CRYPT_IMG=/media/Secondary/.crypt/crypt.img \
+			CRYPT_DIR=~/Crypt \
+			CRYPT_DEV="$(losetup | sed 's/\s\+/ /g' | cut -d ' ' -f 1,6 | \
+			              sed -n 's#\(/[^ ]*\) '"$(realpath -- /media/Secondary/.crypt/crypt.img)"'$#\1#p' | sed 1q)" \
+			sh -ec '
+				if test -z "${CRYPT_DEV}"; then
+					CRYPT_DEV="$(losetup --show -f -P "${CRYPT_IMG}")"
+				fi
+				cryptsetup -d - open "${CRYPT_DEV}" CryptCrypt
+				mkdir -p -- "${CRYPT_DIR}"
+				mount -- /dev/mapper/CryptCrypt "${CRYPT_DIR}"
+			'
+	fi
 }
 
 umount-crypt () {
-	asroot env XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR}" CRYPT_DIR=~/Crypt sh -c '
-		umount "${CRYPT_DIR}"
-		rmdir "${CRYPT_DIR}"
-		cryptsetup close CryptCrypt
-		losetup --detach "$(cat -- "${XDG_RUNTIME_DIR}/CryptCrypt")"
+	asroot env \
+		CRYPT_DIR=~/Crypt \
+		CRYPT_DEV="$(losetup | sed 's/\s\+/ /g' | cut -d ' ' -f 1,6 | \
+		             sed -n 's#\(/[^ ]*\) '"$(realpath -- /media/Secondary/.crypt/crypt.img)"'$#\1#p' | sed 1q)" \
+		sh -c '
+			if mountpoint -q -- "${CRYPT_DIR}"; then
+				umount -- "${CRYPT_DIR}"
+			fi
+			rmdir -- "${CRYPT_DIR}" || :
+			if test -e /dev/mapper/CryptCrypt; then
+				cryptsetup close CryptCrypt
+			fi
+			if test -n "${CRYPT_DEV}"; then
+				losetup -d "${CRYPT_DEV}"
+			fi
 		'
 }
diff --git a/bash/bashrc_prompt b/bash/bashrc_prompt
index 5463afb..dc0e920 100644
--- a/bash/bashrc_prompt
+++ b/bash/bashrc_prompt
@@ -153,13 +153,15 @@ git-colour () {
 
 
 __prompt_func_git () {
-	exec 2>/dev/null
-	if git status >&2; then
-		status="$(git status -s -b | head -n 1)"
-		if [ "$(echo "${status}" | cut -d ' ' -f 3)" = '[ahead' ]; then
-			echo "${status}" | cut -d ' ' -f 2
+	if __prompt_func_git_branch="$(LANG=C git branch --format='%(if)%(HEAD)%(then)%(refname:short)%(end)' 2>/dev/null)"; then
+		if __prompt_func_git_branch="$(printf '%s\n' "${__prompt_func_git_branch}" | grep -v '^$')"; then
+			if [ -z "${__prompt_func_git_branch}" ]; then
+				printf '%s' '(orphan)'
+			else
+				printf '%s' "${__prompt_func_git_branch}" | sed 's/^(HEAD detached at .*)$/(detached)/'
+			fi
 		else
-			echo "${status}" | cut -d ' ' -f 2 | cut -d . -f 1
+			printf '%s' '(orphan)'
 		fi
 	fi
 }
@@ -368,7 +370,7 @@ update-prompt () {
 	fi
 	if [ "${__prompt_git}" = "1" ]; then
 		if [ ! "${PS1}" = "" ]; then
-			PS1="${PS1}"'$(git status 2>/dev/null >&2 && echo -n : || echo -n "")'
+			PS1="${PS1}"'$(if git branch 2>/dev/null >&2; then printf ':'; fi)'
 		fi
 		PS1="${PS1}\[\033[${__prompt_git_colour}m\]"'$(__prompt_func_git)'"\[\033[00m\]"
 	fi
-- 
cgit v1.2.3-70-g09d2