Initial commit of utility scripts

This commit is contained in:
Alex Selimov 2024-12-12 20:16:38 -05:00
parent ac77586a15
commit 1faeefffd7
34 changed files with 1328 additions and 1 deletions

46
bin/albumsplit Executable file
View file

@ -0,0 +1,46 @@
#!/bin/sh
# Requires ffmpeg (audio splitting) and my `tag` wrapper script.
[ ! -f "$2" ] && printf "The first file should be the audio, the second should be the timecodes.\\n" && exit
echo "Enter the album/book title:"; read -r booktitle
echo "Enter the artist/author:"; read -r author
echo "Enter the publication year:"; read -r year
inputaudio="$1"
# Get a safe file name from the book.
escbook="$(echo "$booktitle" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g")"
! mkdir -p "$escbook" && echo "Do you have write access in this directory?" && exit 1
# As long as the extension is in the tag script, it'll work.
ext="${1##*.}"
#ext="${1#*.}"
# Get the total number of tracks from the number of lines.
total="$(wc -l < "$2")"
while read -r x;
do
end="$(echo "$x" | cut -d' ' -f1)"
[ -n "$start" ] &&
echo "From $start to $end; $track $title"
file="$escbook/$(printf "%.2d" "$track")-$esctitle.$ext"
[ -n "$start" ] && echo "Splitting \"$title\"..." &&
ffmpeg -nostdin -y -loglevel -8 -i "$inputaudio" -ss "$start" -to "$end" -vn -c copy "$file" &&
echo "Tagging \"$title\"..." && tag -a "$author" -A "$booktitle" -t "$title" -n "$track" -N "$total" -d "$year" "$file"
title="$(echo "$x" | cut -d' ' -f 2-)"
esctitle="$(echo "$title" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g")"
track="$((track+1))"
start="$end"
done < "$2"
# The last track must be done outside the loop.
echo "From $start to the end: $title"
file="$escbook/$(printf "%.2d" "$track")-$esctitle.$ext"
echo "Splitting \"$title\"..." && ffmpeg -nostdin -y -loglevel -8 -i "$inputaudio" -ss "$start" -vn -c copy "$file" &&
echo "Tagging \"$title\"..." && tag -a "$author" -A "$booktitle" -t "$title" -n "$track" -N "$total" -d "$year" "$file"

62
bin/bar_bat.sh Executable file
View file

@ -0,0 +1,62 @@
#!/bin/bash
#First we get the capacity
charge=$(cat /sys/class/power_supply/BAT0/capacity)
#Now get the status
bstat=$(cat /sys/class/power_supply/BAT0/status)
#Get the symbol for the capacity
if [ "$bstat" = "Charging" ]; then
cstat=""
else
cstat=""
fi
if [ "$charge" -gt 90 ]; then
bat="$cstat"
elif [ "$charge" -gt 70 ]; then
bat="$cstat"
elif [ "$charge" -gt 50 ]; then
bat="$cstat"
elif [ "$charge" -gt 20 ]; then
bat="$cstat"
else
bat=" $cstat"
fi
battery="$bat $charge%"
if [ -d /sys/class/power_supply/BAT1 ]; then
#First we get the capacity
charge=$(cat /sys/class/power_supply/BAT1/capacity)
#Now get the status
bstat=$(cat /sys/class/power_supply/BAT1/status)
if [ "$bstat" = "Charging" ]; then
cstat=""
else
cstat=""
fi
#Get the symbol for the capacity
if [ "$charge" -gt 90 ]; then
bat="$cstat"
elif [ "$charge" -gt 70 ]; then
bat="$cstat"
elif [ "$charge" -gt 50 ]; then
bat="$cstat"
elif [ "$charge" -gt 20 ]; then
bat="$cstat"
else
bat=" $cstat"
fi
echo "$bat $charge% $battery"
else
echo $battery
fi

10
bin/bar_bit.sh Executable file
View file

