summaryrefslogtreecommitdiff
path: root/home/xyz/.local/bin/lastarg
blob: 58f1f14eb27df16ceb1d7085dd74b3871afe4d29 (plain)
1
2
3
4
5
#!/bin/sh
# useage: lastarg "$@"

shift $(($# - 1))
echo "$1"
'#n72'>72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
#!/bin/sh

die () {
	echo "error: $*" >&2
	exit 1
}

all () {
	# pp maybe finish upd -a first and start to suspend, and it cause insp music() failed to rsync to pp, so I need to finish rsync to ssh before pp finish upd, so I put music() here, not perfect but good enough
	[ "$hostname" = xyzinsp ] && music
	# monthly_misc() needs to use qbittorrent-nox APIs, so I need to restart qbt before that, so I put qb() at top here, not perfect but good enough
	if [ "$hostname" = xyzib ]; then
		qb
	fi
	fast
	clean
	# don't run reflector if it is pp or insp, because they all VPN to ba now
	if ! { [ "$hostname" = xyzpp ] || [ "$hostname" = xyzinsp ];}; then
		refl
	fi
	monthly_misc
	if [ "$hostname" = xyzinsp ] || [ "$hostname" = xyzpp ]; then
		userjs
	fi
}

backup () {
	#case "$hostname" in
	#	xyzinsp) backup_branch=master;;
	#	*) backup_branch=${hostname#xyz};;
	#esac

	if [ "$hostname" = xyzinsp ]; then
		# Pull repos and branches to insp, so one more backup on insp or duplicity will backup to ib
		# Also sync the local dir to work on it
		cd "$HOME/programs/config_local_arch" || exit
		git branch | awk '{print ($1=="*")?$2:$1}' | while read -r branch; do
			git checkout "$branch"
			git pull
			# Push any branches other than master/pp to codeberg or any future alternative website for mirroring. Needed for public codes/data, considering my death may result in all copies on my computers being lost.
			case "$branch" in
				master|pp) ;;
				*) git push;;
			esac
		done
		# always go back to a specific branch, because future new branshes may
		# make previous git checkout checkout to that new branch cause rsync
		# have to sync that new branch instead
		git checkout master
		# git checkout will change mtime, need to change back so rsync won't think it needs to backup these files
		# https://stackoverflow.com/q/1964470/9008720
		# https://stackoverflow.com/q/21735435/9008720
		# https://github.com/MestreLion/git-tools
		# aur/git-tools-git
		git restore-mtime

		(
			umask 077
			# need to pull VPS cfgs repos (ca, ib, ba, etc.) to /root/programs for insp to rsync to external HDD to satisfy 321 backup rule
			for dir in $(sudo find /root/programs -maxdepth 1 -mindepth 1 -type d -name 'config_local_arch_secrets*'); do
				sudo -E git -C "$dir" pull
			done
		)

		# rsync backup from ib to insp ~/backup/ib
		rsync -avPR --delete xyz@ib.flylightning.xyz:/home/xyz/.config/qBittorrent :/home/xyz/.local/share/qBittorrent/BT_backup "$HOME/backup/ib"

		# rsync backup from pp to insp ~/backup/pp
		rsync -vPu pp:.config/myconf/upd_rsync_files "$XDG_CONFIG_HOME/myconf/upd_rsync_files_pp"
		# --files-from make -a not imply -r, so need to specify explicitly
		rsync -avPRr --delete --files-from="$XDG_CONFIG_HOME/myconf/upd_rsync_files_pp" pp:/ "$HOME/backup/pp"

		# rsync backup to externel drive
		while ! [ -d /run/media/xyz/bd0 ]; do
			alarm 0 'Plug in external HDD'
			echo 'Plug in external HDD'
			sleep 10
		done
		# --files-from make -a not imply -r, so need to specify explicitly
		# TODO: first time mkdir, chown, chmod proper dirs in /run/media/xyz/bd0, see vq notes
		# backup insp
		rsync -avPRr --delete --files-from="$XDG_CONFIG_HOME/myconf/upd_rsync_files" / "/run/media/xyz/bd0/master"
		# need to backup VPS cfgs repos (ca, ib, ba, etc.) in /root/programs to external HDD to satisfy 321 backup rule
		# need to backup insp cfgs repo to external HDD to satisfy 321 backup rule
		sudo rsync -avPR --delete /etc/.cfgs /root/archive /root/programs "/run/media/xyz/bd0/master_root"
		# backup ib
		rsync -avP --delete "$HOME/backup/ib/" "/run/media/xyz/bd0/ib"
		# backup pp
		rsync -avP --delete "$HOME/backup/pp/" "/run/media/xyz/bd0/pp"

		# busybox df if filesystem name too long, it will put the filesystem
		# name in a separate line which nornally is in the second line and
		# there's total three lines. So I use df -P option which seems busybox
		# will also not put filesystem name in a separate line and behave same
		# as gnu df.
		bd0_fs_name="$(df -P /run/media/xyz/bd0 | awk 'END{print $1}')"
		umount /run/media/xyz/bd0
		sudo cryptsetup close "$bd0_fs_name"
		alarm 0 'Unplug external HDD'

		# duplicity backup to ib
		# note no need to duplicity backup /root/programs to ib for now, because /root/programs now only has cfgs repos which already satisfied 321 backup rule, e.g, insp cfgs is backed up with: insp, duplicty backuped with /etc/.cfgs, rsync backup to external HDD; ib cfgs is backed up with: ib, insp gitolite, insp rsync backup /root/programs to external HDD
		# https://wiki.archlinux.org/title/Duplicity
		# Need // for absolute path, see manpage URL FORMAT section. If not use //, will store to /home/xyz/home/xyz/...
		# --files-from has a bug, this bug makes source url that is / not working while /home works, more see vq notes
		# --use-agent not working when ssh to pp and insp, works on insp, not sure why
		# --use-agent maybe timeout on gpg key and failed when do full backup, maybe due to key stored in gpg agent timeout, so I'm not using --use-agent on insp now
		sudo duplicity --ssh-askpass --encrypt-key 9790577D2BE328D46838117ED3F54FE03F3C68D6 --sign-key 05899270DF25BB1EEDF57BE824F769E5D08C9E9A --full-if-older-than 2Y --include /etc/.cfgs --include /root/archive --include-filelist "/home/xyz/.config/myconf/upd_rsync_files" --exclude / / "sftp://xyz@ib.flylightning.xyz//home/xyz/backup/master"
	fi

	if [ "$hostname" = xyzpp ]; then
		# duplicity backup to ib
		sudo duplicity --ssh-askpass --encrypt-key 9790577D2BE328D46838117ED3F54FE03F3C68D6 --sign-key 05899270DF25BB1EEDF57BE824F769E5D08C9E9A --full-if-older-than 2Y --include /etc/.cfgs --include-filelist "/home/xyz/.config/myconf/upd_rsync_files" --exclude / / "sftp://xyz@ib.flylightning.xyz//home/xyz/backup/pp"
	fi
}

