#!/bin/bash

#### Edit these options for your system

WITHDEBUG=""             # -g for debugging, -p for profiling. -pg for both
STATIC=--static
WITHOPTIMIZE="-O2 -ffast-math -fomit-frame-pointer"

#if compiling for win32, edit WXDEV path to specify the
#location of wxDev-CPP 6.10 (as a cygwin, not windows path.)
#i.e. WXDEV=/cygdrive/c/wxDEV-Cpp
#if left empty, code below will try to locate it, so only set this
#if you've installed it in a different path than the default.

#WXDEV=""


########################################################################



if [ -n "$CYGWIN" ]
then

 #z is first drive because that's how I have it on my machine. :-)
 #c is the most common path of course.
 export EXT=".exe"

 if [ -z "$WXDEV" ]
 then
   [ -d "/cygdrive/z/wxDev-Cpp" ] && export WXDEV="/cygdrive/z/wxDev-Cpp"
   [ -d "/cygdrive/c/wxDev-Cpp" ] && export WXDEV="/cygdrive/c/wxDev-Cpp"
 fi

 # if we still haven't found what we're looking for, look everywhere else
 if [ -z "$WXDEV" ]
 then
  for i in d e f g h i j k l m n o p q r s t u v w x y;
  do
   if [ -z "$WXDEV" ]; then
      [ -d "/cygdrive/${i}/wxDev-Cpp" ] && export WXDEV="/cygdrive/${i}/wxDev-Cpp"
   fi
  done
 fi

 if [ -z "$WXDEV" ]
 then
  echo "Could not find the wxDev C++ 6.10 Environment.  Please download and install it"
  echo "from http://wxdsgn.sourceforge.net/  and install it to C:\\wxDev-Cpp"
  echo "and yes, I did look on every drive for it!"
  exit 1
 fi

 ARCHITECTURE=i586
 #ARCHITECTURE=pentium
 #ARCHITECTURE=pentium4
 #ARCHITECTURE=opteron

 FAILED=0
 [ -z "`/bin/grep -i 'IniVersion=6.10' ${WXDEV}/devcpp.pallete`" ]
 [ -f "${WXDEV}/devcpp.exe" ] || FAILED=1

  if [ $FAILED -eq 0 ]
  then

   export MINGW="${WXDEV}/lib/gcc/mingw32/3.4.2"
   export MINGCPP="${WXDEV}/include/c++/3.4.2"
   export PATH=${WXDEV}/bin:${MINGW}/libexec:${MINGW}/bin/:${PATH}
   export CXXINC="-I ${MINGW}/include  -I ${MINGCPP}/backward  -I ${MINGCPP}/mingw32  -I ${MINGCPP}  -I ${WXDEV}/include  -I ${WXDEV}/  -I ${WXDEV}/include/common/wx/msw  -I ${WXDEV}/include/common/wx/generic  -I ${WXDEV}/include/common/wx/fl  -I ${WXDEV}/include/common/wx/gizmos  -I ${WXDEV}/include/common/wx/html  -I ${WXDEV}/include/common/wx/mmedia  -I ${WXDEV}/include/common/wx/net  -I ${WXDEV}/include/common/wx/ogl  -I ${WXDEV}/include/common/wx/plot  -I ${WXDEV}/include/common/wx/protocol  -I ${WXDEV}/include/common/wx/stc  -I ${WXDEV}/include/common/wx/svg  -I ${WXDEV}/include/common/wx/xml  -I ${WXDEV}/include/common/wx/xrc  -I ${WXDEV}/include/common/wx  -I ${WXDEV}/include/common"
   export CXXDEFS="  -D__WXMSW__ -D__GNUWIN32__ -D__WIN95__ -fno-rtti -fno-exceptions -fno-pcc-struct-return -fstrict-aliasing -Wall -D__WXMSW__ -D__GNUWIN32__ -D__WIN95__   -fexpensive-optimizations -O3"
   export LIBS="-L `cygpath -wp ${WXDEV}/Lib` -L `cygpath -wp ${WXDEV}/Lib/gcc/mingw32/3.4.2/`  -mwindows -lwxmsw27 -lwxmsw27_gl -lwxtiff -lwxjpeg -lwxpng -lwxzlib -lwxregex -lwxexpat -lkernel32 -luser32 -lgdi32 -lcomdlg32 -lwinspool -lwinmm -lshell32 -lcomctl32 -lole32 -loleaut32 -luuid  -ladvapi32 -lwsock32 -lopengl32"
   export INCS="-I. -I..\\wxui -I ..\\cpu68k -I ..\include -I `cygpath -wp $WXDEV/Include` -I `cygpath -wp $WXDEV/include/common`"
   export OPTS="-D__WXMSW__ -O3 -ffast-math -fomit-frame-pointer -march=$ARCHITECTURE -malign-double -falign-loops=5 -falign-jumps=5 -falign-functions=5 -I ..\cpu68k -I ..\\include -I . -I.. -I `cygpath -wp $WXDEV/include` -I `cygpath -wp $MING/include` -I `cygpath -wp $WXDEV/Include` -I `cygpath -wp $WXDEV/include/common`"

   export RCINCS="--include-dir=`cygpath -wp ${WXDEV}/include/common` "
   export DEFINES="-D__WXMSW__ -D__GNUWIN32__ -D__WIN95__ -D __WIN32__"
   export CXXINCS="-I. -I ..\include -I ..\cpu68k -I ..\\wxui -I `cygpath -wp ${MINGW}/include`  -I `cygpath -wp ${MINGCPP}/backward`  -I `cygpath -wp ${MINGCPP}/mingw32`  -I `cygpath -wp ${MINGCPP}`  -I `cygpath -wp ${WXDEV}/include`  -I `cygpath -wp ${WXDEV}/`  -I `cygpath -wp ${WXDEV}/include/common/wx/msw`  -I `cygpath -wp ${WXDEV}/include/common/wx/generic`  -I `cygpath -wp ${WXDEV}/include/common/wx/fl`  -I `cygpath -wp ${WXDEV}/include/common/wx/gizmos`  -I `cygpath -wp ${WXDEV}/include/common/wx/html`  -I `cygpath -wp ${WXDEV}/include/common/wx/mmedia`  -I `cygpath -wp ${WXDEV}/include/common/wx/net`  -I `cygpath -wp ${WXDEV}/include/common/wx/ogl`  -I `cygpath -wp ${WXDEV}/include/common/wx/plot`  -I `cygpath -wp ${WXDEV}/include/common/wx/protocol`  -I `cygpath -wp ${WXDEV}/include/common/wx/stc`  -I `cygpath -wp ${WXDEV}/include/common/wx/svg`  -I `cygpath -wp ${WXDEV}/include/common/wx/xml`  -I `cygpath -wp ${WXDEV}/include/common/wx/xrc`  -I `cygpath -wp ${WXDEV}/include/common/wx`  -I `cygpath -wp ${WXDEV}/include/common`"
   export CXXFLAGS="${CXXINCS} ${DEFINES}"
   export CFLAGS="${INCS} ${DEFINES} -fno-exceptions -fno-pcc-struct-return -fstrict-aliasing -Wall -Wno-format -Wno-unused -D__WXMSW__ -D__GNUWIN32__ -D__WIN95__   -fexpensive-optimizations -O3"
   export GPROF=gprof.exe
   export RM="rm -f"
   export LINK=g++.exe
   export CC=gcc.exe
   export CPP=g++.exe
   export CXX=g++.exe
   #export CFLAGS=$OPTS
   LINKOPTS="-static $LIBS"

   if [ -z "`gcc.exe --version | grep -i ming`" ]
   then
     echo The gcc.exe compiler does not seem to be the mingw version.
     exit 1
   fi
   if [ -z "`g++.exe --version | grep -i ming`" ]
   then
     echo The g++.exe compiler does not seem to be the mingw version.
     exit 1
   fi

 else

  echo "Could not find the wxDev C++ 6.10 Environment.  Please download and install it"
  echo "from http://wxdsgn.sourceforge.net/  and install it in c:\\wxDev-Cpp"
  echo ""
  echo "If you did install it, perhaps it is the wrong version?"

  exit 1
 fi
