Created
January 27, 2021 19:57
-
-
Save SEbbaDK/7d3245b96b0d3ec13d59c3b9b9e3b868 to your computer and use it in GitHub Desktop.
Short script to lock a binary behind a rofi/dmenu division quiz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
error() { | |
echo "$1" | |
echo | |
echo "USAGE:" | |
echo "$0 --lock name" | |
echo " locks the binary called name" | |
echo "$0 --unlock name" | |
echo " unlocks the binary called name" | |
echo "LICENSE: GPLv3" | |
echo "AUTHOR: SEbbaDK" | |
exit 1 | |
} | |
if command -v rofi &> /dev/null ; then | |
DMENU="rofi -dmenu" | |
elif command -v dmenu &> /dev/null ; then | |
DMENU="dmenu" | |
else | |
error "$0 requires either rofi or dmenu to be installed" | |
fi | |
lock() { | |
path=$(which $1) | |
newpath="$path-locked" | |
mv "$path" "$newpath" | |
cat > "$path" <<- EOF | |
#!/usr/bin/env bash | |
program="$newpath" | |
correct=\$(shuf -n 1 -i 3-19) | |
base=\$(shuf -n 1 -i 101-149) | |
possibilities=\$(for i in {2..19}; do echo "\$i"; done) | |
answer=\$(echo -e "\$possibilities" | $DMENU -p "\$base = \$(( \$correct * \$base )) / ??") | |
[ \$correct == \$answer ] && \$program "\$@" | |
EOF | |
chmod +x "$path" | |
} | |
case "$1" in | |
"--unlock"|"-u") | |
[ -z "$2" ] && error "Give binary name to unlock" | |
command -v "$2" &> /dev/null || error "Binary was not in path" | |
path=$(which $2) | |
[ ! -f "$path-locked" ] && error "This binary has not been locked" | |
rm "$path" | |
mv "$path-locked" "$path" | |
;; | |
"--lock"|"-l") | |
[ -z "$2" ] && error "Give binary name to lock" | |
command -v "$2" &> /dev/null || error "Binary was not in path" | |
[ -f "$(which $2)-locked" ] && error "Binary already locked" | |
lock $2 | |
;; | |
*) | |
error "Enter either --lock or --unlock to signify use" | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment