#!/bin/bash
# find out where the build script is located
tdir=`echo "$0" | sed 's%[\\/][^\\/][^\\/]*$%%'`
test "x$tdir" = "x$0" && tdir=.
scriptdir=`cd $tdir; pwd`
# find src directory.  
# If scriptdir ends in SPECS, then topdir is $scriptdir/.. 
# If scriptdir ends in CYGWIN-PATCHES, then topdir is $scriptdir/../..
# Otherwise, we assume that topdir = scriptdir
topdir1=`echo ${scriptdir} | sed 's%/SPECS$%%'`
topdir2=`echo ${scriptdir} | sed 's%/CYGWIN-PATCHES$%%'`
if [ "x$topdir1" != "x$scriptdir" ] ; then # SPECS
  topdir=`cd ${scriptdir}/..; pwd`
else
  if [ "x$topdir2" != "x$scriptdir" ] ; then # CYGWIN-PATCHES
    topdir=`cd ${scriptdir}/../..; pwd`
  else
    topdir=`cd ${scriptdir}; pwd`
  fi
fi

tscriptname=`basename $0 .sh`
export PKG=`echo $tscriptname | sed -e 's/\-[^\-]*\-[^\-]*$//'`
export VER=`echo $tscriptname | sed -e "s/${PKG}\-//" -e 's/\-[^\-]*$//'`
export REL=`echo $tscriptname | sed -e "s/${PKG}\-${VER}\-//"`
# Note that readline generates TWO different binary packages:
# 1) readline -- includes static libs, import libs, executables,
#                header files, documentation
# 2) libreadline5 -- includes only the DLLs
export FULLPKG=${PKG}-${VER}-${REL}
export FULLPKG2=libreadline6-${VER}-${REL}

# determine correct decompression option and tarball filename
if [ -e ${PKG}-${VER}.tar.gz ] ; then
  export opt_decomp=z
  export src_orig_pkg_ext=gz
elif [ -e ${PKG}-${VER}.tar.bz2 ] ; then
  export opt_decomp=j
  export src_orig_pkg_ext=bz2
fi

export src_orig_pkg_name=${PKG}-${VER}.tar.${src_orig_pkg_ext}
export src_pkg_name=${FULLPKG}-src.tar.bz2
export src_patch_name=${FULLPKG}.patch
export src_official_patches="readline50-001 \
readline50-002 \
readline50-003 \
readline50-004 \
readline50-005"
export bin_pkg_name=${FULLPKG}.tar.bz2
export bin_pkg_name2=${FULLPKG2}.tar.bz2

export src_orig_pkg=${topdir}/${src_orig_pkg_name}
export src_pkg=${topdir}/${src_pkg_name}
export src_patch=${topdir}/${src_patch_name}
export src_patch2=${topdir}/${src_patch_name2}
export bin_pkg=${topdir}/${bin_pkg_name}
export bin_pkg2=${topdir}/${bin_pkg_name2}
export srcdir=${topdir}/${PKG}-${VER}
export objdir=${srcdir}/.build
export instdir=${srcdir}/.inst
export srcinstdir=${srcdir}/.sinst
export checkfile=${topdir}/${FULLPKG}.check
# build on
build=i686-pc-cygwin
# run on
host=i686-pc-cygwin
# if this package creates binaries, they run on
target=i686-pc-cygwin
prefix=/usr
pnoslash=`echo $prefix | sed -e 's#^/##'`
sysconfdir=/etc
snoslash=`echo $sysconfdir | sed -e 's#^/##'`

mkdirs() {
  (cd ${topdir} && \
  rm -fr ${objdir} ${instdir} ${srcinstdir} && \
  mkdir -p ${objdir} && \
  mkdir -p ${instdir} && \
  mkdir -p ${srcinstdir}
)}

fixup() {
  thedir=$1
  (cd ${thedir} &&
  if [ -n "${src_official_patches}" ] ; then
    for fn in ${src_official_patches} ; do
      if [ -f ${topdir}/$fn ] ; then
        echo "APPLYING OFFICIAL INCREMENTAL PATCH ${fn}"
        patch -p2 < ${topdir}/$fn
      fi
    done
  fi
  rm -f vi_mode.c.orig
)}

prep() {
  (cd ${topdir} && \
  tar xv${opt_decomp}f ${src_orig_pkg} && \
  fixup ${srcdir}
  cd ${srcdir} && \
  patch -p1 < ${src_patch} 
  mv /usr/lib/libtermcap.a /usr/lib/libtermcap.a.orig 
  mkdirs
)}

conf() {
  (cd ${objdir} && \
  CFLAGS=-I/usr/include/ncurses ${srcdir}/configure \
  --srcdir=${srcdir} --prefix=${prefix} \
  --exec-prefix=${prefix} --sysconfdir=${sysconfdir} \
  --libdir=${prefix}/lib --includedir=${prefix}/include \
  --mandir=${prefix}/share/man --infodir=${prefix}/share/info \
  --with-curses
)}

build() {
  (cd ${objdir} && \
  make CFLAGS=-O2 DESTDIR=${instdir} && \
  make shared CFLAGS=-O2 DESTDIR=${instdir} && \
  make examples CFLAGS=-O2 DESTDIR=${instdir}
)}

check() {
  (cd ${objdir} && \
  make test | tee ${checkfile} 2>&1
)}

clean() {
  (cd ${objdir} && \
  make clean
)}

