#!/bin/bash -e

# NOTE: should be invoked as either:
#    ./msys-build-cpmake  make-VER.tar.bz2
#    which will unpack the "pristine" source distribution, patch it, 
#    and begin the build,
# OR, as
#    ../msys-build-cpmake
#    where the current working directory is the unpacked and patched
#    source distribution.

func_fatal_error ()
{
  printf "$0: *** fatal error: $1\n" 1>&2
  if [ -n "$2" ]; then
    printf "$2\n" 1>&2
  fi
  printf "$0: *** Stop.\n" 2>&1
  exit 1
}


# Check availability of the CVS program.
(cvs -v) >/dev/null 2>/dev/null || \
  func_fatal_error "cvs program not found!" "\
    I need to run autopoint (from gettext), which requires cvs.\n\
    However, I can't find it. Install cvs.exe temporarily in \n\
    your PATH, then try again."


if [ "x" != "x$1" ]; then
  if [ -d "$1" ]; then
    savedir="$PWD"
    cd "$1"
    srcdir="$PWD"
  elif [ -f "$1" ]; then
    case "$1" in
    *.tar.bz2 ) srcdir=`echo $1 | sed -e 's/\.tar\.bz2$//'`
                unpack=tbz
                savedir="$PWD" ;;
    *.tar.gz  ) srcdir=`echo $1 | sed -e 's/\.tar\.gz$//'`
                unpack=tgz
                savedir="$PWD" ;;
    *.zip     ) srcdir=`echo $1 | sed -e 's/\.zip$//'`
                unpack=zip
                savedir="$PWD" ;;
    * ) echo "Bad src directory specified: $1" 
        exit 1 ;;
    esac
  else
    echo "Bad src directory specified: $1"
    exit 1
  fi
else
  savedir="$PWD"
  srcdir="$PWD"
fi

if [ -n "$unpack" ] ; then
  case "$unpack" in
  tbz ) tar xvjf $1 ;;
  tgz ) tar xvzf $1 ;;
  zip ) unzip $1 ;;
  * ) echo "unknown pack format"
      exit 1 ;;
  esac
  if [ ! -d "$srcdir" ]; then
    echo "src package $1 does not unpack into assumed srcdir $srcdir"
    exit 1
  fi

  # apply patches
  for fn in make-3.81-cygwin.patch make-3.81-dos-path.patch case-preserve.patch ${srcdir}.patch ; do
    if [ -e $fn ] ; then
      cd ${srcdir}
      if ! patch -p2 < ../$fn ; then
        echo "unable to apply patch!"
        exit 1
      fi
      cd ${savedir}
    fi
  done
  
  srcdir=`cd $srcdir; pwd`
fi


abovedir=`cd ${srcdir}/..; pwd`
export PKG=`basename $srcdir`
PREFIX=/usr

confargs="--prefix=/usr --disable-nls"

opt_flags="-O3 -s -mcpu=pentium"
export CFLAGS=${CFLAGS:-"-fnative-struct ${opt_flags}"};
export CPPFLAGS="${CPPFLAGS} -D__CYGWIN__";
export CXXFLAGS=${CXXFLAGS:-"-fnative-struct ${opt_flags}"};
export F77FLAGS=${F77FLAGS:-"-fnative-struct ${opt_flags}"};
export GCJFLAGS=${GCJFLAGS:-"-fnative-struct ${opt_flags}"};
export LDFLAGS="${LDFLAGS} -Wl,--enable-auto-import";


mkdir -p ${abovedir}/_build
builddir=${abovedir}/_build
mkdir -p ${abovedir}/_inst
instdir=${abovedir}/_inst

cd ${srcdir}
autoreconf -f -v -i

# we do NOT want the just-built make in our path on accident
export PATH=/msys/bin:/bin:/usr/local/bin:/mingw/bin
cd ${builddir}
${srcdir}/configure --srcdir=${srcdir} ${confargs} \
  CFLAGS="${CFLAGS}" \
  CPPFLAGS="${CPPFLAGS}" \
  CXXFLAGS="${CXXFLAGS}" \
  LDFLAGS="${LDFLAGS}"

make

### always!
### results: 10 Tests in 4 Categories Failed 
###          some of these are ok, having to do with the /usr == / issue
###          but others may be a problem.
make -k check || /bin/true

make install DESTDIR=${instdir}	
cat > ${instdir}${PREFIX}/share/man/man1/make.1 <<-"EOF"
	.TH make 1 MSYS "29 Sep 2007" "make man page"
	
	.SH NAME
	make
	.SH DESCRIPTION
	This is a case-preserving version of GNU make. While the
	win32 file systems encountered when using MSYS software
	are invariably case-INsensitive, some users prefer a 
	GNU `make' that itself is case-sensitive -- this
	version of make is a compromise which exploits the
	case-preserving aspect of win32 file systems. It considers
	*all* targets as fundamentally case-insensitive, but first
	attempts to resolve them as case-sensitive, falling back to the
	case-insensitive behaviour only if the case-sensitive match
	remains unresolved.
EOF
cd ${instdir}${PREFIX}
rm -f share/man/man1/make.1
rm -f share/info/*
rmdir share/info
#mv bin/make.exe bin/cpmake.exe

cd ${instdir}${PREFIX}
tar cvzf ${abovedir}/cp${PKG}-MSYS-1.0.11-1-bin.tar.gz *

cd ${abovedir}
tar cvzf ./cp${PKG}-MSYS-1.0.11-1-src.tar.gz \
  ${PKG}.tar.bz2 \
  ${PKG}.patch \
  make-3.81-cygwin.patch \
  make-3.81-dos-path.patch \
  case-preserve.patch \
  msys-build-cpmake

cd "$savedir"