else

WXVER=0
case "`wx-config --version`" in
  2.9*|3*)
        echo WARNING: wxWidgets versions higher than 2.8 have not been tested.
        echo It might work if they are compiled with backwards compatibility.
        ;;
  2.7*|2.8*) ;;
  *)    echo Could not find wxWidgets 2.7.2 or higher.
        echo Please install it and ensure that wx-config is in the path
        exit 1
  ;;
esac


fi

if [ -z "`which pngtopnm`" ]
then
  echo Could not find pngtopnm which is part of the netpbm package.
  echo this program is required.  Please install the netpbm package,
  echo then run this script again.
  exit 1
fi

#$CYGWIN is pre-set.  Cache $DARWIN so we don't have to call uname over and over.
[ "`uname`" == "Darwin" ] && DARWIN="Darwin"


if [ -z "$PREFIX" ]
then
  if [ -n "$DARWIN" ];
  then
    PREFIX="/Applications"
    PREFIXLIB="/Library"
  else
      if [ -n "$CYGWIN" ]
      then
         PREFIX="/cygdrive/c/Program Files/Sunder.NET/LisaEm"
         PREFIXLIB="/cygdrive/c/Program Files/Sunder.NET/LisaEm"
      else
         PREFIX="/usr/local/bin"
         PREFIXLIB="/usr/local/share/"
      fi
  fi