install() {
  (cd ${objdir} && \
  make install INSTALL=install DESTDIR=${instdir} &&
  make install-shared INSTALL=install DESTDIR=${instdir} &&
  make install-examples INSTALL=install DESTDIR=${instdir} &&
  for fn in ${prefix}/share/info/dir ${prefix}/bin/fileman-stat.exe \
	${prefix}/bin/rl-stat.exe ${prefix}/bin/rltest-stat.exe \
	${prefix}/bin/rlversion-stat.exe ; do
    if [ -f ${instdir}${fn} ] ; then \
      rm ${instdir}${fn} ; \
    fi ;\
  done && \
  if [ ! -d ${instdir}${prefix}/share/doc/${PKG}-${VER} ]; then \
    mkdir -p ${instdir}${prefix}/share/doc/${PKG}-${VER} ; \
  fi && \
  if [ ! -d ${instdir}${prefix}/share/doc/Cygwin ]; then \
    mkdir -p ${instdir}${prefix}/share/doc/Cygwin ; \
  fi && \
#  if [ ! -d ${instdir}${sysconfdir}/postinstall ]; then \
#    mkdir -p ${instdir}${sysconfdir}/postinstall ; \
#  fi && \
  templist=""; \
  for f in ${srcdir}/README ${srcdir}/USAGE ${srcdir}/CHANGELOG \
           ${srcdir}/CHANGES ${srcdir}/COPYING ${srcdir}/NEWS ; do \
    if [ -f $f ] ; then \
      templist="$templist $f"; \
    fi ; \
  done && \
  if [ ! "x$templist" = "x" ]; then \
    /usr/bin/install -m 644 $templist \
	 ${instdir}${prefix}/share/doc/${PKG}-${VER} ;
  fi && \
  if [ -f ${srcdir}/CYGWIN-PATCHES/${PKG}.README ]; then \
    /usr/bin/install -m 644 ${srcdir}/CYGWIN-PATCHES/${PKG}.README \
      ${instdir}${prefix}/share/doc/Cygwin/${PKG}-${VER}.README ; \
  else \
    if [ -f ${srcdir}/CYGWIN-PATCHES/README ]; then \
      /usr/bin/install -m 644 ${srcdir}/CYGWIN-PATCHES/README \
	${instdir}${prefix}/share/doc/Cygwin/${PKG}-${VER}.README ; \
    fi ;\
  fi && \
#  if [ -f ${srcdir}/CYGWIN-PATCHES/postinstall.sh ]; then \
#    /usr/bin/install -m 644 ${srcdir}/CYGWIN-PATCHES/postinstall.sh \
#      ${instdir}${sysconfdir}/postinstall/${PKG}.sh ; \
#  fi && \
  cd ${instdir} && rm -f `find . -name "*.old"`
)}

mkpatch() {
  (cd ${srcdir} && \
  tar xv${opt_decomp}f ${src_orig_pkg} && \
  mv ${PKG}-${VER} ../${PKG}-${VER}-orig && \
  temp=`(cd ../${PKG}-${VER}-orig ; pwd)` && \
  fixup $temp
  cd ${topdir} &&
  diff -urN -x '.build' -x '.inst' -x '.sinst' \
    ${PKG}-${VER}-orig ${PKG}-${VER} > \
    ${srcinstdir}/${src_patch_name} ; \
  rm -rf ${PKG}-${VER}-orig
)}

strip() {
  (cd ${instdir} && \
  find . -name "*.exe" | xargs strip >/dev/null 2>&1 ;
  find . -name "*.dll" | xargs strip >/dev/null 2>&1
)}

pkg() {
  (cd ${instdir} && \
  tar cvjf ${bin_pkg} --exclude="${pnoslash}/bin/*.dll" * && \
  tar cvjf ${bin_pkg2} ${pnoslash}/bin/*.dll
)}

spkg() {
  (mkpatch && \
  cp ${src_orig_pkg} ${srcinstdir}/${src_orig_pkg_name} && \
  if [ -n "${src_official_patches}" ] ; then
    for fn in ${src_official_patches} ; do
      if [ -f ${topdir}/$fn ] ; then
        cp ${topdir}/$fn ${srcinstdir}/$fn
      fi
    done
  fi
  cp $0 ${srcinstdir}/`basename $0` && \
  cd ${srcinstdir} && \
  tar cvjf ${src_pkg} *
)}

finish() {
  mv /usr/lib/libtermcap.a.orig /usr/lib/libtermcap.a
  rm -rf ${srcdir}
}

case $1 in
  prep)	prep ; STATUS=$? ;;
  mkdirs)	mkdirs ; STATUS=$? ;;
  conf)	conf ; STATUS=$? ;;
  build)	build ; STATUS=$? ;;
  check)	check ; STATUS=$? ;;
  clean)	clean ; STATUS=$? ;;
  install)	install ; STATUS=$? ;;
  strip)	strip ; STATUS=$? ;;
  mkpatch) mkdirs ; mkpatch ; STATUS=$? ;;
  package)	pkg ; STATUS=$? ;;
  pkg)	pkg ; STATUS=$? ;;
  src-package)	spkg ; STATUS=$? ;;
  spkg)	spkg ; STATUS=$? ;;
  finish) finish ; STATUS=$? ;;
  all) prep && conf && build && install && \
     strip && pkg && spkg && finish && STATUS=$? ;;
  *) echo "Error: bad arguments" ; exit 1 ;;
esac
exit ${STATUS}
