#!/bin/sh
#
# Modify a GHC binary distribution for the purposes of creating a
# mingw/win32 install tree.  The resulting tree is ready for packaging
# up in whatever form is convenient (MSI installer / tar bundle/ ..)
#
# To use:
#
#   $ cd <top of GHC build tree>
#   $ make binary-dist
#
# This script is called at the appropriate point during 'make binary-dist'.
# The result is a tarball at the top of your GHC build tree, named something
# like ghc-6.6.1-i386-unknown-mingw32.tar.bz2.
# 
# User tweakables 
#    Note: you normally don't need to set any of these, the script
#    will try to figure them out for itself.  If the heuristics don't
#    work for whatever reason, you can override them using environment
#    variables, e.g.
#      mingw_top=<whatever> make binary-dist
#
#    - mingw_top   -- location of mingw distribution tree (usually c:/mingw)
#    - perl_dir    -- location of non-cygwin perl.exe
#    - gcc_lib     -- c:/mingw/lib/gcc/mingw32/3.4.2, or equivalent
#    - gcc_libexec -- c:/mingw/libexec/gcc/mingw32/3.4.2, or equivalent
#

#Directory where a (cygwin-free) perl binary resides.
if [ "${perl_dir}" == "" ]; then
    for i in c:/ghc/*; do
       if [ -e $i/perl.exe ]; then
           perl_dir=$i
           echo "Found perl.exe in $i"
           break
       fi
    done
    if [ "${perl_dir}" == "" ]; then
        echo "Can't find perl.exe; please set \$perl_dir"
        exit 1
    fi
fi

if [ "${mingw_top}" == "" ]; then
    if [ -d c:/mingw ]; then
        mingw_top=c:/mingw
        echo "Found mingw in $mingw_top"
    else
        echo "Can't find mingw; please set \$mingw_top"
        exit 1
    fi
fi

# The gcc-lib directory of the mingw tree you want to
# include with the binary dist.
if [ "x${gcc_lib}" == "x" ]; then
    if [ -d "${mingw_top}/lib/gcc-lib/mingw32" ]; then
        mingw_gcc_lib=${mingw_top}/lib/gcc-lib/mingw32
    else
        mingw_gcc_lib=${mingw_top}/lib/gcc/mingw32
    fi
    for i in `ls -r ${mingw_gcc_lib}`; do
        if [ -d "${mingw_gcc_lib}/$i" ]; then
            gcc_lib=${mingw_gcc_lib}/$i
            echo "Found gcc lib in $gcc_lib"
            break
        fi
    done
    if [ "${gcc_lib}" == "" ]; then
        echo "Can't find gcc lib files; please set \$gcc_lib"
        exit 1
    fi
fi
    
# The gcc-lib directory of the mingw tree you want to
# include with the binary dist.
if [ "x${gcc_libexec}" == "x" ]; then
    if [ -d "${mingw_top}/libexec/gcc-lib/mingw32" ]; then
        mingw_gcc_libexec=${mingw_top}/libexec/gcc-lib/mingw32
    else
        mingw_gcc_libexec=${mingw_top}/libexec/gcc/mingw32
    fi
    for i in `ls -r ${mingw_gcc_libexec}`; do
        if [ -d "${mingw_gcc_libexec}/$i" ]; then
            gcc_libexec=${mingw_gcc_libexec}/$i
            echo "Found gcc libexec in $gcc_libexec"
            break
        fi
    done
    if [ "${gcc_libexec}" == "" ]; then
        echo "Can't find gcc libexec files; please set \$gcc_libexec"
        exit 1
    fi
fi
    
#
# The mingw include, lib, and bin directories; all derived
# from ${mingw_top}.
#
if [ "x${mingw_include}" == "x" ]; then
  export mingw_include=$mingw_top/include
fi
if [ "x${mingw_lib}" == "x" ]; then
  export mingw_lib=$mingw_top/lib
fi
if [ "x${mingw_bin}" == "x" ]; then
  export mingw_bin=$mingw_top/bin
fi

# Check that we're in an OK place before starting to re-org
# the directory tree..
if ! [ -d bin ] ; then
  echo "Doesn't look as if I'm in the toplevel directory of a mingw tree"
  echo "Usage: cd ghc-<version> ; ../distrib/prep-bin-dist-mingw"
  exit 1;
fi;

if [ "`$mingw_bin/windres.exe --version | sed '1p;d'`" = "GNU windres 2.17.50 20060824" ]
then
  echo "Bad windres version for $mingw_bin/windres.exe."
  echo "2.15.91 and 2.18.50 are known to work."
  exit 1
fi

echo "Removing configure script files...not needed"
rm -f config.guess config.sub configure configure.ac mkdirhier
rm -f Makefile-bin.in Makefile.in aclocal.m4 install-sh
rm -rf autom4te.cache

echo "strip ghc"
strip bin/ghc.exe

echo "create gcc-lib/"
#
# A bunch of stuff gets lumped into gcc-lib:
#
#  - the gcc-lib/ + gcc-lib/include of the gcc you
#    intend to ship (normally located as 
#     lib/gcc-lib/mingw/<gcc version>/ in your mingw tree.)
#  - the contents of mingw/lib/ 
#  - ld.exe, as.exe, dlltool.exe, dllwrap.exe from mingw/bin
#    to gcc-lib/
#  - ar.exe from mingw/bin to bin/
#
mkdir gcc-lib
mkdir gcc-lib/include
cp $gcc_lib/* gcc-lib/
cp $gcc_libexec/* gcc-lib/
cp $gcc_lib/include/* gcc-lib/include/
cp $mingw_lib/* gcc-lib/
cp $mingw_bin/as.exe gcc-lib/
cp $mingw_bin/ld.exe gcc-lib/
cp $mingw_bin/ar.exe bin/
cp $mingw_bin/windres.exe bin/
# Note: later versions of dlltool.exe depend on a bfd helper DLL.
cp $mingw_bin/dllwrap.exe gcc-lib/
cp $mingw_bin/dlltool.exe gcc-lib/
# Remove worthy, but unused tools
rm gcc-lib/f771.exe || echo "good - f771.exe not found"
rm gcc-lib/gnat1.exe || echo "good - gnat1.exe not found"
rm gcc-lib/jc1.exe || echo "good - jc1.exe not found"
rm gcc-lib/libgcj* || echo "good - libgcj libs not found"
rm gcc-lib/jvgenmain.exe || echo "good - jvgenmain.exe not found"

echo "extra header files inside of include/"
#
# contains mingw/include
mkdir include/mingw
cp -Rf $mingw_include/* include/mingw
#
# g++-3/ subdir causes problems with installer tool (+ being a 
# troublesome character); leave out for now.
#rm -rf include/mingw/g++-3/ || echo "g++-3/ not there"
#rm -rf include/mingw/c++/ || echo "c++/ not there"
rm -rf include/mingw/ddk/ || echo "ddk/ not there"
rm -rf include/mingw/gnu/ || echo "gnu/ not there"
rm -rf include/mingw/javax/ || echo "javax/ not there"
rm -rf include/mingw/java/ || echo "java/ not there"
rm -rf include/mingw/gcj/ || echo "gcj/ not there"

echo "add gcc"
cp ${mingw_bin}/gcc.exe .
#cp ${mingw_bin}/gcc-2.exe gcc.exe

echo "copy in perl too"
cp ${perl_dir}/perl.exe .
cp ${perl_dir}/perl56.dll .

# For reasons unknown, duplicate copies of misc package files in share/
# (leave them be for now.)

echo "formatting documentation"
unix2dos < README > README.txt
rm README

# Leave out pdf users_guide documentation for now; problematic to build with the versions
# of 'xsltproc' and 'fop' I've been able to lay my hands on.
#cp ../ghc/docs/users_guide/users_guide.pdf doc/ || 
#  (make -C ../ghc/docs/users_guide/ pdf ; cp ../ghc/docs/users_guide/users_guide.pdf doc/) || 
#  echo "No User Guide PDF doc found"
#cp ../hslibs/doc/hslibs.pdf doc/ || 
#  (make -C ../hslibs/doc/ pdf ; cp ../hslibs/doc/hslibs.pdf doc/) ||
#  echo "No HSLIBS PDF doc found"