@ -0,0 +1,10 @@
#!/bin/bash
#price=$(curl rate.sx/1btc | cut -d '.' -f 1)
price=$(coinmon -f BTC | tail -n2 | head -n1 | cut -d ' ' -f 10 | cut -d '.' -f1)
echo "฿ $price"
# Check to see if it's a number
#re='^[0-9]+$'
#if [[ $price =~ $re ]] ; then
#fi

19
bin/bar_cpu.sh Executable file
View file

@ -0,0 +1,19 @@
#!/bin/bash
loads=($(mpstat -P ALL 1 1 | awk '/Average:/ && $2 ~ /[0-9]/ {print $3}'))
warn=''
if [ ${loads[0]} -gt 80 ]; then
warn=$(echo "1 "$warn)
fi
if [ ${loads[1]} -gt 80 ]; then
warn=$(echo "2 "$warn)
fi
if [ ${loads[2]} -gt 80 ]; then
warn=$(echo "3 "$warn)
fi
if [ ${loads[3]} -gt 80 ]; then
warn=$(echo "4 "$warn)
fi
avg=$(echo "(${loads[0]}+${loads[1]}+${loads[2]}+${loads[3]})/4" | bc)
echo $warn $avg%

1
bin/bar_date.sh Executable file
View file

@ -0,0 +1 @@
echo "$(date '+%b %d (%a) %I:%M%p') "

15
bin/bar_mem.sh Executable file
View file

@ -0,0 +1,15 @@
#!/bin/bash
free=$(grep -oP '^MemFree: *\K[0-9]+' /proc/meminfo)
available=$(grep -oP '^MemAvailable: *\K[0-9]+' /proc/meminfo)
total=$(grep -oP '^MemTotal: *\K[0-9]+' /proc/meminfo)
mem=" $(echo "scale=1; 100*($total-$available)/$total"| bc | cut -d '.' -f1 )"
if [ $mem -gt 80 ]; then
mem="$mem"
elif [ $mem -gt 50 ]; then
mem="$mem"
else
mem="$mem"
fi
echo "$mem%"

12
bin/bar_music.sh Executable file
View file

@ -0,0 +1,12 @@
#!/bin/bash
#First we get the next appointmen
var=$(mpc current);
if [ "$var" = "" ]; then
echo ""
else
echo "${var:0:30}"
fi

14
bin/bar_volume.sh Executable file
View file

@ -0,0 +1,14 @@
#!/usr/bin/bash
[ $(pamixer --get-mute) = true ] && echo&& exit
vol="$(pamixer --get-volume)"
if [ "$vol" -gt "70" ]; then
icon="󰕾"
elif [ "$vol" -gt "30" ]; then
icon="󰖀"
else
icon="󰕿"
fi
echo "$icon $vol%"

5
bin/bar_vpn.sh Executable file
View file

@ -0,0 +1,5 @@
#!/bin/bash
# Check to see if openvpn is running. If it is then output a message
pgrep openvpn 1>/dev/null &&
echo " VPN ACTIVATED"

8
bin/bar_wifi.sh Executable file
View file

@ -0,0 +1,8 @@
#!/bin/bash
case "$(cat /sys/class/net/w*/operstate 2>/dev/null)" in
down) echo " 󰖪 ";;
up) percentage="$(awk '/^\s*w/ { print int($3 * 100 / 70) "% " }' /proc/net/wireless)"
ssid=$(iwgetid -r)
echo \ $ssid 
esac

20
bin/daily_scripture.sh Executable file
View file

@ -0,0 +1,20 @@
#!/bin/bash
# Define the input file
input_file="$HOME/docs/kjv.txt"
# Generate a unique identifier based on the current date
date=$( date +%Y%m%d )
# Generate random data with a seed
get_seeded_random()
{
seed="$1"
openssl enc -aes-256-ctr -pass pass:"$seed" -nosalt \
</dev/zero 2>/dev/null
}
random_line=$(shuf -n 1 --random-source=<(get_seeded_random "$date") $input_file)
# Print the randomly selected line
echo "$random_line"

BIN
bin/kjv Executable file

Binary file not shown.

10
bin/mpdnotif.sh Executable file
View file

