#!/bin/bash -e

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

### hardcode PKG and patches tarball
export PKG=lzma-4.43
DEBIAN_PATCHES=${PKG}-debian-12

# displays error message and exits
error() {
        case $? in
                0) local errorcode=1 ;;
                *) local errorcode=$? ;;
        esac

        echo -e "\e[1;31m*** ERROR:\e[0;0m ${1:-no error message provided}";
        exit ${errorcode};
}

# displays information message
inform() {
        echo -e "\e[1;32m*** Info:\e[0;0m ${1}";
}

# displays warning message only
warning() {
        echo -e "\e[1;33m*** Warning:\e[0;0m ${1}";
}

# displays command to stdout before execution
verbose() {
        echo "${@}"
        "${@}"
        return $?
}
export -f verbose warning inform error


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" ;;
    * ) error "Bad src directory specified: $1" ;;
    esac
  else
    error "Bad src directory specified: $1"
  fi
else
  savedir="$PWD"
  srcdir="$PWD"
fi
if [ -n "$unpack" ] ; then
  case "$unpack" in
  tbz ) inform "unpacking $1" ; tar xjf $1 ;;
  tgz ) inform "unpacking $1" ; tar xzf $1 ;;
  zip ) inform "unpacking $1" ; unzip -q $1 ;;
  * ) error "unknown pack format" ;;
  esac
  if [ ! -d "$srcdir" ]; then
    echo "src package $1 does not unpack into assumed srcdir $srcdir"
    exit 1
  fi

  inform "unpacking debian patches" ; tar xjf ${DEBIAN_PATCHES}.tar.bz2


  cd ${srcdir}
  inform "removing pre-compiled native win32 lzma.exe from src directory"
  rm -f lzma.exe

  for fn in 06_sqlzma.diff    05_sqlzma_backport.diff 05_fix_path.diff \
            04_sqlzma_h.patch 03_cflags_makefile.diff 03_lzma_manpage.diff \
            01_large_files.diff  02_lzmp.patch+without-makefile.gcc.patch \
            0001-Makefile-new-file.patch
  do
    inform "applying debian patch: $fn"
    if ! patch -p1 < ../${DEBIAN_PATCHES}/${fn} ; then
      error "unable to apply patch!"
    fi
  done

  if [ -f ../${DEBIAN_PATCHES}/0002-makefile.gcc.patched ]
  then
    cp -v ../${DEBIAN_PATCHES}/0002-makefile.gcc.patched \
          C/7zip/Compress/LZMA_Alone/makefile.gcc
  else
    error "could not located pre-patched makefile for LMZM_Alone"
  fi
  cd ${savedir}

  if [ -e ${PKG}.patch ] ; then
    cd ${srcdir}
    inform "applying msys path: ${PKG}.patch"
    if ! patch -p2 < ${savedir}/${PKG}.patch ; then
      error "unable to apply patch!"
    fi
    cd ${savedir}
  fi
  srcdir=`cd $srcdir; pwd`
fi


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

opt_flags="-fnative-struct -O3 -s -mcpu=pentium"
export CFLAGS=${CFLAGS:-"${opt_flags}"};
export CPPFLAGS="${CPPFLAGS} -D__CYGWIN__" 
export CXXFLAGS=${CXXFLAGS:-"${opt_flags}"};
export F77FLAGS=${F77FLAGS:-"${opt_flags}"};
export GCJFLAGS=${GCJFLAGS:-"${opt_flags}"};
export LDFLAGS="${LDFLAGS} -Wl,--enable-auto-import";
export PATH=`pwd`:/msys/bin:/bin:/usr/local/bin:/mingw/bin

confargs="--prefix=/usr" 

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

cd ${builddir}
inform "creating link tree in _build"
lndir -silent ../${PKG} .

cd ${builddir}
make
make install DESTDIR=${instdir}	
verbose /usr/bin/install -d ${instdir}/usr/share/man/man1
verbose /usr/bin/install -d ${instdir}/usr/share/doc/${PKG}
verbose /usr/bin/install -m 644 ${srcdir}/debian/lzma.1 ${instdir}/usr/share/man/man1/
verbose /usr/bin/install -m 644 ${srcdir}/lzma.txt ${instdir}/usr/share/doc/${PKG}/
verbose /usr/bin/install -m 644 ${srcdir}/history.txt ${instdir}/usr/share/doc/${PKG}/

cd ${instdir}/usr
tar cvzf ${abovedir}/${PKG}-MSYS-1.0.11-2-bin.tar.gz *


cd ${abovedir}
tar cvzf ./${PKG}-MSYS-1.0.11-2-src.tar.gz \
  ${PKG}.tar.bz2 \
  ${DEBIAN_PATCHES}.tar.bz2 \
  ${PKG}.patch \
  msys-build-lzma

cd "$savedir"