clean () {
	if [ "$hostname" = xyzinsp ]; then
		nsxiv -c
		# my ways
		# -exec can't replace -execdir here
		find "$XDG_CACHE_HOME/nsxiv/" -depth -type d -empty -execdir rmdir -- '{}' \+
		# -exec can replace -execdir here
		#find "$XDG_CACHE_HOME/nsxiv/" -depth -type d -execdir rmdir --ignore-fail-on-non-empty -- '{}' \+
		# nsxiv man page way
		#find "$XDG_CACHE_HOME/nsxiv/" -depth -type d -empty ! -name '.' -exec rmdir -- '{}' \;

	fi

	if [ "$hostname" = xyzinsp ] || [ "$hostname" = xyzpp ]; then
		cd "$HOME/.mozilla/firefox/xxxxxxxx.fly/prefsjs_backups" || exit
		# https://stackoverflow.com/a/34862475/9008720
		ls -t | tail -n +11 | tr '\n' '\0' | xargs -0 rm --
		cd "$HOME/.mozilla/firefox/xxxxxxxx.fly/userjs_backups" || exit
		ls -t | tail -n +11 | tr '\n' '\0' | xargs -0 rm --
		# https://unix.stackexchange.com/questions/92095/reset-atq-list-to-zero
		sudo systemctl stop atd
		echo 0 | sudo tee /var/spool/atd/.SEQ > /dev/null
		sudo systemctl start atd
		rm -rf "$XDG_VIDEOS_DIR/recordings/tmp/"
	fi

	paru -aSc --noconfirm

	# https://wiki.archlinux.org/title/Pacman/Tips_and_tricks#Removing_unused_packages_(orphans)
	pacman -Qdttq | sudo pacman -Rns --noconfirm -
}

