#!/bin/sh
#
# 2da ripper for NWN
# grifter 2k4

OLDPATH=`pwd`
RENAME=`md5sum /usr/bin/rename | cut -c 1-32`
if [ ! $RENAME = 0aeadea2d394a8cb2727091991019098 ]; then
	echo "Your system lacks the required rename perlscript (not the ridiculous and useless ELF binary)"
	echo "If you still wish to use it, lowercase all mdl files before you begin, then edit this script"
	exit 1
fi

placeables () {
COUNTER=1000
rm -f ${OLDPATH}/placeables.2da
touch ${OLDPATH}/placeables.2da

for i in $( find `pwd` -name '*resources*' ); do
	cd $i
	rename 'y/A-Z/a-z/' *mdl
	for o in $( ls *mdl | sed 's/....$//' | sort ); do
		FUNKY=`grep --mmap -i -w -m 1 $o placeables.2da | cut -c 1`
		if [ ! -z $FUNKY ]; then
			DUPE=`grep --mmap -i -w -m 1 $o ${OLDPATH}/placeables.2da | cut -c 1`
			if [ -z $DUPE ]; then
				grep --mmap -i -w -m 1 $o placeables.2da | sed "s/^..../$COUNTER/" | sed 'y/A-Z/a-z/' >> ${OLDPATH}/placeables.2da
				echo "inputting line $COUNTER"
				let COUNTER+=1
			fi
		else
		echo "Something is funky, can't add ${i}/${o} to master 2da" >> ${OLDPATH}/errorlog.placeables.2da
		fi
	done
done
}


case "$1" in

	placeables)
		placeables
		;;
	portraits)
		portraits
		;;
	sounds)
		placeableobjsnd
		;;
	appearances)
		appearances
		;;
	doortypes)
		doortypes
		;;
	loadscreens)
		loadscreens
		;;
	genericdoors)
		genericdoors
		;;
	*)
		echo "Usage: $0 argument"
		echo " "
		echo "Where argument is one of the following:"
		echo " "
		echo "placeables: creates a placeables.2da"
		echo "portraits: creates a portraits.2da"
		echo "sounds: creates a placeableobjsnd.2da"
		echo "appearances: creates an appearances.2da"
		echo "doortypes: creates a doortypes.2da"
		echo "loadscreens: creates a loadscreens.2da"
		echo "genericdoors: creates a genericdoors.2da"
		echo " "
		echo "You have to be in placeables root when using."
		echo "WILL overwrite appropriate 2da file in the root!!!"
		echo " "
esac
