#!/bin/sh
# 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/^[^\-]*\-//' -e 's/\-[^\-]*$//'`
export REL=`echo $tscriptname | sed -e 's/^[^\-]*\-[^\-]*\-//'`
export FULLPKG=${PKG}-${VER}-${REL}
# if the orig src package is bzip2'ed, remember to
# change 'z' to 'j' in the 'tar xvzf' commands in the
# prep) and mkpatch) sections
export src_orig_pkg_name=${PKG}-${VER}.tar.gz
export src_pkg_name=${FULLPKG}-src.tar.bz2
export src_patch_name=${FULLPKG}.patch
export bin_pkg_name=${FULLPKG}.tar.bz2

# This is to get the "developers" patch
export src_devpkg_name=${FULLPKG}-src.dev.tar.bz2
export src_devpkg=${topdir}/${src_devpkg_name}
export src_devpatch_name=${FULLPKG}.dev.patch
export src_devpatch=${topdir}/${src_devpatch_name}

export src_orig_pkg=${topdir}/${src_orig_pkg_name}
export src_pkg=${topdir}/${src_pkg_name}
export src_patch=${topdir}/${src_patch_name}
export bin_pkg=${topdir}/${bin_pkg_name}
export srcdir=${topdir}/${PKG}-${VER}
export objdir=${srcdir}/.build
export instdir=${srcdir}/.inst
export srcinstdir=${srcdir}/.sinst
export checkfile=${topdir}/${FULLPKG}.check
# run on
host=i686-pc-cygwin
# if this package creates binaries, they run on
target=i686-pc-cygwin
prefix=/usr
sysconfdir=/etc
MY_CFLAGS="-O2 -g"
MY_LDFLAGS=