@ -0,0 +1,10 @@
#!/bin/bash
# notify when mpd music changed
while "true"; do
status="`mpc status`"
if [[ ${status} == *"[playing]"* ]]; then
dunstify -a Music --replace=27072 -t 2000 -i "$HOME/media/pics/music.png" "Now Playing:" "Artist: $(mpc --format '%artist%' current)\nSong: $(mpc --format '%title%' current)"
fi
mpc current --wait > /dev/null
done

326
bin/notify-send.sh Executable file
View file

@ -0,0 +1,326 @@
#!/usr/bin/env bash
# notify-send.sh - drop-in replacement for notify-send with more features
# Copyright (C) 2015-2021 notify-send.sh authors (see AUTHORS file)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Desktop Notifications Specification
# https://developer.gnome.org/notification-spec/
VERSION=1.2
NOTIFY_ARGS=(--session
--dest org.freedesktop.Notifications
--object-path /org/freedesktop/Notifications)
EXPIRE_TIME=-1
APP_NAME="${0##*/}"
REPLACE_ID=0
URGENCY=1
HINTS=()
SUMMARY_SET=n
help() {
cat <<EOF
Usage:
notify-send.sh [OPTION...] <SUMMARY> [BODY] - create a notification
Help Options:
-?|--help Show help options
Application Options:
-u, --urgency=LEVEL Specifies the urgency level (low, normal, critical).
-t, --expire-time=TIME Specifies the timeout in milliseconds at which to expire the notification.
-f, --force-expire Forcefully closes the notification when the notification has expired.
-a, --app-name=APP_NAME Specifies the app name for the icon.
-i, --icon=ICON[,ICON...] Specifies an icon filename or stock icon to display.
-c, --category=TYPE[,TYPE...] Specifies the notification category.
-h, --hint=TYPE:NAME:VALUE Specifies basic extra data to pass. Valid types are int, double, string and byte.
-o, --action=LABEL:COMMAND Specifies an action. Can be passed multiple times. LABEL is usually a button's label. COMMAND is a shell command executed when action is invoked.
-d, --default-action=COMMAND Specifies the default action which is usually invoked by clicking the notification.
-l, --close-action=COMMAND Specifies the action invoked when notification is closed.
-p, --print-id Print the notification ID to the standard output.
-r, --replace=ID Replace existing notification.
-R, --replace-file=FILE Store and load notification replace ID to/from this file.
-s, --close=ID Close notification.
-v, --version Version of the package.
EOF
}
convert_type() {
case "$1" in
int) echo int32 ;;
double|string|byte) echo "$1" ;;
*) echo error; return 1 ;;
esac
}
make_action_key() {
echo "$(tr -dc _A-Z-a-z-0-9 <<< \"$1\")${RANDOM}"
}
make_action() {
local action_key="$1"
printf -v text "%q" "$2"
echo "\"$action_key\", \"$text\""
}
make_hint() {
type=$(convert_type "$1")
[[ ! $? = 0 ]] && return 1
name="$2"
[[ "$type" = string ]] && command="\"$3\"" || command="$3"
echo "\"$name\": <$type $command>"
}
concat_actions() {
local result="$1"
shift
for s in "$@"; do
result="$result, $s"
done
echo "[$result]"
}
concat_hints() {
local result="$1"
shift
for s in "$@"; do
result="$result, $s"
done
echo "{$result}"
}
parse_notification_id(){
sed 's/(uint32 \([0-9]\+\),)/\1/g'
}
notify() {
local actions="$(concat_actions "${ACTIONS[@]}")"
local hints="$(concat_hints "${HINTS[@]}")"
NOTIFICATION_ID=$(gdbus call "${NOTIFY_ARGS[@]}" \
--method org.freedesktop.Notifications.Notify \
-- \
"$APP_NAME" "$REPLACE_ID" "$ICON" "$SUMMARY" "$BODY" \
"${actions}" "${hints}" "int32 $EXPIRE_TIME" \
| parse_notification_id)
if [[ -n "$STORE_ID" ]] ; then
echo "$NOTIFICATION_ID" > "$STORE_ID"
fi
if [[ -n "$PRINT_ID" ]] ; then
echo "$NOTIFICATION_ID"
fi
if [[ -n "$FORCE_EXPIRE" ]] ; then
SLEEP_TIME="$( LC_NUMERIC=C printf %f "${EXPIRE_TIME}e-3" )"
( sleep "$SLEEP_TIME" ; notify_close "$NOTIFICATION_ID" ) &
fi
maybe_run_action_handler
}
notify_close () {
gdbus call "${NOTIFY_ARGS[@]}" --method org.freedesktop.Notifications.CloseNotification "$1" >/dev/null
}
process_urgency() {
case "$1" in
low) URGENCY=0 ;;
normal) URGENCY=1 ;;
critical) URGENCY=2 ;;
*) echo "Unknown urgency $URGENCY specified. Known urgency levels: low, normal, critical."
exit 1
;;
esac
}
process_category() {
IFS=, read -a categories <<< "$1"
for category in "${categories[@]}"; do
hint="$(make_hint string category "$category")"
HINTS=("${HINTS[@]}" "$hint")
done
}
process_hint() {
IFS=: read type name command <<< "$1"
if [[ -z "$name" ]] || [[ -z "$command" ]] ; then
echo "Invalid hint syntax specified. Use TYPE:NAME:VALUE."
exit 1
fi
hint="$(make_hint "$type" "$name" "$command")"
if [[ ! $? = 0 ]] ; then
echo "Invalid hint type \"$type\". Valid types are int, double, string and byte."
exit 1
fi
HINTS=("${HINTS[@]}" "$hint")
}
maybe_run_action_handler() {
if [[ -n "$NOTIFICATION_ID" ]] && [[ -n "$ACTION_COMMANDS" ]]; then
local notify_action="$(dirname ${BASH_SOURCE[0]})/notify-action.sh"
if [[ -x "$notify_action" ]] ; then
"$notify_action" "$NOTIFICATION_ID" "${ACTION_COMMANDS[@]}" &
exit 0
else
echo "executable file not found: $notify_action"
exit 1
fi
fi
}
process_action() {
IFS=: read name command <<<"$1"
if [[ -z "$name" ]] || [[ -z "$command" ]]; then
echo "Invalid action syntax specified. Use NAME:COMMAND."
exit 1
fi
local action_key="$(make_action_key "$name")"
ACTION_COMMANDS=("${ACTION_COMMANDS[@]}" "$action_key" "$command")
local action="$(make_action "$action_key" "$name")"
ACTIONS=("${ACTIONS[@]}" "$action")
}
process_special_action() {
action_key="$1"
command="$2"
if [[ -z "$action_key" ]] || [[ -z "$command" ]]; then
echo "Command must not be empty"
exit 1
fi
ACTION_COMMANDS=("${ACTION_COMMANDS[@]}" "$action_key" "$command")
if [[ "$action_key" != close ]]; then
local action="$(make_action "$action_key" "$name")"
ACTIONS=("${ACTIONS[@]}" "$action")
fi
}
process_posargs() {
if [[ "$1" = -* ]] && ! [[ "$positional" = yes ]] ; then
echo "Unknown option $1"
exit 1
else
if [[ "$SUMMARY_SET" = n ]]; then
SUMMARY="$1"
SUMMARY_SET=y
else
BODY="$1"
fi
fi
}
while (( $# > 0 )) ; do
case "$1" in
-\?|--help)
help
exit 0
;;
-v|--version)
echo "${0##*/} $VERSION"
exit 0
;;
-u|--urgency|--urgency=*)
[[ "$1" = --urgency=* ]] && urgency="${1#*=}" || { shift; urgency="$1"; }
process_urgency "$urgency"
;;
-t|--expire-time|--expire-time=*)
[[ "$1" = --expire-time=* ]] && EXPIRE_TIME="${1#*=}" || { shift; EXPIRE_TIME="$1"; }
if ! [[ "$EXPIRE_TIME" =~ ^-?[0-9]+$ ]]; then
echo "Invalid expire time: ${EXPIRE_TIME}"
exit 1;
fi
;;
-f|--force-expire)
FORCE_EXPIRE=yes
;;
-a|--app-name|--app-name=*)
[[ "$1" = --app-name=* ]] && APP_NAME="${1#*=}" || { shift; APP_NAME="$1"; }
;;
-i|--icon|--icon=*)
[[ "$1" = --icon=* ]] && ICON="${1#*=}" || { shift; ICON="$1"; }
;;
-c|--category|--category=*)
[[ "$1" = --category=* ]] && category="${1#*=}" || { shift; category="$1"; }
process_category "$category"
;;
-h|--hint|--hint=*)
[[ "$1" = --hint=* ]] && hint="${1#*=}" || { shift; hint="$1"; }
process_hint "$hint"
;;
-o | --action | --action=*)
[[ "$1" == --action=* ]] && action="${1#*=}" || { shift; action="$1"; }
process_action "$action"
;;
-d | --default-action | --default-action=*)
[[ "$1" == --default-action=* ]] && default_action="${1#*=}" || { shift; default_action="$1"; }
process_special_action default "$default_action"
;;
-l | --close-action | --close-action=*)
[[ "$1" == --close-action=* ]] && close_action="${1#*=}" || { shift; close_action="$1"; }
process_special_action close "$close_action"
;;
-p|--print-id)
PRINT_ID=yes
;;
-r|--replace|--replace=*)
[[ "$1" = --replace=* ]] && REPLACE_ID="${1#*=}" || { shift; REPLACE_ID="$1"; }
;;
-R|--replace-file|--replace-file=*)
[[ "$1" = --replace-file=* ]] && filename="${1#*=}" || { shift; filename="$1"; }
if [[ -s "$filename" ]]; then
REPLACE_ID="$(< "$filename")"
fi
STORE_ID="$filename"
;;
-s|--close|--close=*)
[[ "$1" = --close=* ]] && close_id="${1#*=}" || { shift; close_id="$1"; }
# always check that --close provides a numeric value
if [[ -z "$close_id" || ! "$close_id" =~ ^[0-9]+$ ]]; then
echo "Invalid close id: '$close_id'"
exit 1
fi
notify_close "$close_id"
exit $?
;;
--)
positional=yes
;;
*)
process_posargs "$1"
;;
esac
shift
done
# always force --replace and --replace-file to provide a numeric value; 0 means no id provided
if [[ -z "$REPLACE_ID" || ! "$REPLACE_ID" =~ ^[0-9]+$ ]]; then
REPLACE_ID=0
fi
# urgency is always set
HINTS=("$(make_hint byte urgency "$URGENCY")" "${HINTS[@]}")
if [[ "$SUMMARY_SET" = n ]] ; then
help
exit 1
else
notify
fi

