From 31fb987b4cba8212978452e00e0071b10230af7d Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Wed, 2 Aug 2023 03:38:24 +0200
Subject: [PATCH] Improve w530 bookworm setup.

---
 bookworm/borg.sh                            | 145 ++++++++++++++++++++
 bookworm/home_files/user/.borgrepos         |   4 +
 bookworm/home_files/user/.mbsyncrc          |  28 ++++
 bookworm/home_files/user/.notmuch-config    |   9 ++
 bookworm/home_files/user/public_repos/repos |   7 +
 5 files changed, 193 insertions(+)
 create mode 100755 bookworm/borg.sh
 create mode 100644 bookworm/home_files/user/.borgrepos
 create mode 100644 bookworm/home_files/user/.mbsyncrc
 create mode 100644 bookworm/home_files/user/.notmuch-config
 create mode 100644 bookworm/home_files/user/public_repos/repos

diff --git a/bookworm/borg.sh b/bookworm/borg.sh
new file mode 100755
index 0000000..18321b1
--- /dev/null
+++ b/bookworm/borg.sh
@@ -0,0 +1,145 @@
+#!/bin/sh
+set -e
+
+standard_repo="borg"
+config_file="${HOME}/.borgrepos"
+
+usage() {
+    echo "Need operation as argument, one of:"
+    echo "init"
+    echo "store"
+    echo "check"
+    echo "export_keyfiles"
+    echo "orgpush"
+    echo "orgpull"
+    false
+}
+
+read_pw() {
+    if [ "${#SSH_AGENT_PID}" -eq 0 ]; then
+        eval $(ssh-agent)
+        echo "ssh-add"
+        stty -echo
+        ssh-add
+        stty echo
+    fi
+    if [ "${#BORG_PASSPHRASE}" -eq 0 ]; then
+        stty -echo
+        printf "Borg passphrase: "
+        read password
+        stty echo
+        printf "\n"
+        export BORG_PASSPHRASE="${password}"
+    fi
+}
+
+if [ ! -f "${config_file}" ]; then
+    echo '# file read ends at last newline' >> "${config_file}"
+fi
+if [ "$#" -lt 1 ]; then
+    usage
+fi
+first_arg="$1"
+shift
+if [ "${first_arg}" = "init" ]; then
+    if [ ! "$#" -eq 1 ]; then
+        echo "Need exactly one argument: target of form user@server"
+        false
+    fi
+    target="$1"
+    echo "Initializing: ${target}"
+    borg init --verbose --encryption=keyfile "${target}:${standard_repo}"
+    tmp_file="/tmp/new_borgrepos"
+    echo "${target}" > "${tmp_file}"
+    cat "${config_file}" >> "${tmp_file}"
+    cp "${tmp_file}" "${config_file}"
+elif [ "${first_arg}" = "store" ]; then
+    if [ ! "$#" -eq 2 ]; then
+        echo "Need precisely two arguments: archive name and path to archive."
+        false
+    fi
+    archive_name=$1
+    shift
+    to_backup="$@"
+    read_pw
+    cat "${config_file}" | while read line; do
+        first_char=$(echo "${line}" | cut -c1)
+        if [ "${first_char}" = "#" ]; then
+            continue
+        fi
+        repo="${line}:${standard_repo}"
+        archive="${repo}::${archive_name}-{utcnow:%Y-%m-%dT%H:%M}"
+        echo "Creating archive: ${archive}"
+        borg create --verbose --list "${archive}" "${to_backup}"
+    done
+elif [ "${first_arg}" = "check" ]; then
+    if [ ! "$#" -eq 0 ]; then
+        echo "Need no arguments"
+        false
+    fi
+    read_pw
+    cat "${config_file}" | while read line; do
+        first_char=$(echo "${line}" | cut -c1)
+        if [ "${first_char}" = "#" ]; then
+            continue
+        fi
+        repo="${line}:${standard_repo}"
+        echo "Checking repo: ${repo}"
+        borg check --verbose "${repo}"
+    done
+elif [ "${first_arg}" = "export_keyfiles" ]; then
+    if [ ! "$#" -eq 1 ]; then
+        echo "Need output tar file name."
+        false
+    fi
+    tar_target="${1}"
+    tmp_dir="${HOME}/.borgtmp"
+    keyfiles_dir="${tmp_dir}/borg_keyfiles"
+    mkdir -p "${keyfiles_dir}"
+    cat "${config_file}" | while read line; do
+        first_char=$(echo "${line}" | cut -c1)
+        if [ "${first_char}" = "#" ]; then
+            continue
+        fi
+        repo="${line}:${standard_repo}"
+        borg key export "${repo}" "${keyfiles_dir}/${line}"
+    done
+    cur_dir="$(pwd)"
+    cd "${tmp_dir}"
+    target=$(basename "${keyfiles_dir}")
+    tar cf "${tar_target}" "${target}"
+    mv "${tar_target}" "${cur_dir}"
+    cd
+    rm -rf "${tmp_dir}"
+elif [ "${first_arg}" = "orgpush" ]; then
+    archive_name="orgdir"
+    to_backup=~/org
+    read_pw
+    cat "${config_file}" | while read line; do
+        first_char=$(echo "${line}" | cut -c1)
+        if [ "${first_char}" = "#" ]; then
+            continue
+        fi
+        repo="${line}:${standard_repo}"
+        archive="${repo}::${archive_name}-{utcnow:%Y-%m-%dT%H:%M}"
+        echo "Creating archive: ${archive}"
+        borg create --verbose --list "${archive}" "${to_backup}" --exclude ~/org/.git
+    done
+elif [ "${first_arg}" = "orgpull" ]; then
+    archive_name="orgdir"
+    read_pw
+    cd /
+    cat "${config_file}" | while read line; do
+        first_char=$(echo "${line}" | cut -c1)
+        if [ "${first_char}" = "#" ]; then
+            continue
+        fi
+        repo="${line}:${standard_repo}"
+        archive=$(borg list "${repo}" | grep "${orgdir}" | tail -1 | cut -f1 -d' ')
+        echo "Pulling archive: ${archive}"
+        borg extract --verbose "${repo}::${archive}"
+        break
+    done
+else
+    usage
+fi
diff --git a/bookworm/home_files/user/.borgrepos b/bookworm/home_files/user/.borgrepos
new file mode 100644
index 0000000..c40eee3
--- /dev/null
+++ b/bookworm/home_files/user/.borgrepos
@@ -0,0 +1,4 @@
+plom@plomlompom.com
+plom@mail.plomlompom.com
+plom@play.plomlompom.com
+# file read ends at last newline
diff --git a/bookworm/home_files/user/.mbsyncrc b/bookworm/home_files/user/.mbsyncrc
new file mode 100644
index 0000000..6a0e5cd
--- /dev/null
+++ b/bookworm/home_files/user/.mbsyncrc
@@ -0,0 +1,28 @@
+IMAPAccount plom
+# Address to connect to
+Host mail.plomlompom.com
+User plom
+# For some reason, mbsync doesn't accept a PassCmd output beyond 79 chars,
+# therefore the pw in ~/.authinfo should not be longer than that.
+PassCmd "cat ~/.authinfo | cut -d' ' -f8-"
+SSLType IMAPS
+AuthMechs LOGIN
+
+IMAPStore core-remote
+Account plom
+
+MaildirStore core-local
+# The trailing "/" is important
+Path ~/mail/maildir/
+Inbox ~/mail/inbox/
+
+Channel core
+Master :core-remote:
+Slave :core-local:
+Patterns *
+# Automatically create missing mailboxes, both locally and on the server
+Create Both
+# Save the synchronization state files in the relevant directory
+SyncState *
+# If a mail is marked T ("Trashed") or deleted, remove it for real everywhere
+Expunge Both
diff --git a/bookworm/home_files/user/.notmuch-config b/bookworm/home_files/user/.notmuch-config
new file mode 100644
index 0000000..9532761
--- /dev/null
+++ b/bookworm/home_files/user/.notmuch-config
@@ -0,0 +1,9 @@
+[database]
+path=/home/plom/mail
+[search]
+exclude_tags=deleted;spam;
+# the fields below set the From: if the mail composer is called from
+# within notmuch
+[user]
+name=Christian Heller
+primary_email=plom@plomlompom.com
diff --git a/bookworm/home_files/user/public_repos/repos b/bookworm/home_files/user/public_repos/repos
new file mode 100644
index 0000000..27eb028
--- /dev/null
+++ b/bookworm/home_files/user/public_repos/repos
@@ -0,0 +1,7 @@
+# List of repos we want cloned in ~/public_repos
+config
+pingmail.git
+plomlombot-irc.git
+plomrogue
+plomrogue2-experiments
+plomvi.el
-- 
2.30.2