From 43c9825e49d25fbd2c19abcdeb8f73aee8be2a4c Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Wed, 14 May 2025 10:50:27 +0200 Subject: ... --- accounts/gkleen@sif/zshrc | 80 +++++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 37 deletions(-) (limited to 'accounts/gkleen@sif/zshrc') diff --git a/accounts/gkleen@sif/zshrc b/accounts/gkleen@sif/zshrc index 06f6f6f2..7645e0fc 100644 --- a/accounts/gkleen@sif/zshrc +++ b/accounts/gkleen@sif/zshrc @@ -6,11 +6,10 @@ dir() { forceShell=false wormhole=false gitWorktree="" - # notmuchMsg="" - quickserve=false modifyPDF="" + miniserve=false - while getopts ':t:a:d:ir:wqg:p:' arg; do + while getopts ':t:a:d:ir:wg:p:m' arg; do case $arg in "t") ;; "a") @@ -26,9 +25,8 @@ dir() { "r") repoUrl=${OPTARG} ;; "w") wormhole=true ;; "g") gitWorktree=${OPTARG} ;; - # "n") notmuchMsg=${OPTARG} ;; - "q") quickserve=true ;; "p") modifyPDF=${OPTARG:a} ;; + "m") miniserve=true ;; *) printf "Invalid option: %s\n" $arg >&2; exit 2 ;; esac done @@ -52,17 +50,29 @@ dir() { gitWorktree="" fi + miniservePIDFile="" + if [[ ${miniserve} = "true" ]]; then + miniservePIDFile=$(mktemp --tmpdir --suffix=.pid) + fi + cleanup() { - cd ${modifyPDF:h} if [[ -n ${modifyPDF} ]]; then + cd ${modifyPDF:h} typeset -a pages eval 'pages=(${dir}/${modifyPDF:t:r}_*.png(on))' magick -verbose "$pages" ${modifyPDF} + modifyPDF="" + fi + if [[ -n ${miniservePIDFile} ]]; then + kill --verbose $(cat ${miniservePIDFile}) && wait $(cat ${miniservePIDFile}) + miniservePIDFile="" fi } ( + set -o localtraps + trap 'return 1' INT TERM trap cleanup EXIT cd ${dir} @@ -135,18 +145,18 @@ dir() { [[ $wormhole = "true" ]] && wormhole receive --accept-file - if [[ $quickserve = "true" ]]; then - quickserve --root . --upload . --show-hidden --tar gz + if [[ ${#@} -gt 0 ]]; then + ${@} fi + cd $(pwd) # Needed for mounting to work - if [[ ${#@} -eq 0 ]] || [[ $forceShell = "true" ]]; then - if [[ ${#@} -gt 0 ]]; then - ${@} - fi - - cd $(pwd) # Needed for mounting to work + if [[ ${miniserve} = "true" ]]; then + miniserve --random-route --hidden --enable-tar-gz --enable-zip . & + echo $! > "${miniservePIDFile}" + fi + if [[ ${#@} -eq 0 ]] && [[ ${miniserve} != "true" ]] || [[ $forceShell = "true" ]]; then isSingleDir() { typeset -a contents contents=(*(N) .*(N)) @@ -160,10 +170,9 @@ dir() { } while d=$(isSingleDir); do cd ${d}; done - zsh - else - ${@} + elif [[ ${miniserve} == "true" ]]; then + wait $(cat "${miniservePIDFile}") fi ) } @@ -171,27 +180,30 @@ dir() { tmpdir() { cleanup() { - cd / + cd / unmount() { printf "Unmounting %s\n" ${1} >&2 fusermount -u ${1} || umount ${1} || sudo umount ${1} } - if mountpoint -q -- ${dir}; then - unmount ${dir} || return $? - else - while read -d $'\0' subDir; do - mountpoint -q -- ${subDir} || continue - unmount ${subDir} || return $? - done <<<$(find ${dir} -xdev -type d -print0 | sort -zr) - fi - - rm -rfv --one-file-system -- ${dir} + if [[ -n ${dir} ]]; then + if mountpoint -q -- ${dir}; then + unmount ${dir} || return $? + else + while read -d $'\0' subDir; do + mountpoint -q -- ${subDir} || continue + unmount ${subDir} || return $? + done <<<$(find ${dir} -xdev -type d -print0 | sort -zr) + fi + + rm -rfv --one-file-system -- ${dir} + dir="" + fi } local tmpdir="" - while getopts ':t:a:s:Sd:ir:wqg:p:' arg; do + while getopts ':t:a:d:ir:wg:p:m' arg; do case $arg in "t") tmpdir="=${OPTARG}" ;; "?"|":") printf "Invalid option: %s\n" $arg >&2; exit 2 ;; @@ -199,6 +211,8 @@ tmpdir() { done ( + set -o localtraps + trap 'return 1' INT TERM trap cleanup EXIT @@ -247,14 +261,6 @@ l() { ls --long --binary --git --time-style=iso --header $@ } -re() { - systemctl restart $@ -} - -ure() { - systemctl --user restart $@ -} - ssh-installer() { ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/gkleen@sif.midgard.yggdrasil $@ } -- cgit v1.2.3