view bin/index @ 726:78469331407e

Update syntaxes
author nanaya <me@nanaya.net>
date Mon, 19 Aug 2024 17:04:13 +0900
parents 5bafb912837e
children
line wrap: on
line source

#!/bin/sh

#un(POSIX / bourne shell)-ness: "read -r"

WORKDIR="${PWD}"
TEMPFILE="${WORKDIR}/temp/all"
RESFILE="${WORKDIR}/list/all"
ARCDIR="${WORKDIR}/archives"
FILEDIRS="complete1 complete3 complete4 complete5 incomplete movie"
#FILEDIRS="complete-all incomplete movie"

Err_Fatal() {
	echo "Error: $*"
	exit 1
}

Progress_Init() {
	_last="-"
	printf "${_last}"
}

Progress_Anim() {
	case "${_last}" in
		/) _last="-";;
		-) _last=\\;;
		\\) _last=\|;;
		\|) _last="/";;
	esac
	printf "\b${_last}"
}

Progress_Done() { printf "\bdone\n"; }

Check_Dirs() {
	[ -w "${WORKDIR}" ] || Err_Fatal "Unable to write ${WORKDIR}"
	for dir in temp list archives backup; do
		mkdir -p "${WORKDIR}/${dir}" 2>/dev/null
		[ -w "${WORKDIR}/${dir}" ] || Err_Fatal "Unable to write ${WORKDIR}/${dir}"
	done
}

Finish_Copy() {
	printf "Copying result files... "
	rm "${WORKDIR}/list/"*
	cp "${WORKDIR}/temp/"* "${WORKDIR}/list"
	mkdir -p "archives/${DATE}" && cp "${WORKDIR}/temp/"* "${WORKDIR}/archives/${DATE}"
	echo "done"
}

Generate_File_List() {
	printf "Generating list of files... "
	> "${TEMPFILE}-bare"
	for i in ${FILEDIRS}; do
		printf "${i}... "
		dir="/anime/${i}"
		[ -d "${dir}" ] && find "${dir}/"* -type f >> "${TEMPFILE}-bare"
	done
	echo "done"
	printf "Checking list validity... "
	[ -n "$(grep -v ^/ "${TEMPFILE}-bare")" ] && Err_Fatal "Newline in filename. Please check ${TEMPFILE}-bare"
	echo "done"

	printf "Generating list of filenames... "
	sed -e 's/.*\/\([^/]*\)/\1/' "${TEMPFILE}-bare" > "${TEMPFILE}-basename"
	echo "done"

	printf "Sorting and finding duplicates... "
	sort -- "${TEMPFILE}-basename" > "${TEMPFILE}-basename-sorted"
	mv -- "${TEMPFILE}-basename-sorted" "${TEMPFILE}-basename"
	sort -u -- "${TEMPFILE}-basename" > "${TEMPFILE}-basename-sortuniq"
	comm -23 -- "${TEMPFILE}-basename" "${TEMPFILE}-basename-sortuniq" > "${TEMPFILE}-dup-basename"
	echo "done"
	if [ "$(wc -l < "${TEMPFILE}-dup-basename")" -gt 0 ]; then 
		> "${TEMPFILE}-dup-bare"
		printf "Duplicate files detected. Generating list of duplicates... "
		Progress_Init
		while read -r dup; do
			fgrep "${dup}" "${TEMPFILE}-bare" >> "${TEMPFILE}-dup-bare"
			Progress_Anim
		done < "${TEMPFILE}-dup-basename"
		Progress_Done
		Err_Fatal "Duplicated files detected. Please resolve this before continuing.
       Check ${TEMPFILE}-dup-basename for list of duplicates"
	else
		rm "${TEMPFILE}-dup-basename" "${TEMPFILE}-basename-sortuniq"
	fi
	#yay finished
}

