#!/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

# Fix the line endings of the included text files, makefiles, and sources.
if [ -x "`which d2u 2> /dev/null`" ]; then
  d2u `find . -name "*.txt" -print` 2> /dev/null
  d2u `find . -name "*.mak" -print` 2> /dev/null
  d2u `find . -path './src/unix' -prune -o -name "*.[ch]" -print` 2> /dev/null
  d2u makefile.mes 2> /dev/null
  d2u sysinfo.dat 2> /dev/null
else
  echo "Unable to run d2u.  This may cause build problems on some platforms."
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 crc memcard nvram snap software

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

if [ -f messnew.txt ]; then
  mv messnew.txt doc/changes.mess
fi

if [ -f messold.txt ]; then
  mv messold.txt doc/old-changes.mess
fi

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

if [ -f windows.txt ]; then
  mv windows.txt doc/
fi

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

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

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

# Delete the MESS tools directory.
rm -r tools

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