CompressMissingFiles.sh for *nix servers

Discussions about Servers
Post Reply
User avatar
Barbie
Godlike
Posts: 2808
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

CompressMissingFiles.sh for *nix servers

Post by Barbie »

Just a simple shell script that checks if all available files also exist in the redirect folder for compressed files and compresses them, if not there. It also checks the other way round if there are compressed files that have no uncompressed equivalent and print their names. I have this running for months now without problems. Maybe it helps someone.

Code: Select all

#!/bin/bash

BASEDIR=/home/ut99/ut-server
REDIRECTDIR=$BASEDIR/redirdownloads
# patterns relative to BASEDIR
PATTERNS[0]=Maps/*.unr
PATTERNS[1]=Music/*.umx
PATTERNS[2]=Sounds/*.uax
PATTERNS[3]=System/*.u
PATTERNS[4]=Textures/*.utx
EXCLUDEFILES="Engine.u IpDrv.u IpServer.u UBrowser.u UMenu.u UTBrowser.u UTMenu.u UTServerAdmin.u UWeb.u UWindow.u"

function ErrorExit {
	echo error: $@
	exit 1
}


function extract_filename {
  # returns the part of the given parameter until (but without)
  # the last dot.
  # The result is not necessarly the filename itself - it may still
  # contain a path. Use "basename" for that...

  echo ${1%.*}
}



function extract_file_ext {
  # returns the (last) extension of the given parameter
  echo ${1##*.}
}


function GetDirToExt {
# 1. param: extension (eg "uax")
# returns the directory given in PATTERNS
local EXT="$1"

#	echo ${#PATTERNS[@]}
	for ((i=0; i < ${#PATTERNS[@]}; i++)); do
		# echo "${PATTERNS[$i]}"
		CUREXT=$(extract_file_ext "${PATTERNS[$i]}")
		if [ "$CUREXT" = "$EXT" ]; then
			echo $(dirname "${PATTERNS[$i]}")
			return
		fi
	done

#	for DIR in "${PATTERNS[@]}" ; do
#		CUREXT=$(extract_file_ext "$DIR")
#		echo $DIR
#	done
}

function DoExclude {
	for i in $EXCLUDEFILES; do
		test "$i" = "$1" && return 0
	done
	return 1
}


function CheckRedirFiles {
	for i in $REDIRECTDIR/*; do
		test -f "$i" || continue
		if [ -L "$i" ]; then
			echo "skipping soft link \"$i\""
			continue
		fi
		REDIR_FILENAME=$(basename "$i")
		REDIR_EXT=$(extract_file_ext "$REDIR_FILENAME")
		if [ "$REDIR_EXT" != "uz" ]; then
			echo "Warning: extension 'uz' expected for file \"$i\", skipping"
			continue
		fi
		# now left out the extension "uz":
		REDIR_FILENAME=$(extract_filename "$REDIR_FILENAME")
		# now get the extension of the uncompressed file:
		EXT=$(extract_file_ext "$REDIR_FILENAME")
		if [ -z "$EXT" ]; then
			echo "Error: could not extract the uncompressed extension for file \"$i\", skipping"
			continue
		fi
		SRCDIR=$(GetDirToExt "$EXT")
		if [ -z "$SRCDIR" ]; then
			echo "Error: could determinate the source directory for the extension \"$EXT\" of file \"$i\", skipping"
			continue
		fi
		test -f "$SRCDIR/$REDIR_FILENAME" || echo "file not needed: $i"
	done
}


CheckRedirFiles

cd $BASEDIR/System
for DIR in ${PATTERNS[@]}; do
	for f in ../$DIR; do
		FN=$(basename $f)
		if [ -f "$REDIRECTDIR/$FN.uz" ]; then
			test "$f" -ot "$REDIRECTDIR/$FN.uz" && continue
		fi
		DoExclude "$FN" && continue
		test -f "$f.uz" && rm -v "$f.uz"
		./ucc-bin compress "$f" -nohomedir | grep "Compressed"
		test -f "$f.uz" || ErrorExit "compressed file $f.uz does not exist"
		# touch "$f.uz" --reference="$f"
		mv "$f.uz" "$REDIRECTDIR/"
    done
done
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
Post Reply