# basic daily stuff
fast () {
	pac
	misc
	if [ "$hostname" = xyzinsp ] || [ "$hostname" = xyzpp ]; then
		backup
	fi
}

userjs () {
	kill $(pidof "$BROWSER")
	# change working dir for cleaner
	cd "$HOME/.mozilla/firefox/xxxxxxxx.fly" || exit
	arkenfox-cleaner -s
	# when multiple firefox profiles, it will prompt me to choose, which breaks automation, so I explicitly specify one profile
	arkenfox-updater -s -p "$HOME/.mozilla/firefox/xxxxxxxx.fly"
}

misc () {
	"$EDITOR" +PlugClean! +PlugUpdate +qa

	if [ "$hostname" = xyzinsp ] || [ "$hostname" = xyzpp ]; then
		tldr --update
	fi

	if [ "$hostname" = xyzinsp ]; then
		sudo hardcode-fixer
		ssh pp '[ -s "$HOME/.local/share/sdcv_history" ] && cat "$HOME/.local/share/sdcv_history" && rm "$HOME/.local/share/sdcv_history"' >> "$XDG_DATA_HOME/sdcv_history"
		awk '!a[$0]++' "$XDG_DATA_HOME/sdcv_history" | sponge "$XDG_DATA_HOME/sdcv_history"
		# temperory solution before find a way of using git submodule or subtree with `cfg -l`
		git -C "$HOME/.mozilla/firefox/xxxxxxxx.fly/chrome/firefox-csshacks" pull
		git -C "$XDG_DOCUMENTS_DIR/notes" commit --all -m 'update'
		git -C "$XDG_DOCUMENTS_DIR/notes" push
		git -C "$HOME/programs/reminders" commit --all -m 'update'
		git -C "$HOME/programs/reminders" push
		pass git push
		argospm update
	fi

	if [ "$hostname" = xyzpp ]; then
		git -C "$XDG_DOCUMENTS_DIR/notes" pull
		git -C "$HOME/programs/reminders" pull
		pass git pull
	fi
}

