diff --git a/README.md b/README.md index 4943522..7129168 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # crater's script collection Some scripts that I wrote over the years and wanted to store with versioning -somewhere. Maybe some of them are useful for others. +somewhere. Maybe some of them are useful for others, some are probably not. Documentation may or may not follow, some scripts include a `--help` output. diff --git a/bin/alert b/bin/alert deleted file mode 100755 index b0fa120..0000000 --- a/bin/alert +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/zsh -time=$1; shift -echo "DISPLAY=:0.0 sm \"$@\"" | at $time diff --git a/bin/idea b/bin/idea deleted file mode 100755 index 625d80c..0000000 --- a/bin/idea +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/zsh -unset JAVA_HOME -source /etc/profile.d/11_oracle-jdk.sh -export IBUS_ENABLE_SYNC_MODE=1 -export XMODIFIERS="" -exec $(ls ${XDG_DATA_HOME:-~/.local/share}/idea-IU-*/bin/idea.sh | sort | tail -n 1) diff --git a/bin/venv b/bin/venv deleted file mode 100755 index c760019..0000000 --- a/bin/venv +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/zsh - -zparseopts -D -E c=cdhere -cdhere=cdhere h=help -help=help -if [[ -n $help ]]; then - <<-HELP - Usage: ${0:t} [-c] [DIR] - - If DIR contains bin/activate, start a shell with that virtualenv. - Otherwise recursively look up virtualenvs in DIR (by looking for dirs ending - in "venv") and select one with fzf. - - -c, --cdhere Change back to the current working directory instead of the - virtual env's base dir - HELP - exit -fi - -ORIGDIR=$PWD - -venvsh() { - cd $1 - . bin/activate - if [[ -n $cdhere ]]; then - cd $ORIGDIR - fi - $SHELL -} - -if [[ -n $1 && -e $1/bin/activate ]]; then - exec venvsh $1 -fi - -venv=$(fd -I -t d 'venv$' $1 | fzf) -if [[ -n $venv ]]; then - venvsh $venv -fi diff --git a/install.zsh b/install.zsh new file mode 100755 index 0000000..f3d838d --- /dev/null +++ b/install.zsh @@ -0,0 +1,49 @@ +#!/bin/zsh + +source ${$(realpath "$0"):h}/lib/common.zsh + +typeset -A interpreter_checked +have_interpreter() { + if [[ ! $interpreter_checked[$1] ]]; then + check "Checking for $1… " + if which ruby &> /dev/null; then + succeed + interpreter_checked[$1]=1 + return 0 + else + fail "not found in path" + return 1 + fi + fi + +} + +uses_interpreter() { + head -n 1 $2 | grep -q $1 +} + +if [[ -z $1 ]]; then + <<-HELP + Usage: $0 [-p PATH] + HELP + exit 1 +fi + +zparseopts -D -E p:=install_path -path:=install_path f=force -force=force + +if [[ ! $install_path ]]; then + install_path=$HOME/.local/bin +fi + +for prog in $@; do + for lang in zsh ruby python; do + if uses_interpreter $lang $prog; then + have_interpreter $lang || exit 1 + fi + done + if [[ -e $install_path/${prog:t} && ! $force ]]; then + error "$prog already exists at $install_path. Skipping." + else + ln -fsr $prog $install_path + fi +done diff --git a/lib/common.zsh b/lib/common.zsh new file mode 100644 index 0000000..119568f --- /dev/null +++ b/lib/common.zsh @@ -0,0 +1,41 @@ +autoload -Uz colors && colors + +local c_fail="${fg_bold[red]}✘$reset_color" +local c_success="${fg_bold[green]}✔$reset_color" + +check() { + echo -n " ? $*" +} + +result() { + echo -n "\e[s" + echo -n "\e[1G $1" + echo -n "\e[u" + shift + echo " $*" +} + +fail() { result $c_fail $@ } +succeed() { result $c_success $@ } +error() { echo -n " "; fail $@ } + +depend() { + local missing + local i + + for i in "$@"; do + type "$i" &>/dev/null || { + echo >&2 " $c_fail Missing dependency: $i. Please install it" + missing=1 + } + done + [[ -n "$missing" ]] && exit 1 +} + +urlencode() { + setopt extendedglob + input=$(/dev/null +echo "Alerting at $time on screen $DISPLAY" diff --git a/bin/autoless b/misc/autoless similarity index 100% rename from bin/autoless rename to misc/autoless diff --git a/bin/dbus-find b/misc/dbus-find similarity index 100% rename from bin/dbus-find rename to misc/dbus-find diff --git a/bin/dmjavadoc b/misc/dmjavadoc similarity index 100% rename from bin/dmjavadoc rename to misc/dmjavadoc diff --git a/bin/dmpc b/misc/dmpc similarity index 100% rename from bin/dmpc rename to misc/dmpc diff --git a/bin/dmscaladoc b/misc/dmscaladoc similarity index 100% rename from bin/dmscaladoc rename to misc/dmscaladoc diff --git a/bin/dmscrot b/misc/dmscrot similarity index 100% rename from bin/dmscrot rename to misc/dmscrot diff --git a/bin/dmsearch b/misc/dmsearch similarity index 90% rename from bin/dmsearch rename to misc/dmsearch index 646c50a..de3c73a 100755 --- a/bin/dmsearch +++ b/misc/dmsearch @@ -1,4 +1,7 @@ #!/bin/zsh + +source ${$(realpath "$0"):h:h}/lib/common.zsh + emulate -L zsh SYSTEM_CONFIG_PATH='/etc/dmsearch' @@ -7,21 +10,6 @@ CACHE_PATH="$HOME/.cache/dmsearch" mkdir -p "${CACHE_PATH}" # required software - -depend() { - local script="$1"; shift; - local missing - local i - - for i in "$@"; do - type "$i" &>/dev/null || { - echo >&2 "$i is required for ${script}. Please install it" - missing=1 - } - done - [ -n "$missing" ] && exit 1 -} - depend dmsearch \ rofi \ mv \ @@ -33,13 +21,6 @@ depend dmsearch \ # helpers ################################################################################ -urlencode() { - setopt extendedglob - input=$( /dev/null; then + echo $candidate + exit 0 + fi +done +echo "no terminal found" >&2 +exit 1 diff --git a/bin/fixenc b/misc/fixenc similarity index 100% rename from bin/fixenc rename to misc/fixenc diff --git a/misc/guiterm b/misc/guiterm new file mode 100755 index 0000000..5540c0d --- /dev/null +++ b/misc/guiterm @@ -0,0 +1,29 @@ +#!/bin/zsh + +if [[ "${0:t}" == "guiterm" ]]; then + <<-HELP + Symlink to g\$progname to start \$progname in tmux in a new terminal or + reattach a tmux session named \$progname in a new terminal + + Symlink to g.\$progname to start \$progname in tmux in a new terminal + regardless of existing tmux sessions" + + Example symlink names: + gmutt: start mutt in a terminal or open the existing one + g.zsh: open a terminal running a new zsh session + HELP + exit 1 +fi + +prog=${${0:t}[2,-1]} +if [[ "$prog[1]" == "." ]]; then + prog=${prog[2,-1]} + action="tmux new-session ${prog}" +elif tmux list-sessions | grep -qe "^${prog}:"; then + action="tmux attach -t ${prog}" +else + action="tmux new-session -s ${prog} ${prog}" +fi + +#exec alacritty --title G${prog} -e ${=action} +exec alacritty -t G${prog} -e ${=action} diff --git a/misc/idea b/misc/idea new file mode 100755 index 0000000..71437ba --- /dev/null +++ b/misc/idea @@ -0,0 +1,21 @@ +#!/bin/zsh +unset JAVA_HOME +source /etc/profile.d/11_oracle-jdk.sh +export IBUS_ENABLE_SYNC_MODE=1 +export XMODIFIERS="" + +dotfiles=( $HOME/.* ) +project_dirs=( + $HOME/learning + $HOME/teaching + $HOME/projects + $HOME/sandbox + $HOME/sources + $HOME/toy-projects + $HOME/work + ) + +exec firejail \ + --whitelist=${^dotfiles} \ + --whitelist=${^project_dirs} \ + $(ls ${XDG_DATA_HOME:-~/.local/share}/idea-IU-*/bin/idea.sh | sort | tail -n 1) diff --git a/misc/install-proton-ge.zsh b/misc/install-proton-ge.zsh new file mode 100755 index 0000000..ccbb128 --- /dev/null +++ b/misc/install-proton-ge.zsh @@ -0,0 +1,31 @@ +#!/bin/zsh +# installs or updates to latest release of Proton build by GloriousEggroll +# saves installed versions in $STEAM_DIR/compatibilitytools.d/proton-ge-tags + +autoload -Uz colors && colors + +if [[ ! -e $HOME/.steam/root/ ]]; then + if [[ -e $HOME/.var/app/com.valvesoftware.Steam/data/Steam ]]; then + #flatpak-version + COMPTOOLSDIR=$HOME/.var/app/com.valvesoftware.Steam/data/Steam/compatibilitytools.d/ + fi + echo "steam root not found" + exit 1 +else + COMPTOOLSDIR=$HOME/.steam/root/compatibilitytools.d +fi + +mkdir -p $COMPTOOLSDIR + +latest_json=$(curl -s https://api.github.com/repos/GloriousEggroll/proton-ge-custom/releases/latest) +release_tag=$(jq -r .tag_name <<<$latest_json) + +installed_tags="$COMPTOOLSDIR/proton-ge-tags" + +if grep -q "^$release_tag\$" "$installed_tags"; then + echo "${fg_bold[yellow]}Current release $release_tag already installed.$reset_color" +else + echo $release_tag >> .steam/root/compatibilitytools.d/proton-ge-tags + curl -L $(jq -r '.tarball_url'<<<"$latest_json") | tar -C $COMPTOOLSDIR -xvz + echo "${fg_bold[green]}Installed release $release_tag.$reset_color" +fi diff --git a/bin/ipy b/misc/ipy similarity index 100% rename from bin/ipy rename to misc/ipy diff --git a/bin/mdcat b/misc/mdcat similarity index 100% rename from bin/mdcat rename to misc/mdcat diff --git a/bin/mkscript b/misc/mkscript similarity index 100% rename from bin/mkscript rename to misc/mkscript diff --git a/bin/newmails b/misc/newmails similarity index 100% rename from bin/newmails rename to misc/newmails diff --git a/misc/pathtest b/misc/pathtest new file mode 100755 index 0000000..eaaa581 --- /dev/null +++ b/misc/pathtest @@ -0,0 +1,3 @@ +#!/bin/zsh + +source ${$(realpath "$0"):h:h}/lib diff --git a/bin/pdftosvg b/misc/pdftosvg similarity index 100% rename from bin/pdftosvg rename to misc/pdftosvg diff --git a/bin/pomdep-graph b/misc/pomdep-graph similarity index 100% rename from bin/pomdep-graph rename to misc/pomdep-graph diff --git a/bin/qrshow b/misc/qrshow similarity index 100% rename from bin/qrshow rename to misc/qrshow diff --git a/bin/re b/misc/re similarity index 92% rename from bin/re rename to misc/re index 093cf53..5123ca0 100755 --- a/bin/re +++ b/misc/re @@ -27,9 +27,9 @@ fi if [[ -e $SCRIPTDIR/$1 ]]; then if [[ -n $dry_run ]]; then - $PAGER -F $SCRIPTDIR/$1 + ${PAGER:-less} -F $SCRIPTDIR/$1 elif [[ -n $edit ]]; then - $EDITOR $SCRIPTDIR/$1 + ${EDITOR:-vim} $SCRIPTDIR/$1 else script=$1 shift diff --git a/misc/share b/misc/share new file mode 100755 index 0000000..bcf4532 --- /dev/null +++ b/misc/share @@ -0,0 +1,8 @@ +#!/bin/zsh +file_id=$(head -c 12 /dev/urandom|base64| tr '/' '+') +file_name=${2:-${1:t}} + +ssh qwerty mkdir -p ~/share/$file_id/ +rsync -P $1 qwerty:share/$file_id/$file_name +ssh qwerty ~/update-www-perms.sh +echo https://share.qwertyuiop.de/$file_id/$file_name diff --git a/misc/sweechat b/misc/sweechat new file mode 100755 index 0000000..6daeda8 --- /dev/null +++ b/misc/sweechat @@ -0,0 +1,10 @@ +#!/bin/zsh +if [[ "$1" == "-S" ]]; then + shift + server=${1}; + shift +else + server="qwerty"; +fi + +exec alacritty -t Weechat -e tmux new-session mosh "${server}" -- tmux attach "$@" -t weechat diff --git a/bin/tmux-url-fuzz b/misc/tmux-url-fuzz similarity index 100% rename from bin/tmux-url-fuzz rename to misc/tmux-url-fuzz diff --git a/bin/unlock-parse b/misc/unlock-parse similarity index 100% rename from bin/unlock-parse rename to misc/unlock-parse diff --git a/bin/unlock-track b/misc/unlock-track similarity index 100% rename from bin/unlock-track rename to misc/unlock-track diff --git a/bin/update-amm b/misc/update-amm similarity index 100% rename from bin/update-amm rename to misc/update-amm diff --git a/misc/venv b/misc/venv new file mode 100755 index 0000000..3cd2af0 --- /dev/null +++ b/misc/venv @@ -0,0 +1,55 @@ +#!/bin/zsh + +zparseopts -D -E c=cdhere -cdvenv=cdvenv h=help -help=help n=newvenv -new=newvenv t=tmsu -tmsu=tmsu +if [[ -n $help ]]; then + <<-HELP + Usage: ${0:t} [-c] [DIR] + ${0:t} -t + + If DIR contains bin/activate, start a shell with that virtualenv. + Otherwise recursively look up virtualenvs in DIR (by looking for dirs ending + in "venv") and select one with fzf. + + -c, --cdvenv Change to the venv directory instead of going back to the + current working directory + -t, --tmsu list only venvs in a directory with tmsu tag lang=python + HELP + exit +fi + +ORIGDIR=$PWD + +venvsh() { + cd $1 + . bin/activate + if [[ -z $cdvenv ]]; then + cd $ORIGDIR + fi + $SHELL +} + +if [[ -n $tmsu ]]; then + venv=$( + for i in $(tmsu files --directory lang=python); do + [[ -d $i/venv ]] && echo $i/venv + done | fzf) + if [[ -n $venv ]]; then + exec venvsh $venv + else + exit 1 + fi +fi + +if [[ -n $newvenv ]]; then + python -mvenv ${1:-venv} + exec venvsh ${1:-venv} +fi + +if [[ -n $1 && -e $1/bin/activate ]]; then + exec venvsh $1 +fi + +venv=$(fd -I -t d 'venv$' $1 | fzf) +if [[ -n $venv ]]; then + exec venvsh $venv +fi diff --git a/bin/vf b/misc/vf similarity index 100% rename from bin/vf rename to misc/vf diff --git a/bin/webapp b/misc/webapp similarity index 52% rename from bin/webapp rename to misc/webapp index 39a7625..8aead7a 100755 --- a/bin/webapp +++ b/misc/webapp @@ -1,10 +1,12 @@ #!/bin/zsh + +source ${$(realpath "$0"):h:h}/lib/common.zsh + profile="${0:t}" -need firefox -need firejail +depend firefox firejail mkdir -p $HOME/.jails/firefox-$profile exec firejail --private=$HOME/.jails/firefox-$profile \ - firefox --no-remote "$@" + firefox --class $profile --no-remote "$@" diff --git a/bin/webapp-cp b/misc/webapp-cp similarity index 100% rename from bin/webapp-cp rename to misc/webapp-cp diff --git a/bin/xc b/misc/xc similarity index 94% rename from bin/xc rename to misc/xc index a4eaaf3..62c576f 100755 --- a/bin/xc +++ b/misc/xc @@ -3,8 +3,8 @@ # install this program with the name xc and symlink it as xs. # xc will use the clipboard selection, while xs while use the primary selection -source $(which need) -need xclip +source ${$(realpath "$0"):h:h}/lib/common.zsh +depend xclip function get_primary() { xclip -o -selection primary } function get_clipboard() { xclip -o -selection clipboard } @@ -19,7 +19,7 @@ function show_both() { } function read_both() { - need pee moreutils + depend pee pee "xclip -i -selection primary" "xclip -i -selection clipboard" } @@ -35,7 +35,7 @@ function common() { "showboth") show_both ;; "sed") $getc | sed -e "$@" | $setc; $getc;; "pipe") $getc | "$@" | $setc; $getc;; - "edit") need vipe moreutils; $getc | vipe | $setc; $getc;; + "edit") depend vipe; $getc | vipe | $setc; $getc;; "") if [[ -t 0 ]]; then $getc; else $setc; fi;; *) <<-HELP Usage: xc [COMMAND] diff --git a/misc/zenity-askpass b/misc/zenity-askpass new file mode 100755 index 0000000..59263e4 --- /dev/null +++ b/misc/zenity-askpass @@ -0,0 +1,2 @@ +#!/bin/zsh +zenity --password --title="sudo password prompt" --timeout=10 diff --git a/void/xbps-worldfile b/void/xbps-worldfile new file mode 100755 index 0000000..ce0f319 --- /dev/null +++ b/void/xbps-worldfile @@ -0,0 +1,66 @@ +#!/bin/zsh + +zparseopts -D -E h=help -help=help d=desc -describe=desc + +function not-in-left() { + awk 'NR==FNR{a[$0]=1;next}!a[$0]' "$@" +} + +function first-col() { + cut -d$'\t' -f1 +} + +function info() { + printf "$@" >&2 +} + +if [[ -n $help ]]; then + echo 'Usage: xbps-worldfile [-d]' + echo 'Edit a list of manually installed packages in your editor and update the pkgdb' + echo + echo ' -d, --describe Query short description for packages and display it in the list (slow)' + exit +fi + +TMPFILE=$(mktemp -p ${TMPDIR:-/tmp} xbps-worldfile.XXXXXXXXXX) + +if [[ -n $desc ]]; then + info "Generating worldfile with descriptions\n" + xbps-query -m | while read pkg; do + info . + printf "%-40s\t%s\n" $pkg "$(xbps-query -p short_desc $pkg)" + done > $TMPFILE +else + xbps-query -m > $TMPFILE +fi + +EDITFILE=$(mktemp -p ${TMPDIR:-/tmp} xbps-worldfile.edit.XXXXXXXXXX) +cp $TMPFILE $EDITFILE + +$EDITOR $EDITFILE + +autopkgs=$(not-in-left $EDITFILE $TMPFILE | first-col) +manualpkgs=$(not-in-left $TMPFILE $EDITFILE | first-col) +rm $TMPFILE $EDITFILE + +if [[ -z $manualpkgs && -z $autopkgs ]]; then + info "No changes.\n" + exit +fi +info "\e[92minstalling:\e[0m\n${manualpkgs}\n\n" +info "\e[93mchanging to auto:\e[0m\n${autopkgs}\n\n" + + +info "\e[1mContinue? [y/n]:" +if ! read -q; then exit; fi + +if [[ -n $manualpkgs ]]; then + xbps-install $=manualpkgs + xbps-pkgdb -m manual $=manualpkgs +fi + +if [[ -n $autopkgs ]]; then + xbps-pkgdb -m auto $=autopkgs + info "\e[93mcleaning orphans\e[0m" + xbps-remove -Ro +fi diff --git a/void/xrevdep b/void/xrevdep new file mode 100755 index 0000000..9fa5a5f --- /dev/null +++ b/void/xrevdep @@ -0,0 +1,23 @@ +#!/bin/zsh + +CLEAR="\e[2K\e[${COLUMNS}D" +printc() { + printf "$CLEAR\e[32m%s\e[0m" "$*" >&2 +} + +revdep() { + printc "> $1" + parents=$(xbps-query -X "$1") + + if [[ -z $parents ]]; then + echo $1 + else + while IFS= read -r i; do + revdep $i + done <<<"$parents" + fi +} + +result=$(revdep "$1") +printc +sort -u <<<"$result"