#!/bin/busybox ash

# update-everything v7.2 (March 31, 2023)
# by Bruno "GNUser" Dantas, with special thanks to jazzbiker and Rich
# GPLv3

# Purpose: Do a full TCL system update as quickly and efficiently as possible, leaving custom extensions intact 
# Usage: $ update-everything

PATH="/bin:/sbin:/usr/bin:/usr/sbin"
export PATH

main()
{
	echo "Looking for changed .dep files..."
	get_depdb
	generate_dep_files
	sync_dep_files

	if $DEP_FILES_UPDATED; then
		echo "Building package database..."
		tce-audit builddb
		echo "Looking for missing dependencies..."
		tce-audit fetchmissing
	fi

	echo "Updating extensions..."
	tce-update --skip-dependency-check

	rm -rf $DEPDIR
	exit 0
}

get_depdb()
{
	rm -rf "$DEPDIR"; mkdir -p "$DEPDIR"
	cd "$DEPDIR"
	. /etc/init.d/tc-functions
	getMirror
	wget -q "$MIRROR"/"$DBGZ"
	gunzip -kf "$DBGZ"
}

generate_dep_files()
{
	awk 'BEGIN {FS="\n";RS=""} { out=$1".dep"; for (i=2; i<=NF; i++) printf("%s\n", $(i)) >out; close(out) }' dep.db
}

sync_dep_files()
{
	DEP_FILES_UPDATED=false
	for depfile in $(find $OPTIONALDIR -name '*.dep' -exec basename {} \;); do
		extension=${depfile%.dep}
		if [ ! -f $DEPDIR/$depfile ]; then # it's a custom extension, leave .dep file alone
			continue
		elif [ ! -f $OPTIONALDIR/$extension.md5.txt ]; then # it's a custom extension, leave .dep file alone
			continue
		# echo is used in the comparison to eat whitespace (e.g., trailing spaces and blank lines):
		elif [ "$(echo $(cat $OPTIONALDIR/$depfile))" = "$(echo $(cat $DEPDIR/$depfile))" ]; then # .dep file is current
			continue
		else
			echo "local $depfile is different from one in mirror, fixing it now..."
			cp $DEPDIR/$depfile $OPTIONALDIR
			DEP_FILES_UPDATED=true
		fi
	done
}

# internal variables, do not touch:
OPTIONALDIR="/etc/sysconfig/tcedir/optional"
DEPDIR="/tmp/depfiles"
DB="dep.db"
DBGZ="$DB.gz"

main