pac () {
	pacout="$(sudo pacman --noconfirm -Syu | tee /dev/tty)"
	pacpacs="$(echo "$pacout" | grep -m1 '^Packages' | cut -d' ' -f3-)"
	# Update rust toolchains before paru so paru can compile things in newest rust if needed.
	[ "$hostname" = xyzinsp ] && rustup update
	aurout="$(paru --color never --noconfirm -aSu | tee /dev/tty)"
	aurpacs="$(echo "$aurout" | grep '^Aur' | cut -d' ' -f3-)"
	# /usr/share/libalpm/hooks/rebuild-detector.hook has a line `NeedsTargets` shows it maybe checkrebuild only upgraded packages by `printf 'zoom\nminiconda3' | checkrebuild` instead of maybe check all by `checkrebuild`, so I think query pacman hook will be faster than run another `checkrebuild`
	# notes about awk f=1 things see https://git.flylightning.xyz/public_archive_codes/tree/sh/mrt
	# about `/^(\(|:|=)/ {f=0}`:
	# - consider $aurout start with `^:: Looking for devel upgrades...` , rebuild-detector.hook maybe the last hook to run for $pacout
	# - consider ^(4/5), the hook is not the last
	# - consider paru `==> Making package: ...`, the hook maybe followed by this. Note: paru somehow still gives color output even if I use --color never, so I can't check with ^=, so I choose to check with ==>
	# awk use `if(!a[$2]++)` to check if package name is repeated in multiple checkrebuild pacman hook run, happened when upgrade python cause all python packages need to be rebuilt
	# TODO: Some packages maybe are rebuilt later on when paru upgrade packages, but those will still got shown in upd log. Try consider this situation. e.g., when pacman upgrade packages, checkrebuild hook output a b c d packages, then paru upgrade d, now checkrebuild hook output a b c, the final upd log will have a b c d all packages instead of a b c
	checkrebuild_pacs="$(echo "$pacout$aurout" | awk '
	/^\([0-9]+\/[0-9]+\) Checking which packages need to be rebuilt$/ {f=1; next}
	/^(\(|:)|==>/ {f=0}
	f {
		if($2!~"zoom|miniconda3")
			if(!a[$2]++)
				printf("%s ",$2)
	}')"
	# part steal from aur comment
	# sometimes "ERROR: Failure while downloading": https://github.com/neovim/neovim/issues/15709
	# echo 1, printf 1 and yes 1 all works? not sure why
	# aur neovim-nightly-bin has some issue on 12/26/2021? switch to community repo neovim temporary
	#rm -rf ~/.cache/paru/clone/neovim-nightly-bin/ && echo 1 | PARU_PAGER=cat paru --rebuild --redownload neovim-nightly-bin
	if [ "$hostname" = xyzinsp ]; then
		case "$pacpacs" in
			*qt5-base*) echo 1 | PARU_PAGER=cat paru --rebuild qt5-styleplugins;;
		esac
		case "$pacpacs" in
			*qt6-base*) echo 1 | PARU_PAGER=cat paru --rebuild qt6gtk2;;
		esac
	fi
	pacman -Qqme > "$XDG_CONFIG_HOME/myconf/pacman_Qqme"
	pacman -Qqne > "$XDG_CONFIG_HOME/myconf/pacman_Qqne"
	systemctl list-unit-files --state=enabled > "$XDG_CONFIG_HOME/myconf/sye"
	systemctl --user list-unit-files --state=enabled > "$XDG_CONFIG_HOME/myconf/syue"

	if echo "$aurpacs" | grep -q 'zoom-[0-9]'; then
		new_schemes="$(grep -m1 '^X-KDE-Protocols=' /usr/share/applications/Zoom.desktop)"
		old_schemes="$(grep -m1 '^X-KDE-Protocols=' "$HOME/programs/repos/aur/zoom-firejail/ZoomFirejail.desktop")"

		if [ "$new_schemes" != "$old_schemes" ]; then
			zoom_firejail_log="zoom-firejail updated MIME type, test then aurpublish"
			# aur/zoom-firejail author snowball did not include
			# application/x-zoom in MimeType, not sure why, I decided to follow
			# what he did anyway
			new_mime="$(awk -F';' '
			/^MimeType=/ {
				for(i=1;i<=NF;i++)
					if($i != "application/x-zoom" && $i != "")
						printf("%s;",$i)
				exit
			}' /usr/share/applications/Zoom.desktop)"
			schemes="$(echo "$new_schemes" | awk -F'[=;]' '
			{
				for(i=2;i<=NF;i++)
					if($i != "")
						printf(" %s",$i)
			}
			')"
			old_pkgrel="$(awk -F= '/^pkgrel=/{printf("%d",$2);exit}' "$HOME/programs/repos/aur/zoom-firejail/PKGBUILD")"

			sed -i -e "s/^X-KDE-Protocols=.*/$new_schemes/" \
				-e "s#^MimeType=.*#$new_mime#" \
				"$HOME/programs/repos/aur/zoom-firejail/ZoomFirejail.desktop"
			# must checksum after write new desktop entry because PKGBUILD need new checksum
			desktop_sum="$(sha512sum "$HOME/programs/repos/aur/zoom-firejail/ZoomFirejail.desktop" | awk '{printf("%s",$1)}')"
			sed -i -e "s/^\(    for scheme in\).*\(; do\)$/\1${schemes}\2/" \
				-e "s/^\(    if \[ \$(vercmp \$2 [0-9]\+\.[0-9]\+\.[0-9]\+-\)[0-9]\+\() -le 0 \]; then\)$/\1${old_pkgrel}\2/" \
				"$HOME/programs/repos/aur/zoom-firejail/zoom-firejail.install"
			sed -i -e "s/^pkgrel=.*/pkgrel=$((old_pkgrel+1))/" \