Generate_New_List() {
	printf "Generating list of new files... "
	Progress_Init
	> "${TEMPFILE}-newbase"
	sort "${RESFILE}-basename" > "${TEMPFILE}-res-basename"
	mv "${TEMPFILE}-res-basename" "${RESFILE}-basename"
	comm -13 "${RESFILE}-basename" "${TEMPFILE}-basename" > "${TEMPFILE}-newbase"
	Progress_Anim
	> "${TEMPFILE}-new"
	while read -r added; do
		fgrep "${added}" "${TEMPFILE}-bare" >> "${TEMPFILE}-new"
		Progress_Anim
	done < "${TEMPFILE}-newbase"
	rm "${TEMPFILE}-newbase"
	Progress_Done
}

Init_Check() {
	cp "${TEMPFILE}-bare" "${TEMPFILE}-new"
}

Run_ed2k() {
	#[ "$(echo $(wc -l < "${TEMPFILE}-new"))" -eq 0 ] && Err_Fatal "Not an error. Just lazy to create new handler. (Nothing new)"
	printf "Generating ed2k links...0"
	> "${TEMPFILE}-ed2k-newlist"
	num=0
	while read -r file; do
		ed2k -l "${file}" >> "${TEMPFILE}-ed2k-newlist"
		num=$((num+1))
		if [ "$((num%10))" -eq 0 ]; then
			printf "${num}"
		else
			printf "."
		fi
	done < "${TEMPFILE}-new"
	echo " done"
}

Generate_Removed() {
	printf "Generating list of removed files... "
	Progress_Init
	cp "${TEMPFILE}-ed2k-newlist" "${ARCDIR}/ed2k-newlist.${DATE}"
	cat "${RESFILE}-ed2k-newlist" >> "${TEMPFILE}-ed2k-newlist"
	sort -u "${TEMPFILE}-ed2k-newlist" > "${TEMPFILE}-ed2k-newlist-temp"
	mv "${TEMPFILE}-ed2k-newlist-temp" "${TEMPFILE}-ed2k-newlist"
	Progress_Anim
	comm -13 "${TEMPFILE}-basename" "${RESFILE}-basename" | sort > "${TEMPFILE}-removed"
	Progress_Anim
	if [ "$(echo $(wc -l < "${TEMPFILE}-removed"))" -gt 0 ]; then
		> "${TEMPFILE}-ed2k-newlisttemp"
		while read -r deleted; do
			fgrep "${deleted}" "${TEMPFILE}-ed2k-newlist" >> "${TEMPFILE}-ed2k-removed"
			Progress_Anim
			fgrep -v "${deleted}" "${TEMPFILE}-ed2k-newlist" > "${TEMPFILE}-ed2k-newlisttemp"
			mv "${TEMPFILE}-ed2k-newlisttemp" "${TEMPFILE}-ed2k-newlist"
			Progress_Anim
		done < "${TEMPFILE}-removed"
		cp "${TEMPFILE}-ed2k-removed" "${ARCDIR}/ed2k-removed.${DATE}"
		#sed -e 's/ed2k:\/\/|file|\(.*\)|[^|]*|[^|]*|$/\1/' "${TEMPFILE}-ed2k-removed" > "${TEMPFILE}-base-removed"
		Progress_Anim
		while read -r deleted; do
			fgrep "${deleted}" "${RESFILE}-bare" >> "${TEMPFILE}-bare-removed"
			Progress_Anim
		done < "${TEMPFILE}-removed"
		cp "${TEMPFILE}-bare-removed" "${ARCDIR}/bare-removed.${DATE}"
	fi
	Progress_Done
}

INIT=
UPDATE=
DATE=$(date -u "+%Y%m%d-%H.%M")
case "$1" in
	init) INIT=1;;
	update|up) UPDATE=1;;
	*) Err_Fatal "You fail.";;
esac
Check_Dirs 
[ -n "${UPDATE}" ] && cd "${WORKDIR}" && tar zcf "${WORKDIR}/backup/${DATE}.tar.gz" "temp" "list" "archives"
[ -n "${UPDATE}" ] && rm "${WORKDIR}/temp/"* 2>/dev/null
Generate_File_List
[ -n "${INIT}" ] && Init_Check
[ -n "${UPDATE}" ] && Generate_New_List
Run_ed2k 
[ -n "${UPDATE}" ] && Generate_Removed
Finish_Copy