blob: 2824d07ebb87d328abcef88ef20e27588502d56f (
plain) (
tree)
|
|
#!/bin/bash
# functions
function find_directory {
find "$directory_home" -maxdepth 3 -mount -type d -name "$1" 2>/dev/null
}
function find_remote {
rclone listremotes | awk -v remote="$1" '$0 ~ remote {print $0;exit}'
}
function check_root {
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 0
fi
}
function check_not_root {
if [ "$EUID" -eq 0 ]; then
echo "Don't run this function as root"
exit 0
fi
}
function check_depends {
dependencies=(aria2c awk bash bc docker docker-compose ffmpeg git gnuplot journalctl logname media-sort mp3val opustags phockup pip3 python3 qpdf rbw rclone scour sed seq sort uniq vnstat we-get yt-dlp)
echo "Checking dependencies..."
for i in "${dependencies[@]}"; do
echo -n "$i: "
if [[ $(command -v "$i") ]]; then
echo -e "\e[32mpresent\e[39m"
else
echo -e "\e[31mmissing\e[39m"
fi
done
exit 1
}
function umount_remote {
if [ -z "$2" ]; then
working_directory="$(find_directory "$1")"
else
working_directory="$(find_directory "$2")"
fi
umount "$working_directory"
fusermount -uz "$working_directory" 2>/dev/null
find "$working_directory" -maxdepth 1 -mount -type d -not -path "*/\.*" -empty -delete
}
function password_manager {
check_not_root
case "$1" in
addr) rbw get --full "$2" | awk '/URI:/ {print $2}' ;;
full) rbw get --full "$2" ;;
pass) rbw get "$2" ;;
sync) rbw sync ;;
user) rbw get --full "$2" | awk '/Username:/ {print $2}' ;;
*) rbw get "$2" ;;
esac
}
function duolingo_streak {
check_not_root
# check api is installed
[[ -d "$(find_directory $directory_config)/duolingo" ]] || git clone https://github.com/KartikTalwar/Duolingo.git "$(find_directory $directory_config)/duolingo"
# cd to git dir to include module
cd "$(find_directory $directory_config)/duolingo" || return
# write script
password_manager sync
{
printf "#!/usr/bin/env python3\\n\\n"
printf "import duolingo\\n"
printf "lingo = duolingo.Duolingo('%s', password='%s')\\n" "$(password_manager user duolingo)" "$(password_manager pass duolingo)"
printf "lingo.buy_streak_freeze()"
} >"streak-freeze.py"
# run and remove script
python "streak-freeze.py"
rm "streak-freeze.py"
}
function blog_duolingo_rank {
duo_username="$(awk -F'[/()]' '/Duolingo/ {print $5}' "$(find_directory blog."$domain")"/content/about.md)"
rank_filename="$(find_directory blog."$domain")/content/posts/logging-duolingo-ranks-over-time.md"
echo -n "Fetching data for $duo_username... "
page_source="$(curl -s https://duome.eu/"$duo_username")"
rank_lingot="$(printf %s "$page_source" | awk -F"[#><]" '/icon lingot/ {print $15}')"
rank_streak="$(printf %s "$page_source" | awk -F"[#><]" '/icon streak/{getline;print $15}')'"
echo -e "$i \e[32mdone\e[39m"
echo -n "Appending ranks to page... "
echo "| $(date +%F) | $(date +%H:%M) | $rank_streak | $rank_lingot |" | tr -d \' >>"$rank_filename"
echo -e "$i \e[32mdone\e[39m"
lastmod "$rank_filename"
}
function docker_build {
cd "$directory_script" || exit
# write env file, overwriting any existing
password_manager sync
{
printf "DOMAIN=%s\\n" "$domain"
printf "PUID=%s\\n" "$(id -u)"
printf "PGID=%s\\n" "$(id -g)"
printf "TZ=%s\\n" "$(timedatectl status | awk '/Time zone/ {print $3}')"
printf "DOCKDIR=%s\\n" "$(find_directory docker)"
printf "SYNCDIR=%s\\n" "$(find_directory vault)"
printf "TANKDIR=%s\\n" "$directory_tank"
printf "DBPASSWORD=%s\\n" "$(password_manager pass postgresql)"
printf "VPNUSER=%s\\n" "$(password_manager user transmission-openvpn)"
printf "VPNPASS=%s\\n" "$(password_manager pass transmission-openvpn)"
printf "HTPASSWD=%s\\n" "$(docker exec -it caddy caddy hash-password --plaintext "$(password_manager pass htpasswd)" | base64 -w0)"
printf "TODOSECRET=%s\\n" "$(password_manager full vikunja | awk '/secret:/ {print $2}')"
} >"$directory_script/.env"
# make network, if not existing
if ! printf "%s" "$(docker network ls)" | grep -q "proxy"; then
echo Creating docker network
docker network create proxy
fi
# start containers
echo Starting docker containers
docker-compose up -d --remove-orphans
# delete temporary env file
if [[ -f "$directory_script/.env" ]]; then
echo Deleting detected env file
rm "$directory_script/.env"
fi
# clean up existing stuff
echo Cleaning up existing docker files
for i in volume image system network; do
docker "$i" prune -f
done
docker system prune -af
}
function media_logger {
# specify directories
git_directory="$(find_directory logger)"
file_git_log="$git_directory/media.log"
log_remote="$(find_remote nas)"
git_logger="git --git-dir=$git_directory/.git --work-tree=$git_directory"
# git configuruation
if [ ! -e "$git_directory" ]; then
printf "Logger directory not found, quitting...\n"
exit 1
fi
if [ ! -e "$git_directory/.git" ]; then
printf "Initialising blank git repo...\n"
$git_logger init
fi
if [ -e "$file_git_log.xz" ]; then
printf "Decompressing existing xz archive...\n"
xz -d "$file_git_log.xz"
fi
if [ -e "$file_git_log" ]; then
printf "Removing existing log file...\n"
rm "$file_git_log"
fi
printf "Creating log...\n"
rclone ls "$log_remote" | sort -k2 >"$file_git_log"
printf "Appending size information...\n"
rclone size "$log_remote" >>"$file_git_log"
printf "Commiting log file to repository...\n"
$git_logger add "$file_git_log"
$git_logger commit -m "Update: $(date +%F)"
if [ -e "$file_git_log.xz" ]; then
printf "Removing xz archive...\n"
rm "$file_git_log.xz"
fi
printf "Compressing log file...\n"
xz "$file_git_log"
printf "Compressing repository...\n"
$git_logger config pack.windowMemory 10m
$git_logger config pack.packSizeLimit 20m
$git_logger gc --aggressive --prune
printf "Log complete!\n"
}
function parse_magnets {
# sources and destinations
cd "$(find_directory
|