fi



##############################################################################
#To compile or not to compile.
#
#if the .o file exists, compare it to the .c
#
# $1 - source file.
# $2 - object file.
#
# if the object file does not exist, it will return true.
# if the object is older than the source, it will return true.
##############################################################################

function NEEDED()
{
 if [ -f $2 ]
 then
   [ "`ls -tr $2 $1 2>/dev/null| tail -1`" == "$1" ] && return 0
   return 1
 fi
 return 0
}


echo
echo '----------------------------------------------------------------'
echo '     Apple Lisa 2 Emulator   -    Unified Build Script'
echo '                   http://lisaem.sunder.net'
echo '  Copyright (C) 2007 Ray A. Arachelian, All Rights Reserved'
echo 'Released under the terms of the GNU General Public License 2.0'
echo '----------------------------------------------------------------'

# Check our directories
ERROR=""
for i in lisa wxui include generator cpu68k
do
 [ -d "$i" ] || ERROR="$ERROR $i"
done

if [ -n "$ERROR" ]
then
 echo I could not find one or more of my directories:
 echo $ERROR
 echo Please make sure that you are running this script while inside
 echo the top lisaem directory, and that your download and extraction
 echo of the source code was complete.
 exit 1
fi


# Parse command line options if any, overriding defaults.

for i in $@
do

 case "$i" in
  clean)
        echo Removing binaries

        cd ./lisa
        /bin/rm -f *.a *.o *.exe *.bmp *.wav.c lisaem lisafsh-tool lisaem_private.res floppy?.xpm lisaface?.xpm power_off.xpm power_on.xpm
        /bin/rm -rf LisaEm.app
        cd ../generator
        /bin/rm -f *.a *.o
        cd ../wxui
        /bin/rm -f *.a *.o
        cd ../cpu68k
        /bin/rm -f *.a *.o *.exe def68k gen68k cpu68k-?.c
        cd ..

        echo clean done.
        #if we said clean install or clean build, then do not quit
        Z="`echo $@ | grep -i install``echo $@ | grep -i build`"
        [ -z "$Z" ] && exit 0
  ;;
 build*)    echo ;;    #default - nothing to do here, this is the default.
 install)   echo install command has not yet been created
            INSTALL=1;
            ;;

 uninstall)
           if [ -n "$DARWIN" ];
           then
             echo Uninstall commands not yet implemented.
             exit 1
           fi

           if [ -n "$CYGWIN" ];
           then
              [ -n "$PREFIX" ]    && echo Deleting $PREFIX    && rm -rf $PREFIX
              [ -n "$PREFIXLIB" ] && echo Deleting $PREFIXLIB && rm -rf $PREFIXLIB
              exit 0
           fi

           #Linux, etc.

           #PREFIX="/usr/local/bin"
           #PREFIXLIB="/usr/local/share/"

           echo Uninstalling from $PREFIX and $PREFIXLIB
           rm -rf $PREFIXLIB/LisaEm/
           rm -f  $PREFIX/lisaem
           rm -f  $PREFIX/lisafsh-tool

           exit 0

    ;;

 --without-debug)     WITHDEBUG=""               ;;
 --with-debug)        WITHDEBUG="$WITHDEBUG -g"  ;;
 --with-profile)      WITHDENUG="$WITHDEBUG -p"  ;;
 --with-static)       STATIC="-static"           ;;
 --without-static)    STATIC=""                  ;;
 --without-optimize)  WITHOPTIMIZE=""            ;;
 --with-tracelog)     WITHTRACE="-DDEBUG"        ;;
 *)                   UNKNOWNOPT="$UNKNOWNOPT $i";;
 esac

