summaryrefslogblamecommitdiffstats
path: root/server.sh
blob: 1b12f4aec20548ad023b7f4bf0beca8cdb11000d (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 docker docker-compose ffmpeg getmail git gnuplot journalctl logname media-sort mp3val mpack munpack opustags phockup pip3 python3 qpdf rbw rclone sed seq sort svgo 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 {
	case "$1" in
	addr) check_not_root && rbw get --full "$2" | awk '/URI:/ {print $2}' ;;
	full) check_not_root && rbw get --full "$2" ;;
	pass) check_not_root && rbw get "$2" ;;
	sync) check_not_root && rbw sync ;;
	user) check_not_root && rbw get --full "$2" | awk '/Username:/ {print $2}' ;;
	*) check_not_root && rbw get "$2" ;;
	esac
}
function duolingo_streak {
	# check api is installed
	[[ -d "$(find_directory config)/duolingo" ]] || git clone https://github.com/KartikTalwar/Duolingo.git "$(find_directory config)/duolingo"
	# cd to git dir to include module
	cd "$(find_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"
	echo -n "Amending lastmod value... "
	mod_timestamp="$(date +%FT%H:%M:00)"
	sed -i "s/lastmod: .*/lastmod: $mod_timestamp/g" "$rank_filename"
	echo -e "$i \e[32mdone\e[39m"
}
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 "RCLONE_REMOTE_MEDIA=%s\\n" "$(find_remote media)"
		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)"
	} >"$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