From 321d9a8247d50cd634615ffed707aed3f4376108 Mon Sep 17 00:00:00 2001 From: Tharre Date: Wed, 9 May 2018 02:58:18 +0200 Subject: Move various scripts and functions into bin/ --- .mbsyncrc | 2 +- .msmtprc | 2 +- .req_pwd.sh | 19 ----------- .zshrc | 89 +--------------------------------------------------- bin/.gitignore | 11 +++++++ bin/play | 12 +++++++ bin/req_pwd.sh | 19 +++++++++++ bin/reset_usb | 5 +++ bin/set_xdg_dirs | 13 ++++++++ bin/setup_borgbackup | 18 +++++++++++ bin/suspend_after | 6 ++++ bin/update_dotfiles | 20 ++++++++++++ bin/ut8font_test | 3 ++ install.sh | 3 ++ 14 files changed, 113 insertions(+), 109 deletions(-) delete mode 100755 .req_pwd.sh create mode 100644 bin/.gitignore create mode 100755 bin/play create mode 100755 bin/req_pwd.sh create mode 100755 bin/reset_usb create mode 100755 bin/set_xdg_dirs create mode 100755 bin/setup_borgbackup create mode 100755 bin/suspend_after create mode 100755 bin/update_dotfiles create mode 100755 bin/ut8font_test diff --git a/.mbsyncrc b/.mbsyncrc index b809cc2..bab3a04 100644 --- a/.mbsyncrc +++ b/.mbsyncrc @@ -3,7 +3,7 @@ IMAPAccount tharre3@gmail.com # widely supported at this time Host imaps:imap.gmail.com User tharre3@gmail.com -PassCmd "~/.req_pwd.sh email/tharre3@gmail.com" +PassCmd "req_pwd.sh email/tharre3@gmail.com" # SSLType IMAPS SSLVersion TLSv1.2 CertificateFile /etc/ssl/certs/ca-certificates.crt diff --git a/.msmtprc b/.msmtprc index 3ccf8c9..32b7b73 100644 --- a/.msmtprc +++ b/.msmtprc @@ -11,7 +11,7 @@ host smtp.gmail.com port 587 from tharre3@gmail.com user tharre3@gmail.com -passwordeval "~/.req_pwd.sh email/tharre3@gmail.com" +passwordeval "req_pwd.sh email/tharre3@gmail.com" # Set a default account account default : gmail diff --git a/.req_pwd.sh b/.req_pwd.sh deleted file mode 100755 index 3a6be86..0000000 --- a/.req_pwd.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/sh -e -# A simple wrapper script to deal with situations where pass, password-store, or -# my gpg key is not available. - -pass "$1" && exit - -# maybe gpg-agent is acting up, retry after restarting it -gpg-connect-agent "KILLAGENT" /bye >&2 >/dev/null -gpg-connect-agent /bye >&2 >/dev/null -pass "$1" && exit - -# if pass fails, request password manually -stty -echo -printf "Password: " >&2 -read passwd -stty echo -printf "\n" >&2 - -printf "$passwd" diff --git a/.zshrc b/.zshrc index c059206..17025f4 100644 --- a/.zshrc +++ b/.zshrc @@ -108,95 +108,8 @@ function 256color_test { done ) } -function utf8font_test { - curl http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-demo.txt -} - -# requires youtube-dl, mpv -function play { - # Skip DASH manifest for speed purposes. This might actually disable - # being able to specify things like 'bestaudio' as the requested format, - # but try anyway. - # Use "$*" so that quoting the requested song isn't necessary. - mpv $(youtube-dl --default-search=ytsearch: \ - --max-downloads 1 \ - --youtube-skip-dash-manifest \ - --format="bestaudio/best" \ - -g "$*") --no-video -} - -function set_xdg_dirs { - xdg-user-dirs-update --set DESKTOP ~/var/desktop - xdg-user-dirs-update --set DOWNLOAD ~/var/downloads - xdg-user-dirs-update --set TEMPLATES ~/var/templates - xdg-user-dirs-update --set PUBLICSHARE ~/share/public - xdg-user-dirs-update --set DOCUMENTS ~/var/documents - xdg-user-dirs-update --set MUSIC ~/media/music - xdg-user-dirs-update --set PICTURES ~/media/pictures - xdg-user-dirs-update --set VIDEOS ~/media/videos - - rmdir ~/Desktop ~/Downloads ~/Templates ~/Public ~/Documents ~/Music \ - ~/Pictures ~/Videos -} - -# update -function update_dotfiles() { - dotfiles="$HOME/dotfiles" - epoch_file="$dotfiles/.updated" - epoch_curr=$(date +'%s') - [ -e "$epoch_file" ] && epoch_old=$(<"$epoch_file") - epoch_diff=$(($epoch_curr - ${epoch_old:-0})) - max_time=$((60 * 60 * 24 * 7)) - - if [ $(($epoch_diff / $max_time)) -ne 0 ]; then - echo "Updating dotfiles ..." - - cd "$dotfiles" - git pull --rebase --stat origin master - echo $epoch_curr > "$epoch_file" - ./install.sh - cd - - - echo "Finished." - fi -} - -function suspend_after() { - trap 'kill $!' INT TERM EXIT - systemd-inhibit --what=handle-lid-switch sleep 1d & - eval "$*" - systemctl suspend -} - -function reset_usb() { - for i in /sys/bus/pci/drivers/[uoex]hci_hcd/*:*; do - echo "${i##*/}" | sudo tee "${i%/*}/"{unbind,bind} - done -} - -function setup_borgbackup() { - setopt ERR_RETURN - - BORG_SECRET=~/.borg_secret - BORG_SSH_KEY=~/.ssh/borg_key - - if [ ! -f "$BORG_SECRET" ]; then - install -m600 /dev/null "$BORG_SECRET" - pass backups/$(hostname) > "$BORG_SECRET" - fi - - if [ ! -f "$BORG_SSH_KEY" ]; then - ssh-keygen -q -N "" -f "$BORG_SSH_KEY" - KEY=$(< ${BORG_SSH_KEY}.pub) - - printf '%s\n' "command=\"cd /srv/borgbackup/repos/$(hostname);borg serve --restrict-to-path /srv/borgbackup/repos/$(hostname)\",restrict $KEY" | ssh "$1" "cat >> /srv/borgbackup/.ssh/authorized_keys" - - echo "Added key to remote server." - fi -} - ## PATH -export PATH=$PATH:$HOME/bin +export PATH="$HOME/bin:$PATH" ## alias diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 0000000..5c976d1 --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,11 @@ +* + +!.gitignore +!play +!req_pwd.sh +!reset_usb +!set_xdg_dirs +!setup_borgbackup +!suspend_after +!update_dotfiles +!ut8font_test diff --git a/bin/play b/bin/play new file mode 100755 index 0000000..ed70ce9 --- /dev/null +++ b/bin/play @@ -0,0 +1,12 @@ +#!/bin/sh + +# requires youtube-dl, mpv +# Skip DASH manifest for speed purposes. This might actually disable +# being able to specify things like 'bestaudio' as the requested format, +# but try anyway. +# Use "$*" so that quoting the requested song isn't necessary. +mpv $(youtube-dl --default-search=ytsearch: \ + --max-downloads 1 \ + --youtube-skip-dash-manifest \ + --format="bestaudio/best" \ + -g "$*") --no-video diff --git a/bin/req_pwd.sh b/bin/req_pwd.sh new file mode 100755 index 0000000..3a6be86 --- /dev/null +++ b/bin/req_pwd.sh @@ -0,0 +1,19 @@ +#!/bin/sh -e +# A simple wrapper script to deal with situations where pass, password-store, or +# my gpg key is not available. + +pass "$1" && exit + +# maybe gpg-agent is acting up, retry after restarting it +gpg-connect-agent "KILLAGENT" /bye >&2 >/dev/null +gpg-connect-agent /bye >&2 >/dev/null +pass "$1" && exit + +# if pass fails, request password manually +stty -echo +printf "Password: " >&2 +read passwd +stty echo +printf "\n" >&2 + +printf "$passwd" diff --git a/bin/reset_usb b/bin/reset_usb new file mode 100755 index 0000000..2d20d8e --- /dev/null +++ b/bin/reset_usb @@ -0,0 +1,5 @@ +#!/bin/bash -e + +for i in /sys/bus/pci/drivers/[uoex]hci_hcd/*:*; do + echo "${i##*/}" | sudo tee "${i%/*}/"{unbind,bind} +done diff --git a/bin/set_xdg_dirs b/bin/set_xdg_dirs new file mode 100755 index 0000000..5a365e2 --- /dev/null +++ b/bin/set_xdg_dirs @@ -0,0 +1,13 @@ +#!/bin/sh -e + +xdg-user-dirs-update --set DESKTOP ~/var/desktop +xdg-user-dirs-update --set DOWNLOAD ~/var/downloads +xdg-user-dirs-update --set TEMPLATES ~/var/templates +xdg-user-dirs-update --set PUBLICSHARE ~/share/public +xdg-user-dirs-update --set DOCUMENTS ~/var/documents +xdg-user-dirs-update --set MUSIC ~/media/music +xdg-user-dirs-update --set PICTURES ~/media/pictures +xdg-user-dirs-update --set VIDEOS ~/media/videos + +rmdir ~/Desktop ~/Downloads ~/Templates ~/Public ~/Documents ~/Music \ + ~/Pictures ~/Videos diff --git a/bin/setup_borgbackup b/bin/setup_borgbackup new file mode 100755 index 0000000..6e857a1 --- /dev/null +++ b/bin/setup_borgbackup @@ -0,0 +1,18 @@ +#!/bin/sh -e + +BORG_SECRET=~/.borg_secret +BORG_SSH_KEY=~/.ssh/borg_key + +if [ ! -f "$BORG_SECRET" ]; then + install -m600 /dev/null "$BORG_SECRET" + pass backups/$(hostname) > "$BORG_SECRET" +fi + +if [ ! -f "$BORG_SSH_KEY" ]; then + ssh-keygen -q -N "" -f "$BORG_SSH_KEY" + KEY=$(< ${BORG_SSH_KEY}.pub) + + printf '%s\n' "command=\"cd /srv/borgbackup/repos/$(hostname);borg serve --restrict-to-path /srv/borgbackup/repos/$(hostname)\",restrict $KEY" | ssh "$1" "cat >> /srv/borgbackup/.ssh/authorized_keys" + + echo "Added key to remote server." +fi diff --git a/bin/suspend_after b/bin/suspend_after new file mode 100755 index 0000000..5f6cce4 --- /dev/null +++ b/bin/suspend_after @@ -0,0 +1,6 @@ +#!/bin/bash -e + +trap 'kill $!' INT TERM EXIT +systemd-inhibit --what=handle-lid-switch sleep 1d & +eval "$*" +systemctl suspend diff --git a/bin/update_dotfiles b/bin/update_dotfiles new file mode 100755 index 0000000..6c45971 --- /dev/null +++ b/bin/update_dotfiles @@ -0,0 +1,20 @@ +#!/bin/sh -e + +dotfiles="$HOME/dotfiles" +epoch_file="$dotfiles/.updated" +epoch_curr=$(date +'%s') +[ -e "$epoch_file" ] && epoch_old=$(<"$epoch_file") +epoch_diff=$(($epoch_curr - ${epoch_old:-0})) +max_time=$((60 * 60 * 24 * 7)) + +if [ $(($epoch_diff / $max_time)) -ne 0 ]; then + echo "Updating dotfiles ..." + + cd "$dotfiles" + git pull --rebase --stat origin master + echo $epoch_curr > "$epoch_file" + ./install.sh + cd - + + echo "Finished." +fi diff --git a/bin/ut8font_test b/bin/ut8font_test new file mode 100755 index 0000000..30c6b45 --- /dev/null +++ b/bin/ut8font_test @@ -0,0 +1,3 @@ +#!/bin/sh + +curl http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-demo.txt diff --git a/install.sh b/install.sh index 3dde15b..52b879d 100755 --- a/install.sh +++ b/install.sh @@ -40,5 +40,8 @@ done # fix .gnupg permissions chmod 700 .gnupg +mkdir -p ~/bin +find "$(pwd)/bin" -maxdepth 1 ! -path . -exec ln -sf {} ~/bin/ \; + # remove broken symlinks find -L "$HOME" -maxdepth 1 -type l -delete -- cgit v1.2.3-70-g09d2