done


if [ -n "$UNKNOWNOPT" ]
then
 echo
 echo "Unknown options $UNKNOWNOPT"
 cat <<ENDHELP

Commands:
 clean               Removes all compiled objects, libs, executables
 build               Compiles lisaem (default)
 install             Not yet implemented on all platforms
 uninstall           Not yet implemented on all platforms

Options:
--without-debug      Disables debug and profiling
--with-debug         Enables symbol compilation
--with-static        Enables a static compile
--without-static     Enables shared library compile (not recommended)
--without-optimize   Disables optimizations

Environment Variables you can pass:

CC                   Path to C Compiler
CPP                  Path to C++ Compiler
WXDEV                Cygwin Path to wxDev-CPP 6.10 (win32 only)
PREFIX               Installation directory

ENDHELP
exit 1

#future help additions
# install             install to $PREFIX
# uninstall           uninstall lisaem and files from $PREFIX

fi

[ -n "${WITHDEBUG}${WITHTRACE}" ] && if [ -n "$INSTALL" ];
then
   echo "Warning, will not install since debug/profile/trace options are enabled"
   echo "as Install command is only for production builds."
   INSTALL=""
fi


if NEEDED include/vars.h lisa/unvars.c
then
cd ./lisa

echo Creating unvars.c
cat <<END1 >unvars.c
/**************************************************************************************\
*                             Apple Lisa 2 Emulator                                    *
*                                                                                      *
*                  Copyright (C) MMVI Ray A. Arachelian                                *
*                            All Rights Reserved                                       *
*                                                                                      *
*                        Reset Global Variables .c file                                *
*                                                                                      *
*            This is a stub file - actual variables are in vars.h.                     *
*        (this is autogenerated by the build script. do not hand edit.)                *
\**************************************************************************************/

#define IN_UNVARS_C 1
// include all the includes we'll (might) need (and want)
#include "vars.h"

#define REASSIGN(  a , b  , c  )  {(b) = (c);}

