From 3f8dc335a01f4a0c20e915592ea525f5f1fb8af3 Mon Sep 17 00:00:00 2001 From: eeva Date: Fri, 18 Mar 2016 17:59:50 +0100 Subject: [PATCH] Useful scripts --- scripts/prefix/bin/coc | 24 +++++++++++ scripts/prefix/bin/mvln | 74 +++++++++++++++++++++++++++++++++ scripts/prefix/bin/vpn | 33 +++++++++++++++ scripts/prefix/bin/zathura-sync | 9 ++++ 4 files changed, 140 insertions(+) create mode 100755 scripts/prefix/bin/coc create mode 100755 scripts/prefix/bin/mvln create mode 100755 scripts/prefix/bin/vpn create mode 100755 scripts/prefix/bin/zathura-sync diff --git a/scripts/prefix/bin/coc b/scripts/prefix/bin/coc new file mode 100755 index 0000000..c6f89b0 --- /dev/null +++ b/scripts/prefix/bin/coc @@ -0,0 +1,24 @@ +#!/bin/sh +# +# "coc" stands for "compile on change" and runs the supplied compile +# command when the given list of files is changed. +# +# coc [command] [fileToCompile] [filesToWatch] +# +# If no explicit list of files to watch is given, fileToCompile is +# watched. + +command=$1 +file=$2 +shift 2 +watchedFiles=$@ + +if [ -z "$watchedFiles" ] ; then + watchedFiles=$file +fi + +while true ; do + date + inotifywait -e modify $watchedFiles + $command $file +done diff --git a/scripts/prefix/bin/mvln b/scripts/prefix/bin/mvln new file mode 100755 index 0000000..80b1a4d --- /dev/null +++ b/scripts/prefix/bin/mvln @@ -0,0 +1,74 @@ +#!/bin/bash +# Moves and links a file back at its initial location +# It can not cross filesystems! +# +# Usage: mvln PATH1 PATH2 +# Depends on: realpath, basename, dirname + +if [[ $# -ne 2 ]] +then + echo "Usage: mvln " + exit 0 +fi + +# Step 1: building full path out of both arguments ############################# +[[ $1 =~ ^/ ]] && F1=$1 || F1=$(realpath -s $1) +[[ $2 =~ ^/ ]] && F2=$2 || F2=$(realpath -s $2) + +# Step 2: enumerating all interesting cases #################################### +# PATH1 can be: +if [[ -f $F1 ]] +then STATE1="0" # a regular file +elif [[ -d $PWD/$F1 ]] +then STATE1="1" # a directory +elif [[ -e $F1 ]] +then STATE1="8" # an other kind of file +else STATE1="9" # a non-existent file +fi + +# PATH2 can be: +if [[ -d $F2 ]] +then STATE2="0" # a directory +elif [[ -d $(dirname $F2) ]] +then STATE2="1" # a non-existent file in an existent directory +elif [[ -e $F2 ]] +then STATE2="8" # any other kind of file +else STATE2="9" # a non-existent file in a non-existent directory +fi + +# Step 3: eat this my little state automaton ################################### +STATE="$STATE1$STATE2" +case $STATE in + [89]* ) + echo "$F1 is not a proper file (or a non-existent one)" + echo "Note that only regular files and directories are allowed" + exit $STATE + ;; + *[89] ) + echo "$F2 can not be created (containing directory does not exist)" + echo "or exists already (refusing to overwrite)" + exit $STATE + ;; + [01]0 ) # file or dir to an existing dir + DEGUB="File/Dir to same name in existing dir" + PATH1=$F1 + PATH2=$F2/$(basename $F1) + ;; + [01]1 ) # file to new file name in an existing dir + DEGUB="File/Dir to new name in existing dir" + PATH1=$F1 + PATH2=$F2 + ;; + * ) + echo "Uncaught error (this is f*****g bad)" + exit $STATE + ;; +esac + +#echo $DEGUB + +# Step 4: eventually act upon gathered knowledge ############################### +mv $PATH1 $PATH2 +cd $(dirname $PATH1) +ln -s $PATH2 $(basename $PATH1) + diff --git a/scripts/prefix/bin/vpn b/scripts/prefix/bin/vpn new file mode 100755 index 0000000..8447b27 --- /dev/null +++ b/scripts/prefix/bin/vpn @@ -0,0 +1,33 @@ +#!/bin/sh + +if [[ $# -ne 1 ]] +then + echo "$(basename $0) requires exactly one of: hard, medium, off" + exit 1 +else + case $1 in + "hard") + PROFILE="2947fbbad4" + ;; + "medium") + PROFILE="OodCanine" + ;; + "off") + ;; + *) + echo "Wrong command, either hard, medium or off" + exit 2 + ;; + esac + + sudo systemctl stop openvpn@2947fbbad4.service + sudo systemctl stop openvpn@OodCanine.service + sleep 1s + + if [[ -n $PROFILE ]] + then + sudo systemctl start openvpn@$PROFILE.service + fi + echo "$PROFILE started" +fi + diff --git a/scripts/prefix/bin/zathura-sync b/scripts/prefix/bin/zathura-sync new file mode 100755 index 0000000..8767565 --- /dev/null +++ b/scripts/prefix/bin/zathura-sync @@ -0,0 +1,9 @@ +#!/bin/sh +pos="$1" +pdffile="$2" +zathura --synctex-forward "$pos" "$pdffile" || \ + ( + zathura -x "emacsclient --eval '(progn (find-file \"%{input}\") (goto-line %{line}))'" "$pdffile" & + sleep 1; zathura --synctex-forward "$pos" "$pdffile" ) + +exit