diff options
Diffstat (limited to 'accounts/gkleen@sif/zshrc')
| -rw-r--r-- | accounts/gkleen@sif/zshrc | 140 |
1 files changed, 63 insertions, 77 deletions
diff --git a/accounts/gkleen@sif/zshrc b/accounts/gkleen@sif/zshrc index c628e2e9..702990c3 100644 --- a/accounts/gkleen@sif/zshrc +++ b/accounts/gkleen@sif/zshrc | |||
| @@ -2,17 +2,14 @@ dir() { | |||
| 2 | curlArchive=false | 2 | curlArchive=false |
| 3 | templateArchive="" | 3 | templateArchive="" |
| 4 | repoUrl="" | 4 | repoUrl="" |
| 5 | nixShell="" | ||
| 6 | findNix=false | ||
| 7 | dir="" | 5 | dir="" |
| 8 | forceShell=false | 6 | forceShell=false |
| 9 | wormhole=false | 7 | wormhole=false |
| 10 | gitWorktree="" | 8 | gitWorktree="" |
| 11 | # notmuchMsg="" | ||
| 12 | quickserve=false | ||
| 13 | modifyPDF="" | 9 | modifyPDF="" |
| 10 | miniserve=false | ||
| 14 | 11 | ||
| 15 | while getopts ':t:a:s:Sd:ir:wqg:p:' arg; do | 12 | while getopts ':t:a:d:ir:wg:p:m' arg; do |
| 16 | case $arg in | 13 | case $arg in |
| 17 | "t") ;; | 14 | "t") ;; |
| 18 | "a") | 15 | "a") |
| @@ -23,16 +20,13 @@ dir() { | |||
| 23 | templateArchive=${OPTARG:a} | 20 | templateArchive=${OPTARG:a} |
| 24 | fi | 21 | fi |
| 25 | ;; | 22 | ;; |
| 26 | "s") nixShell=${OPTARG:a} ;; | ||
| 27 | "S") findNix=true ;; | ||
| 28 | "d") dir=${OPTARG} ;; | 23 | "d") dir=${OPTARG} ;; |
| 29 | "i") forceShell=true ;; | 24 | "i") forceShell=true ;; |
| 30 | "r") repoUrl=${OPTARG} ;; | 25 | "r") repoUrl=${OPTARG} ;; |
| 31 | "w") wormhole=true ;; | 26 | "w") wormhole=true ;; |
| 32 | "g") gitWorktree=${OPTARG} ;; | 27 | "g") gitWorktree=${OPTARG} ;; |
| 33 | # "n") notmuchMsg=${OPTARG} ;; | ||
| 34 | "q") quickserve=true ;; | ||
| 35 | "p") modifyPDF=${OPTARG:a} ;; | 28 | "p") modifyPDF=${OPTARG:a} ;; |
| 29 | "m") miniserve=true ;; | ||
| 36 | *) printf "Invalid option: %s\n" $arg >&2; exit 2 ;; | 30 | *) printf "Invalid option: %s\n" $arg >&2; exit 2 ;; |
| 37 | esac | 31 | esac |
| 38 | done | 32 | done |
| @@ -56,20 +50,34 @@ dir() { | |||
| 56 | gitWorktree="" | 50 | gitWorktree="" |
| 57 | fi | 51 | fi |
| 58 | 52 | ||
| 53 | miniservePIDFile="" | ||
| 54 | if [[ ${miniserve} = "true" ]]; then | ||
| 55 | miniservePIDFile=$(mktemp --tmpdir --suffix=.pid) | ||
| 56 | fi | ||
| 57 | |||
| 59 | cleanup() | 58 | cleanup() |
| 60 | { | 59 | { |
| 61 | cd ${modifyPDF:h} | 60 | if [[ -n ${modifyPDF} ]]; then |
| 62 | [[ -n ${modifyPDF} ]] && nix shell 'nixos#imagemagick' -c convert -verbose ${dir}/${modifyPDF:t:r}_*.png(on) ${modifyPDF} | 61 | cd ${modifyPDF:h} |
| 62 | typeset -a pages | ||
| 63 | eval 'pages=(${dir}/${modifyPDF:t:r}_*.png(on))' | ||
| 64 | magick -verbose "$pages" ${modifyPDF} | ||
| 65 | modifyPDF="" | ||
| 66 | fi | ||
| 67 | if [[ -n ${miniservePIDFile} ]]; then | ||
| 68 | command kill --verbose -- $(cat ${miniservePIDFile}) && wait $(cat ${miniservePIDFile}) | ||
| 69 | miniservePIDFile="" | ||
| 70 | fi | ||
| 63 | } | 71 | } |
| 64 | 72 | ||
| 65 | ( | 73 | ( |
| 74 | set -o localoptions -o localtraps | ||
| 75 | trap 'return 1' INT TERM | ||
| 66 | trap cleanup EXIT | 76 | trap cleanup EXIT |
| 67 | 77 | ||
| 68 | cd ${dir} | 78 | cd ${dir} |
| 69 | export dir; | 79 | export dir; |
| 70 | 80 | ||
| 71 | ${findNix} && { nixShell=$(findNix) || return $? } | ||
| 72 | |||
| 73 | [[ -n ${repoUrl} ]] && git clone -- ${repoUrl} . | 81 | [[ -n ${repoUrl} ]] && git clone -- ${repoUrl} . |
| 74 | 82 | ||
| 75 | [[ -n ${modifyPDF} ]] && templateArchive=${modifyPDF} | 83 | [[ -n ${modifyPDF} ]] && templateArchive=${modifyPDF} |
| @@ -82,23 +90,23 @@ dir() { | |||
| 82 | } | 90 | } |
| 83 | trap cleanup EXIT | 91 | trap cleanup EXIT |
| 84 | 92 | ||
| 85 | if ${curlArchive}; then | 93 | if [[ $curlArchive = "true" ]]; then |
| 86 | archiveFile=$(mktemp -t "archive.XXXXXXXXXX.${templateArchive:t}") | 94 | archiveFile=$(mktemp -t "archive.XXXXXXXXXX.${templateArchive:t}") |
| 87 | 95 | ||
| 88 | curl -L -o ${archiveFile} ${templateArchive} | 96 | curl -SfL -o ${archiveFile} ${templateArchive} |
| 89 | 97 | ||
| 90 | templateArchive=${archiveFile} | 98 | templateArchive=${archiveFile} |
| 91 | fi | 99 | fi |
| 92 | 100 | ||
| 93 | unpack=true | 101 | unpack=true |
| 94 | while ${unpack}; do | 102 | while [[ $unpack = "true" ]]; do |
| 95 | case $(file --brief --mime-type --dereference ${templateArchive}) in | 103 | case $(file --brief --mime-type --dereference ${templateArchive}) in |
| 96 | application/zip) | 104 | application/zip) |
| 97 | unzip ${templateArchive} | 105 | unzip ${templateArchive} |
| 98 | unpack=false | 106 | unpack=false |
| 99 | ;; | 107 | ;; |
| 100 | application/vnd.debian.binary-package) | 108 | application/vnd.debian.binary-package) |
| 101 | nix shell 'nixos#binutils' --command ar x ${templateArchive} | 109 | ar x ${templateArchive} |
| 102 | mkdir control data | 110 | mkdir control data |
| 103 | tar -C control -xvaf control.* | 111 | tar -C control -xvaf control.* |
| 104 | tar -C data -xvaf data.* | 112 | tar -C data -xvaf data.* |
| @@ -106,7 +114,7 @@ dir() { | |||
| 106 | ;; | 114 | ;; |
| 107 | application/x-rpm) | 115 | application/x-rpm) |
| 108 | cpioArchive=$(mktemp -t "archive.XXXXXXXXXX.${templateArchive:t:r}.cpio") | 116 | cpioArchive=$(mktemp -t "archive.XXXXXXXXXX.${templateArchive:t:r}.cpio") |
| 109 | nix shell 'nixos#busybox' --command rpm2cpio ${templateArchive} > ${cpioArchive} | 117 | rpm2cpio ${templateArchive} > ${cpioArchive} |
| 110 | templateArchive=${cpioArchive} | 118 | templateArchive=${cpioArchive} |
| 111 | unpack=true | 119 | unpack=true |
| 112 | ;; | 120 | ;; |
| @@ -115,15 +123,19 @@ dir() { | |||
| 115 | unpack=false | 123 | unpack=false |
| 116 | ;; | 124 | ;; |
| 117 | application/pdf) | 125 | application/pdf) |
| 118 | nix shell 'nixos#ghostscript' 'nixos#imagemagick' -c convert -verbose -density 400 ${templateArchive} ${modifyPDF:t:r}_%0d.png | 126 | magick -verbose -density 400 ${templateArchive} ${modifyPDF:t:r}_%0d.png |
| 119 | unpack=false | 127 | unpack=false |
| 120 | ;; | 128 | ;; |
| 121 | application/octet-stream) | 129 | application/octet-stream) |
| 122 | if [[ $(file --brief --dereferenc ${templateArchive}) =~ Squashfs ]]; then | 130 | if [[ $(file --brief --dereference ${templateArchive}) =~ Squashfs ]]; then |
| 123 | nix shell 'nixos#squashfsTools' -c unsquashfs -d . ${templateArchive} | 131 | unsquashfs -d . ${templateArchive} |
| 124 | unpack=false | 132 | unpack=false |
| 125 | fi | 133 | fi |
| 126 | ;; | 134 | ;; |
| 135 | application/x-iso9660-image) | ||
| 136 | 7z x ${templateArchive} | ||
| 137 | unpack=false | ||
| 138 | ;; | ||
| 127 | *) | 139 | *) |
| 128 | tar -xvaf ${templateArchive} | 140 | tar -xvaf ${templateArchive} |
| 129 | unpack=false | 141 | unpack=false |
| @@ -134,25 +146,21 @@ dir() { | |||
| 134 | fi | 146 | fi |
| 135 | 147 | ||
| 136 | 148 | ||
| 137 | ${wormhole} && wormhole receive --accept-file | 149 | [[ $wormhole = "true" ]] && wormhole receive --accept-file |
| 138 | 150 | ||
| 139 | 151 | ||
| 140 | if ${quickserve}; then | 152 | if [[ ${#@} -gt 0 ]]; then |
| 141 | quickserve --root . --upload . --show-hidden --tar gz | 153 | ${@} |
| 142 | fi | 154 | fi |
| 143 | 155 | ||
| 156 | cd $(pwd) # Needed for mounting to work | ||
| 144 | 157 | ||
| 145 | if [[ ${#@} -eq 0 ]] || ${forceShell}; then | 158 | if [[ ${miniserve} = "true" ]]; then |
| 146 | if [[ ${#@} -gt 0 ]]; then | 159 | miniserve --random-route --hidden --enable-tar-gz --enable-zip . & |
| 147 | if [[ -z ${nixShell} ]]; then | 160 | echo $! > "${miniservePIDFile}" |
| 148 | ${@} | 161 | fi |
| 149 | else | ||
| 150 | nix-shell ${nixShell} --run "${@}" | ||
| 151 | fi | ||
| 152 | fi | ||
| 153 | |||
| 154 | cd $(pwd) # Needed for mounting to work | ||
| 155 | 162 | ||
| 163 | if [[ ${#@} -eq 0 ]] && [[ ${miniserve} != "true" ]] || [[ $forceShell = "true" ]]; then | ||
| 156 | isSingleDir() { | 164 | isSingleDir() { |
| 157 | typeset -a contents | 165 | typeset -a contents |
| 158 | contents=(*(N) .*(N)) | 166 | contents=(*(N) .*(N)) |
| @@ -166,18 +174,9 @@ dir() { | |||
| 166 | } | 174 | } |
| 167 | while d=$(isSingleDir); do cd ${d}; done | 175 | while d=$(isSingleDir); do cd ${d}; done |
| 168 | 176 | ||
| 169 | 177 | zsh | |
| 170 | if [[ -z ${nixShell} ]]; then | 178 | elif [[ ${miniserve} == "true" ]]; then |
| 171 | zsh | 179 | wait $(cat "${miniservePIDFile}") |
| 172 | else | ||
| 173 | nix-shell ${nixShell} --run zsh | ||
| 174 | fi | ||
| 175 | else | ||
| 176 | if [[ -z ${nixShell} ]]; then | ||
| 177 | ${@} | ||
| 178 | else | ||
| 179 | nix-shell ${nixShell} --run "${@}" | ||
| 180 | fi | ||
| 181 | fi | 180 | fi |
| 182 | ) | 181 | ) |
| 183 | } | 182 | } |
| @@ -185,27 +184,30 @@ dir() { | |||
| 185 | tmpdir() { | 184 | tmpdir() { |
| 186 | cleanup() | 185 | cleanup() |
| 187 | { | 186 | { |
| 188 | cd / | 187 | cd / |
| 189 | unmount() { | 188 | unmount() { |
| 190 | printf "Unmounting %s\n" ${1} >&2 | 189 | printf "Unmounting %s\n" ${1} >&2 |
| 191 | fusermount -u ${1} || umount ${1} || sudo umount ${1} | 190 | fusermount -u ${1} || umount ${1} || sudo umount ${1} |
| 192 | } | 191 | } |
| 193 | 192 | ||
| 194 | if mountpoint -q -- ${dir}; then | 193 | if [[ -n ${dir} ]]; then |
| 195 | unmount ${dir} || return $? | 194 | if mountpoint -q -- ${dir}; then |
| 196 | else | 195 | unmount ${dir} || return $? |
| 197 | while read -d $'\0' subDir; do | 196 | else |
| 198 | mountpoint -q -- ${subDir} || continue | 197 | while read -d $'\0' subDir; do |
| 199 | unmount ${subDir} || return $? | 198 | mountpoint -q -- ${subDir} || continue |
| 200 | done <<<$(find ${dir} -xdev -type d -print0 | sort -zr) | 199 | unmount ${subDir} || return $? |
| 201 | fi | 200 | done <<<$(find ${dir} -xdev -type d -print0 | sort -zr) |
| 202 | 201 | fi | |
| 203 | rm -rfv --one-file-system -- ${dir} | 202 | |
| 203 | rm -rfv --one-file-system -- ${dir} | ||
| 204 | dir="" | ||
| 205 | fi | ||
| 204 | } | 206 | } |
| 205 | 207 | ||
| 206 | local tmpdir="" | 208 | local tmpdir="" |
| 207 | 209 | ||
| 208 | while getopts ':t:a:s:Sd:ir:wqg:p:' arg; do | 210 | while getopts ':t:a:d:ir:wg:p:m' arg; do |
| 209 | case $arg in | 211 | case $arg in |
| 210 | "t") tmpdir="=${OPTARG}" ;; | 212 | "t") tmpdir="=${OPTARG}" ;; |
| 211 | "?"|":") printf "Invalid option: %s\n" $arg >&2; exit 2 ;; | 213 | "?"|":") printf "Invalid option: %s\n" $arg >&2; exit 2 ;; |
| @@ -213,6 +215,8 @@ tmpdir() { | |||
| 213 | done | 215 | done |
| 214 | 216 | ||
| 215 | ( | 217 | ( |
| 218 | set -o localoptions -o localtraps | ||
| 219 | trap 'return 1' INT TERM | ||
| 216 | trap cleanup EXIT | 220 | trap cleanup EXIT |
| 217 | 221 | ||
| 218 | 222 | ||
| @@ -231,17 +235,7 @@ clock() { | |||
| 231 | } | 235 | } |
| 232 | 236 | ||
| 233 | public-ip() { | 237 | public-ip() { |
| 234 | curl -s -H 'Accept: application/json' $@ ifconfig.co | jq -r '.ip' | 238 | curl -sSf -H 'Accept: application/json' $@ ifconfig.co | jq -r '.ip' |
| 235 | } | ||
| 236 | |||
| 237 | nix-ghci() { | ||
| 238 | pkgExpr="" | ||
| 239 | if [[ ${#@} -gt 0 ]]; then | ||
| 240 | pkgExpr="${1}" | ||
| 241 | shift | ||
| 242 | fi | ||
| 243 | |||
| 244 | nix-shell -p "with (import <nixpkgs> {}); pkgs.haskellPackages.ghcWithPackages (p: with p; [${pkgExpr}])" --run "ghci ${@}" | ||
| 245 | } | 239 | } |
| 246 | 240 | ||
| 247 | swap() { | 241 | swap() { |
| @@ -271,14 +265,6 @@ l() { | |||
| 271 | ls --long --binary --git --time-style=iso --header $@ | 265 | ls --long --binary --git --time-style=iso --header $@ |
| 272 | } | 266 | } |
| 273 | 267 | ||
| 274 | re() { | ||
| 275 | systemctl restart $@ | ||
| 276 | } | ||
| 277 | |||
| 278 | ure() { | ||
| 279 | systemctl --user restart $@ | ||
| 280 | } | ||
| 281 | |||
| 282 | ssh-installer() { | 268 | ssh-installer() { |
| 283 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/gkleen@sif.midgard.yggdrasil $@ | 269 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/gkleen@sif.midgard.yggdrasil $@ |
| 284 | } | 270 | } |