void unvars(void)
{

#undef GLOBAL
#undef AGLOBAL
#undef ACGLOBAL

END1
egrep 'GLOBAL\(' ../include/vars.h | grep -v '*' | grep -v AGLOBAL | grep -v '#define' | grep -v FILE | grep -v get_exs | grep -v '\[' | sed 's/GLOBAL/REASSIGN/g'  >>unvars.c

echo "}" >> unvars.c

cd ..
fi

#if we're not on Cygwin, then setup the defaults, unless
#they were defined already from the parent shell.
if [ -z "$CYGWIN" ]
then

 WXCONFIGFLAGS=`wx-config  --cppflags`
 CFLAGS="-I. -I../include -I../cpu68k -I../wxui $WXCONFIGFLAGS $WITHOPTIMIZE $WITHDEBUG"
 CXXFLAGS="-I. -I../include -I../cpu68k -I../wxui $WXCONFIGFLAGS $WITHOPTIMIZE $WITHDEBUG"
 LINKOPTS="`wx-config $STATIC  --libs --linkdeps --cppflags`"
 [ -z "$CC" ] && CC=gcc
 [ -z "$CXX" ] && CXX=g++

fi

###########################################################################
echo Building...
echo
echo ./cpu68k
cd cpu68k

DEPS=0
[ "$DEPS" -eq 0 ] && if NEEDED def68k-iibs.h          lib68k.a; then  DEPS=1;fi
[ "$DEPS" -eq 0 ] && if NEEDED def68k.def             lib68k.a; then  DEPS=1;fi
[ "$DEPS" -eq 0 ] && if NEEDED gen68k.c               lib68k.a; then  DEPS=1;fi
[ "$DEPS" -eq 0 ] && if NEEDED tab68k.c               lib68k.a; then  DEPS=1;fi
[ "$DEPS" -eq 0 ] && if NEEDED def68k.c               lib68k.a; then  DEPS=1;fi
[ "$DEPS" -eq 0 ] && if NEEDED ../include/vars.h      lib68k.a; then  DEPS=1;fi
[ "$DEPS" -eq 0 ] && if NEEDED ../include/generator.h lib68k.a; then  DEPS=1;fi


if [ "$DEPS" -gt 0 ]
then

for src in tab68k.c def68k.c; do
    echo Compiling $src...
    $CC $CFLAGS -c $src || exit 1
done

$CC -o def68k tab68k.o def68k.o

./def68k || exit 1

echo Compiling gen68k.c...
$CC $CFLAGS -c gen68k.c || exit 1

$CC -o gen68k tab68k.o gen68k.o
./gen68k || exit 1

for src in cpu68k-[0-9a-f].c; do
    echo Compiling $src...
    $CC $CFLAGS  -c $src || exit 1
done

rm -f lib68k.a

ar cru lib68k.a cpu68k-[0-9a-f].o || exit 1
ranlib lib68k.a || exit 1

fi

###########################################################################

#Build libgenerator.a
echo ./generator
cd ../generator

DEPS=0
[ "$DEPS" -eq 0 ] && if  NEEDED ../include/vars.h  libgenerator.a;then  DEPS=1; fi
[ "$DEPS" -eq 0 ] && if  NEEDED cpu68k.c           libgenerator.a;then  DEPS=1; fi
[ "$DEPS" -eq 0 ] && if  NEEDED reg68k.c           libgenerator.a;then  DEPS=1; fi
[ "$DEPS" -eq 0 ] && if  NEEDED diss68k.c          libgenerator.a;then  DEPS=1; fi
[ "$DEPS" -eq 0 ] && if  NEEDED ui_log.c           libgenerator.a;then  DEPS=1; fi

if [ "$DEPS" -gt 0 ]
then
for src in cpu68k reg68k ui_log diss68k; do
    #only compile what we need, unlike ./cpu68k this is less sensitive
    if NEEDED ${src}.c ${src}.o
    then
      echo Compiling ${src}.c...
      $CC $CFLAGS -c ${src}.c || exit 1
    fi
done
ar cru libgenerator.a cpu68k.o reg68k.o ui_log.o diss68k.o || exit 1
ranlib libgenerator.a || exit 1

fi
###########################################################################


echo ./lisa
cd ../lisa

# Compile C
for i in libdc42 floppy profile unvars vars glue fliflo_queue cops zilog8530 via6522 irq mmu rom memory #imagewriter
do

	LIST="$LIST $i.o"

  DEPS=0
  [ "$DEPS" -eq 0 ] && if NEEDED ../include/vars.h  ${i}.o;then DEPS=1; fi
  [ "$DEPS" -eq 0 ] && if NEEDED ${i}.c             ${i}.o;then DEPS=1; fi
  if [ "$DEPS" -gt 0 ]
  then
     echo Compiling ${i}.c...
     $CC -W -Wall -Wstrict-prototypes -Wno-format -Wno-unused  $CFLAGS -c ${i}.c -o ${i}.o || exit 1
  fi
done


DEPS=0
[ "$DEPS" -eq 0 ] && if NEEDED lisafsh-tool.c lisafsh-tool;then  DEPS=1; fi
[ "$DEPS" -eq 0 ] && if NEEDED libdc42.o      lisafsh-tool;then  DEPS=1; fi
if [ "$DEPS" -gt 0 ]
then
  echo Linking lisafsh-tool...
  $CC $CFLAGS -o lisafsh-tool lisafsh-tool.c libdc42.o || exit 1
  if [ -z "$WITHDEBUG" ]
  then
    strip lisafsh-tool
    if [ -n "`which upx 2>/dev/null`"     ]; then upx --best lisafsh-tool${EXT} ; fi
    if [ -n "`which md5sum  2>/dev/null`" ]; then md5sum     lisafsh-tool${EXT} | tee lisafsh-tool.md5; fi
    if [ -n "`which sha1sum 2>/dev/null`" ]; then sha1sum    lisafsh-tool${EXT} | tee lisafsh-tool.sha1; fi

   fi
fi

#vars.c must be linked before any C++ or else there will be
#linking conflicts due to extern "C" re-defs from vars.h!

for i in lisaem_wx imagewriter-wx
do
  LIST="$LIST ${i}.o"
  if NEEDED ${i}.cpp ${i}.o
  then
     echo Compiling ${i}.cpp...
     $CXX -W -Wall $CFLAGS -c ${i}.cpp -o ${i}.o || exit 1
  fi
done



VSTATIC=0

# If we're on the evilOS, we use COFF resources, and the images are BMP's

if [ -n "$CYGWIN" ]
then
 for i in floppy0 floppy1 floppy2 floppy3 floppyN lisaface0 lisaface1 lisaface2 lisaface3 power_off power_on
 do
   if NEEDED ${i}.png ${i}.bmp
   then
        echo Generating ${i}.bmp...
        pngtopnm  < ${i}.png | ppmtobmp -windows -bpp=8 -quiet > ${i}.bmp
        VSTATIC=1
   fi

 done
fi

# If we're on Mac OS X, we use the Resources directory rather than embedded
# XPMs, so we can skip the XPM generation.  On windows, it's BMP's, so this is only for X11/GTK builds

if [ -z "${CYGWIN}${DARWIN}" ]
then
    for i in floppy0 floppy1 floppy2 floppy3 floppyN lisaface0 lisaface1 lisaface2 lisaface3 power_off power_on
    do
       if NEEDED ${i}.png ${i}.xpm
       then
           echo Generating ${i}.xpm...
           pngtopnm  < ${i}.png | ppmtoxpm -quiet -name ${i}_xpm | sed -e's/^static //' > ${i}.xpm
       fi
    done
fi

# should remove this for windows and allow them in as resources, and files for all others.
#
#    if NEEDED genchararray.c genchararray
#    then
#     echo Compiling genchararray.c...
#     $CC genchararray.c -o genchararray || exit 1
#    fi
#
#    for i in floppy_eject floppy_insert_sound floppy_motor1 floppy_motor2 lisa_power_switch01 lisa_power_switch02 poweroffclk
#    do
#    if NEEDED ${i}.wav ${i}.wav.c
#    then
#        echo Generating `pwd`/${i}.wav.c...
#        ./genchararray <${i}.wav ${i} >${i}.wav.c
#        VSTATIC=1
#    fi
#    done


if [ "$VSTATIC" -gt 0 ]; then DEPS=1; else DEPS=0; fi
[ "$DEPS" -eq 0 ] && if NEEDED lisaem_static_resources.cpp lisaem_static_resources.o; then DEPS=1; fi

if [ "$DEPS" -gt 0 ]
then
  echo Compiling lisaem_static_resources.cpp...
  $CXX $CFLAGS -c lisaem_static_resources.cpp -o lisaem_static_resources.o || exit 1
fi
LIST="$LIST lisaem_static_resources.o"

if [ -n "$CYGWIN" ]
then
 [ "$VSTATIC" -eq 0 ] && if NEEDED lisaem_private.rc lisaem_private.res; then VSTATIC=1; fi
 if [ "$VSTATIC" -gt 0 ]
 then
    echo win32 resources
    #echo windres.exe --input-format=rc $RCINCS  -O coff lisaem_private.rc lisaem_private.res
         windres.exe --input-format=rc $RCINCS  -O coff lisaem_private.rc lisaem_private.res || exit 1
 fi
 LIST="$LIST lisaem_private.res"
fi



rm -f lisaem lisaem.exe

# If we're on Darwin, we know the system libraries are there and we
# don't have to link statically against them. On Linux, link statically
# for the binary release so we don't have to worry about cross-distribution
# library version issues.
# Also, Apple's GCC doesn't support --static, so disable it there.
if [ -n "$DARWIN" ]
then
    SYSLIBS=
    GCCSTATIC=
else
    [ -z "$CYGWIN" ] && \
    SYSLIBS="/usr/lib/libX*.a  /usr/lib/libcairo*.a /usr/lib/libpango*.a
             /usr/lib/libfreetype.a /usr/lib/libexpat.a /usr/X11R6/lib/libICE.a
             /usr/lib/librt.a /usr/lib/libXrender.a /usr/lib/libX11.a
             /usr/lib/libfontconfig.a"

    GCCSTATIC=$STATIC
fi

#vars.c must be linked in before any C++ source code or else there will be linking conflicts!

cd ../wxui
echo ./wxui


# Compile C++
for i in LisaConfig LisaConfigFrame
do
  if [ -z "$CYGWIN" ]; then LIST="$LIST ../wxui/${i}.o"
  else                      LIST="$LIST ..\\wxui\\${i}.o"
  fi

  DEPS=0;
  [ "$DEPS" -eq 0 ] && if NEEDED ${i}.cpp ${i}.o;   then DEPS=1; fi
  [ "$DEPS" -eq 0 ] && if NEEDED ${i}.h   ${i}.cpp; then DEPS=1; fi

  if [ "$DEPS" -gt 0 ]
  then
     echo Compiling ${i}.cpp...
     $CXX -W -Wall $CXXFLAGS -c ${i}.cpp -o ${i}.o || exit 1
  fi
done
cd ../lisa

echo Linking.
#echo
#echo $CXX $GCCSTATIC  $WITHDEBUG -o lisaem  $LIST ../generator/libgenerator.a ../cpu68k/lib68k.a $LINKOPTS $SYSLIBS
$CXX $GCCSTATIC $WITHTRACE $WITHDEBUG -o lisaem  $LIST ../generator/libgenerator.a ../cpu68k/lib68k.a $LINKOPTS $SYSLIBS 2>&1  | head -20


if [ -f lisaem ]
then

du -sh lisaem

if [ -n "$DARWIN" ]
then

    mkdir -pm775 LisaEm.app/Contents/MacOS
    mkdir -pm775 LisaEm.app/Contents/Resources
    cp Info.plist LisaEm.app/Contents/
    echo -n 'APPL????' > LisaEm.app/Contents/PkgInfo
    cp LisaEm.icns floppy0.png floppy1.png floppy2.png LisaEm.app/Contents/Resources/
    cp floppy3.png floppyN.png lisaface0.png LisaEm.app/Contents/Resources/
    cp lisaface1.png lisaface2.png lisaface3.png LisaEm.app/Contents/Resources/
    cp power_off.png power_on.png LisaEm.app/Contents/Resources/
    cp floppy_eject.wav floppy_insert_sound.wav LisaEm.app/Contents/Resources/
    cp floppy_motor1.wav floppy_motor2.wav LisaEm.app/Contents/Resources/
    cp lisa_power_switch01.wav lisa_power_switch02.wav LisaEm.app/Contents/Resources/
    cp poweroffclk.wav LisaEm.app/Contents/Resources/

    [ -n "$WITHDEBUG" ] && strip lisaem
    mv lisaem LisaEm.app/Contents/MacOS/LisaEm
    [ -n "$WITHDEBUG" ] && echo run >gdb-run && gdb ./LisaEm.app/Contents/MacOS/LisaEm
    #if we turned on profiling, process the results
    if [ `echo "$WITHDEBUG" | grep 'p' >/dev/null 2>/dev/null` ]
    then
      gprof ../lisa/lisaem >lisaem-gprof-out
      echo lisaem-gprof-out created.
    fi

    exit $?

fi



if [ -z "$WITHDEBUG" ]
then

  echo "Freshly compiled `du -sh lisaem`"
  strip lisaem${EXT}
  echo "Stripped `du -sh lisaem`"

  # compress it if upx exists.
  if [ -n "`which upx 2>/dev/null`" ]
  then
    upx --best lisaem${EXT}
    echo "upxed `du -sh lisaem`"
  fi
  if [ -n "`which md5sum  2>/dev/null`" ]; then md5sum  lisaem${EXT} | tee lisaem.md5; fi
  if [ -n "`which sha1sum 2>/dev/null`" ]; then sha1sum lisaem${EXT} | tee lisaem.sha1; fi


  ## Install ###################################################
  if [ -n "$INSTALL" ]
  then
    echo Install command not yet implemented on all platforms.


    if [ -n "$DARWIN" ]
    then
      exit 1
    fi

    if [ -n "$CYGWIN" ]
    then
         #PREFIX   ="/cygdrive/c/Program Files/Sunder.NET/LisaEm"
         #PREFIXLIB="/cygdrive/c/Program Files/Sunder.NET/LisaEm"
         echo Installing resources in     $PREFIX
         echo Installing lisaem binary in $PREFIX/lisaem
         mkdir -p $PREFIX
         cp *.wav $PREFIX
         cp lisaem.exe $PREFIXLIB
      exit 1
    fi

    if [ -z "$CYGWIN" ]
    then

      #   PREFIX="/usr/local/bin"
      #   PREFIXLIB="/usr/local/share/"

      echo Installing resources in     $PREFIXLIB/LisaEm
      echo Installing lisaem binary in $PREFIX/lisaem

      mkdir -pm755 $PREFIXLIB/LisaEm/ $PREFIX
      cp *.wav $PREFIXLIB/LisaEm/
      cp lisaem $PREFIX
      echo Done.
      exit 0

    fi

  fi     # end of  INSTALL
  ##########################################################

else

 echo run >gdb-run
 if [ -z "$CYGWIN" ]
 then
    gdb ../lisa/lisaem  -x gdb-run
 else
    gdb.exe ..\\lisa\\lisaem  -x gdb-run
 fi


 if [ -n "`echo -- $WITHDEBUG | grep p`" ]
 then
   pwd
   [ -n "$CYGWIN" ] && gprof.exe ..\\lisa\\lisaem.exe >lisaem-gprof-out.txt
   [ -z "$CYGWIN" ] && gprof ./lisa/lisaem >lisaem-gprof-out.txt
 fi

fi


fi