1
bin/pubs-rofi.sh Symbolic link
View file

@ -0,0 +1 @@
/home/aselimov/projects/utility/scripts/pubs-rofi.sh

46
bin/viewurl Executable file
View file

@ -0,0 +1,46 @@
#!/bin/bash
# Feed script a url.
# If an image, it will view in feh,
# if a video or gif, it will view in mpv
# if a music file or pdf, it will download,
# otherwise it opens link in browser.
# List of sites that will be opened in mpv.
vidsites="youtube.com
\|youtu.be
\|hooktube.com
\|bitchute.com
\|lukesmith.xyz
\|odysee.com
\|rumble.com
"
if [ -p /dev/stdin ]; then
read url
else
url=$1
fi
echo $url
ext="${url##*.}"
mpvFiles="ogg mkv mp4 gif mp3 mp3?source=feed"
sxivFiles="png jpg jpeg jpe"
mpvAdd="mp3?updated=*"
wgetFiles="mp3 flac opus mp3?source=feed pdf"
if echo $sxivFiles | grep -w $ext > /dev/null; then
view_url_image "$url" >/dev/null &
elif echo $mpvFiles | grep -w $ext > /dev/null; then
mpv --audio-display=no --loop "$url"
elif echo $wgetFiles | grep -w $ext > /dev/null; then
wget "$url" >/dev/null &
elif echo "$url" | grep "$vidsites">/dev/null; then
mpv "$url" > /dev/null &
elif echo "$url" | grep "$mpvAdd" > /dev/null; then
mpv --audio-display=no --loop "$url"
elif echo "$url" | grep "doi.org" > /dev/null; then
view_doc $url
elif echo "$url" | grep "mp3" > /dev/null; then
mpv --audio-display=no --loop "$url"
else
$BROWSER "$url" 2>/dev/null & disown
fi