mkdirs() {
  (cd ${topdir} && \
  mkdir -p ${objdir} && \
  mkdir -p ${instdir} && \
  mkdir -p ${srcinstdir} )
}
# GnuPG needs a ./scripts/autogen.sh run
devprep() {
  (cd ${topdir} && \
  tar xvzf ${src_orig_pkg} ; \
  cd ${topdir} && \
  patch -p0 < ${src_devpatch} && \
  cd ${srcdir} && \
  ./scripts/autogen.sh \
  && mkdirs )
}
prep() {
  (cd ${topdir} && \
  tar xvzf ${src_orig_pkg} ; \
  cd ${topdir} && \
  patch -p0 < ${src_patch} && \
  cd ${srcdir} && mkdirs )
}
devconf() {
  (devflag="--enable-maintainer-mode" conf )
}
# Attention! Don't use --host and --target for AC 2.5x builds
conf() {
  (cd ${objdir} && \
  CFLAGS="${MY_CFLAGS}" LDFLAGS="${MY_LDFLAGS}" \
  ${srcdir}/configure  $devflag \
  --srcdir=${srcdir} --prefix=${prefix} \
  --exec-prefix=${prefix} --sysconfdir=${sysconfdir} \
  --libdir=${prefix}/lib --includedir=${prefix}/include \
  --mandir=${prefix}/share/man --infodir=${prefix}/share/info \
  --libexecdir='${sbindir}' --localstatedir=/var \
  --datadir='${prefix}/share' \
#  --host=${host} --target=${target}
)
}
build() {
  (cd ${objdir} && \
  CFLAGS="${MY_CFLAGS}" make )
}
check() {
  (cd ${objdir} && \
  make check )
}
clean() {
  (cd ${objdir} && \
  make clean )
}
install() {
  (cd ${objdir} && \
  make install DESTDIR=${instdir} 

  for f in ${prefix}/share/info/dir ${prefix}/info/dir ; do
    if [ -f ${instdir}${f} ] ; then \
      rm -f ${instdir}${f} ; \
    fi ;\
  done &&\
  for d in ${prefix}/share/doc/${PKG}-${VER} ${prefix}/share/doc/Cygwin \
           ${sysconfdir}/postinstall ; do
    if [ ! -d ${instdir}${d} ] ; then
      mkdir -p ${instdir}${d} ;\
    fi ;\
  done &&\
  templist=""; \
  for f in ${srcdir}/AUTHORS \
           ${srcdir}/BUGS \
           ${srcdir}/COPYING \
           ${srcdir}/INSTALL \
           ${srcdir}/NEWS \
           ${srcdir}/PROJECTS \
           ${srcdir}/README \
           ${srcdir}/THANKS \
           ${srcdir}/TODO \
           ${srcdir}/VERSION \
           ${srcdir}/doc/FAQ \
           ${srcdir}/doc/HACKING \
           ${srcdir}/doc/OpenPGP ; 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 755 ${srcdir}/CYGWIN-PATCHES/postinstall.sh \
      ${instdir}${sysconfdir}/postinstall/${PKG}.sh ; \
  fi ;\
  )
}
strip() {
  (cd ${instdir} && \
  find . -name "*.dll" | xargs strip > /dev/null 2>&1
  find . -name "*.exe" | xargs strip > /dev/null 2>&1 )
}
pkg() {
  (cd ${instdir} && \
  tar cvjf ${bin_pkg} * )
}
mkpatch() {
  (cd ${srcdir} && \
  tar xvzf ${src_orig_pkg} ;\
  mv ${PKG}-${VER} ../${PKG}-${VER}-orig && \
  cp po/ChangeLog ../${PKG}-${VER}-orig/po/ && \
  cd ${topdir} && \
  diff -urN -x '.build' -x '.inst' -x '.sinst' \
    -x 'autom4te.cache' -x '*~' \
    ${PKG}-${VER}-orig ${PKG}-${VER} > \
    ${srcinstdir}/${src_patch_name} ; \
  rm -rf ${PKG}-${VER}-orig )
}
# GnuPG uses autotools -> some exceptions
# I copy the autogenerated po/ChangeLog to pseudo original source
# because I found no way to exclude po/Changelog with -x ''
# The mkpatch_dev shows only the "real" changes.
devmkpatch() {
  (cd ${srcdir} && \
  tar xvzf ${src_orig_pkg} ;\
  mv ${PKG}-${VER} ../${PKG}-${VER}-orig && \
  cp po/ChangeLog ../${PKG}-${VER}-orig/po/ && \
  cd ${topdir} && \
  diff -urN -x '.build' -x '.inst' -x '.sinst' -x '.deps' \
    -x 'autom4te.cache' -x 'aclocal.m4' -x '*.in' -x '*~' \
    -x 'gpg.info' -x 'configure' -x 'Makefile' -x 'stamp-h1' -x 'compile' \
    -x 'depcomp' -x 'install-sh' -x 'missing' -x 'mkinstalldirs' \
    -x 'construct.c' -x 'config.*' -x 'g10defs.h' \
    -x 'm4' -x 'intl' -x '*.po' -x '*.pot' -x 'Rules-quot' -x '*.sed' \
    -x '*.header' -x '*.sin' -x '*.gmo' -x 'ABOUT-NLS' -x 'COPYING' \
    -x 'INSTALL' \
    -x 'Makevars.template' -x 'ltmain.sh' -x 'zconf.h' -x 'zlib.h' \
    -x 'Makevars' -x '*.info' -x '*.orig' \
    ${PKG}-${VER}-orig ${PKG}-${VER} > \
    ${srcinstdir}/${src_devpatch_name} ; \
  rm -rf ${PKG}-${VER}-orig )
}
spkg() {
  (mkpatch && \
 if [ "${SIG}" ]; then name=${srcinstdir}/${src_patch_name} text="PATCH" sigfile; fi && \
  cp ${src_orig_pkg} ${srcinstdir}/${src_orig_pkg_name} && \
 if [ -e ${src_orig_pkg}.sig ]; then cp ${src_orig_pkg}.sig ${srcinstdir}/; fi && \
  cp $0 ${srcinstdir}/`basename $0` && \
 if [ \( "${SIG}" \) -a \( -e $0.sig \) ]; then cp $0.sig ${srcinstdir}/; fi && \
  cd ${srcinstdir} && \
  tar cvjf ${src_pkg} * )
}
devspkg() {
  (devmkpatch && \
 if [ "${SIG}" ]; then name=${srcinstdir}/${src_devpatch_name} text="PATCH" sigfile; fi && \
  cp ${src_orig_pkg} ${srcinstdir}/${src_orig_pkg_name} && \
 if [ -e ${src_orig_pkg}.sig ]; then cp ${src_orig_pkg}.sig ${srcinstdir}/; fi && \
  cp $0 ${srcinstdir}/`basename $0` && \
 if [ \( "${SIG}" \) -a \( -e $0.sig \) ]; then cp $0.sig ${srcinstdir}/; fi && \
  cd ${srcinstdir} && \
  tar cvjf ${src_devpkg} * )
}
finish() {
  rm -rf ${srcdir} 
}
# Signing stuff
sigfile() {
  if [ \( "${SIG}" \) -a \( -e "$name" \) -a \( \( ! -e "$name.sig" \) -o \( "$name" -nt "$name.sig" \) \) ]; then \
    if [ -x /usr/bin/gpg ]; then \
      echo "$text signature need to be updated"; \
      /usr/bin/gpg --detach-sign $name; \
    else \
      echo "You need the gnupg package installed in order to make signatures."; \
    fi; \
  fi;
}
checksig() {
  if [ -x /usr/bin/gpg ]; then \
    if [ -e $0.sig ]; then \
      echo "SCRIPT signature follows:"; \
      /usr/bin/gpg --verify $0.sig $0; \
    else \
      echo "SCRIPT signature missing."; \
    fi; \
    if [ -e ${src_orig_pkg}.sig ]; then \
      echo "ORIGINAL PACKAGE signature follows:"; \
      /usr/bin/gpg --verify ${src_orig_pkg}.sig ${src_orig_pkg}; \
    else \
      echo "ORIGINAL PACKAGE signature missing."; \
    fi; \
    if [ -e ${src_patch}.sig ]; then \
      echo "PATCH signature follows:"; \
      /usr/bin/gpg --verify ${src_patch}.sig ${src_patch}; \
    else \
      echo "PATCH signature missing."; \
    fi; \
    if [ -e ${src_devpatch}.sig ]; then \
      echo "DEVPATCH signature follows:"; \
      /usr/bin/gpg --verify ${src_devpatch}.sig ${src_devpatch}; \
    else \
#      echo "DEVPATCH signature missing."; \
    fi; \
  else
    echo "You need the gnupg package installed in order to check signatures."; \
  fi;
}
# end
case $1 in
  prep)	prep ; STATUS=$? ;;
  devprep)	devprep ; STATUS=$? ;;
  mkdirs)	mkdirs; STATUS=$? ;;
  devconf)	devconf ; STATUS=$? ;;
  conf)		conf ; STATUS=$? ;;
  build)	build ; STATUS=$? ;;
  check)	check ; STATUS=$? ;;
  clean)	clean ; STATUS=$? ;;
  install)	install ; STATUS=$? ;;
  strip)	strip ; STATUS=$? ;;
  package)	pkg ; STATUS=$? ;;
  pkg)	pkg ; STATUS=$? ;;
  mkpatch)	mkpatch ; STATUS=$? ;;
  devmkpatch)	devmkpatch ; STATUS=$? ;;
  src-package)	spkg ; STATUS=$? ;;
  spkg)		name=$0 text="SCRIPT" sigfile; spkg ; STATUS=$? ;;
  devspkg)	name=$0 text="SCRIPT" sigfile; devspkg ; STATUS=$? ;;
  finish)	finish ; STATUS=$? ;;
  sigfile)	sigfile ; STATUS=$? ;;
  checksig)	checksig ; STATUS=$? ;;
  all) prep && conf && build && install && \
     strip && pkg && name=$0 text="SCRIPT" sigfile && spkg && \
     finish && echo All finished! ; \
	  STATUS=$? ;;
  devall) devprep && devconf && build && check && install && \
     strip && pkg && name=$0 text="SCRIPT" sigfile && devspkg && \
     finish && echo All dev finished! ; \
	  STATUS=$? ;;
  *) echo "Error: bad arguments" ; exit 1 ;;
esac
exit ${STATUS}

