#!/bin/bash
# This is a script which automates some of the operations needed for porting DOS
# MESS to UN*X.  Run it in a directory containing the latest xmame sources.  You
# must specify a path to the latest MESS sources, which were unzipped with -L to
# force filenames to lower case.  For example, here's what I would do in my
# ~/emulators directory to port 0.37b14:
#           mkdir mess-0.37b14
#           cd mess-0.37b14
#           unzip -L ../ms37b14s.zip
#           cd ../xmame-0.37b14.1
#           prep-new-mess ../mess-0.37b14
#
# Lawrence Gold <gold@aros.net>
# Last modified on 11 April 2001.

# Copy the MESS files over.
if [ -d "$1" ]; then
	cp -R $1/* .
else
	echo "Cannot find MESS source directory, exiting..."
	exit 1
fi

D2U=
if [ -x "`which dos2unix 2> /dev/null`" ]; then
	D2U="dos2unix -kq"
elif [ -x "`which d2u 2> /dev/null`" ]; then
	D2U="d2u"
else
	echo "Unable to run dos2unix or d2u.  This may cause build problems on some platforms."
fi

# Fix the line endings of the included text files, makefiles, and sources.
if [ -n "$D2U" ]; then
	$D2U `find . -name "*.txt" -print` 2> /dev/null
	$D2U `find . -name "*.htm" -print` 2> /dev/null
	$D2U `find . -name "*.html" -print` 2> /dev/null
	$D2U `find . -name "*.mak" -print` 2> /dev/null
	$D2U `find . -name "*.hsi" -print` 2> /dev/null
	$D2U `find . -name "*.vc" -print` 2> /dev/null
	$D2U `find . -name "*.wce" -print` 2> /dev/null
	$D2U `find . -name "*.xml" -print` 2> /dev/null
	$D2U `find . -name "*.cfg" -print` 2> /dev/null
	$D2U makefile.mes 2> /dev/null
	$D2U makefile.vc 2> /dev/null
	$D2U sysinfo.dat 2> /dev/null
fi

# Change // comments to /* */ pairs.  Some C compilers don't like //.  We skip
# the src/unix directory, though, since fix-comments mangles some of the files.
# Besides, they should already be fine.
fix-comments `find . -path './src/unix' -prune -o -name "*.[ch]" -print`

# Do some cleanup.
rm -rf artwork bios cfg cheat memcard nvram snap software

# Rename some files.
if [ -f messnew.txt ]; then
	mv messnew.txt doc/mess/
fi

if [ -f sysinfo.dat ]; then
	mv sysinfo.dat doc/mess/
fi

if [ -f tools/imgtool.txt ]; then
	mv tools/imgtool.txt doc/mess/
fi

if [ -f tools/messroms.txt ]; then
	mv tools/messroms.txt doc/mess/
fi

if [ -d docs ]; then
	mv docs/* doc/mess/
	rm -r docs
fi

# Clean up the files left over from d2u and fix-comments.
rm -f `find . -name "*~" -print`
rm -f `find . -name "*.orig" -print`
