#!/bin/sh

# script to move stuff from  ~/.ut2004/Cache elsewere, e.g.
# do a chmod o+wt to /u/loc/games/ut../* folders to move
# stuff there, duplicates are overwritten (if possible)
# GPL'ed :)

target=~/.ut2004 #trailing slash not required
#target=/usr/local/games/ut2004 #trailing slash not required
#cmd="echo mv" #test, also comment out last line if testing!
cmd="mv -v" #action, DON'T add '-i' since piping into sh
            # is strictly noninteractive!

cd ~/.ut2004/Cache || exit 1
echo '[Cache]' > cache.ini.new
cp cache.ini cache.ini.bak.$$ # never know.. ;)
grep '=' cache.ini  | awk --field-separator '=' \
--assign cmd="$cmd" --assign=target="$target" \
'function moveit(fromhere, tothere)
{
	tothere="\""target""tothere"\"";
	print "[ -f "tothere" ] && echo \\ \\ \\ "tothere" already there! || "cmd" \""fromhere".uxx\" "tothere | "sh";
}
{
	#print $1" "$2; # yet another test here
	if(match($2,".ukx$"))      { moveit($1, "/Animations/"$2 );  }
	else if(match($2,".ut2$")) { moveit($1, "/Maps/"$2 );  }
	else if(match($2,".ogg"))  { moveit($1, "/Music/"$2 );  }
	else if(match($2,".uax$")) { moveit($1, "/Sounds/"$2 );  }
	else if(match($2,".usx$")) { moveit($1, "/StaticMeshes/"$2 );  }
# preferably not... - better install mods youself
	else if(match($2,".u$"))   { moveit($1, "/System/"$2 );  }
	else if(match($2,".utx$")) { moveit($1, "/Textures/"$2 );  }
	else printf $1"="$2"\n" >> "cache.ini.new";
}
END { close("sh") }'

#comment out if testing:
mv cache.ini.new cache.ini