70
bin/volume.sh Executable file
View file

@ -0,0 +1,70 @@
#!/bin/bash
# You can call this script like this:
# $./volume.sh up
# $./volume.sh down
# $./volume.sh mute
function get_volume {
pamixer --get-volume
}
function is_mute {
[ $(pamixer --get-mute) = 'true']
}
function send_notification {
DIR=$(dirname "$0")
volume=$(get_volume)
# Make the bar with the special character ─ (it's not dash -)
# https://en.wikipedia.org/wiki/Box-drawing_character
#bar=$(seq -s "─" $(($volume/5)) | sed 's/[0-9]//g')
if [ "$volume" = "0" ]; then
icon_name="/usr/share/icons/Papirus/48x48/status/notification-audio-volume-muted.svg"
$DIR/notify-send.sh "$volume"" " -i "$icon_name" -t 2000 -h string:synchronous:"─" --replace=555
else
if [ "$volume" -lt "10" ]; then
icon_name="/usr/share/icons/Papirus/48x48/status/notification-audio-volume-low.svg"
$DIR/notify-send.sh "$volume"" " -i "$icon_name" --replace=555 -t 2000
else
if [ "$volume" -lt "30" ]; then
icon_name="/usr/share/icons/Papirus/48x48/status/notification-audio-volume-low.svg"
else
if [ "$volume" -lt "70" ]; then
icon_name="/usr/share/icons/Papirus/48x48/status/notification-audio-volume-medium.svg"
else
icon_name="/usr/share/icons/Papirus/48x48/status/notification-audio-volume-high.svg"
fi
fi
fi
fi
bar=$(seq -s "─" $(($volume / 5)) | sed 's/[0-9]//g')
# Send the notification
$DIR/notify-send.sh "$volume%"" ""$bar" -i "$icon_name" -t 2000 -h string:synchronous:"$bar" --replace=555
}
case $1 in
up)
# Set the volume on (if it was muted)
#amixer -D pulse set Master on > /dev/null
# Up the volume (+ 5%)
pactl set-sink-volume @DEFAULT_SINK@ +5%
send_notification
;;
down)
#amixer -D pulse set Master on > /dev/null
pactl set-sink-volume @DEFAULT_SINK@ -5%
send_notification
;;
mute)
# Toggle mute
#amixer -D pulse set Master 1+ toggle > /dev/null
if is_mute; then
DIR=$(dirname "$0")
$DIR/notify-send.sh -i "/usr/share/icons/Papirus/48x48/status/notification-audio-volume-muted.svg" --replace=555 -u normal "Mute" -t 2000
else
send_notification
fi
;;
esac

28
bin/wallpaper_randomizer.sh Executable file
View file

@ -0,0 +1,28 @@
#!/bin/bash
papesdir=$HOME/media/wallpapers/current_rotation
# Kill existing execution if it exists
pgrep -fl wallpaper_randomizer.sh | while read -r line; do
pid=$(echo $line | cut -d " " -f1)
process=$(echo $line | cut -d " " -f2)
if [[ "$pid" != "$$" ]] && [[ "$process" == "wallpaper_rando" ]]; then
kill $pid
fi
done
last_index=""
index=""
while :; do
files=($papesdir/*)
count=$(find -L $papesdir -type f | wc -l)
# Get a new wallpaper
while [ "$index" == "$last_index" ]; do
index=$((($RANDOM % $count)))
done
feh --bg-fill ${files[$index]}
last_index=$index
sleep 15m
done