#!/bin/bash -e
#
# Generic package build script
#
# $Id: generic-build-script,v 1.24 2004/06/19 19:23:09 igor Exp $
#
# Package maintainers: if the original source is not distributed as a
# (possibly compressed) tarball, set the value of ${src_orig_pkg_name},
# and redefine the unpack() helper function appropriately.
# Also, if the Makefile rule to run the test suite is not "test", change
# the definition of ${test_rule} below.

# 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}\-//"`
export VER2=${VER}
export BASEPKG=libtool-${VER2}
export FULLPKG=${PKG}-${VER}-${REL}

# determine correct decompression option and tarball filename
export src_orig_pkg_name=
if [ -e "${src_orig_pkg_name}" ] ; then
  export opt_decomp=? # Make sure tar punts if unpack() is not redefined
elif [ -e ${BASEPKG}.tar.bz2 ] ; then
  export opt_decomp=j
  export src_orig_pkg_name=${BASEPKG}.tar.bz2
elif [ -e ${BASEPKG}.tar.gz ] ; then
  export opt_decomp=z
  export src_orig_pkg_name=${BASEPKG}.tar.gz
elif [ -e ${BASEPKG}.tgz ] ; then
  export opt_decomp=z
  export src_orig_pkg_name=${BASEPKG}.tgz
elif [ -e ${BASEPKG}.tar ] ; then
  export opt_decomp=
  export src_orig_pkg_name=${BASEPKG}.tar
else
  echo Cannot find original package.
  exit 1
fi

export src_orig_pkg=${topdir}/${src_orig_pkg_name}

# determine correct names for generated files
export src_pkg_name=${FULLPKG}-src.tar.bz2
export src_patch_name=${FULLPKG}.patch
export src_patch_name2=${FULLPKG}.new
export bin_pkg_name=${FULLPKG}.tar.bz2
export bin_pkg_name2=libltdl3-${VER}-${REL}.tar.bz2

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}/${BASEPKG}
export objdir=${srcdir}/.build
export instdir=${srcdir}/.inst
export srcinstdir=${srcdir}/.sinst
export checkfile=${topdir}/${FULLPKG}.check


prefix=/usr/autotool/devel
docprefix=/usr
sysconfdir=/etc
localstatedir=/var
pnoslash=`echo $prefix | sed -e 's#^/##'`
snoslash=`echo $sysconfdir | sed -e 's#^/##'`
dnoslash=`echo $docprefix | sed -e 's#^/##'`
path_old="${PATH}"

if [ -z "$MY_CFLAGS" ]; then
  MY_CFLAGS="-O2"
fi
if [ -z "$MY_CFLAGS" ]; then
  MY_LDFLAGS=
fi

export install_docs="\
	ABOUT-NLS \
	ANNOUNCE \
	AUTHORS \
	BUG-REPORTS \
	CHANGES \
	CONTRIBUTORS \
	COPYING \
	CREDITS \
	ChangeLog* \
	FAQ \
	HOW-TO-CONTRIBUTE \
	INSTALL \
	KNOWNBUG \
	LEGAL \
	LICENSE \
	NEWS \
	NOTES \
	PROGLIST \
	README \
	THANKS \
	TODO \
"
export install_docs="`for i in ${install_docs}; do echo $i; done | sort -u`"
export test_rule=check
if [ -z "$SIG" ]; then
  export SIG=0	# set to 1 to turn on signing by default
fi

# helper function
# unpacks the original package source archive into ./${BASEPKG}/
# change this if the original package was not tarred
# or if it doesn't unpack to a correct directory
unpack() {
  tar xv${opt_decomp}f "$1"
}

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

patchit() {
  thedir=$1
  (export PATH="${prefix}/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin" && \
  cd ${thedir}             && \
  if [ -f ${src_patch} ] ; then \
    patch -p1 < ${src_patch} ;\
  fi
)}

fixup() {
  thedir=$1
  (export PATH="${prefix}/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin" && \
  cd ${thedir}             && \
  ./bootstrap
)}

prep() {
  (export PATH="${prefix}/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin" && \
  cd ${topdir} && \
  unpack ${src_orig_pkg} && \
  patchit ${srcdir}
  fixup ${srcdir}
  cd ${topdir} && \
#   patch -p0 < ${src_patch2}
  mkdirs && \
  export PATH="${path_old}"
)}

conf() {
  (export PATH="${prefix}/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin" && \
  cd ${objdir} && \
  CFLAGS="${MY_CFLAGS}" LDFLAGS="${MY_LDFLAGS}" \
  ${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' \
  --libexecdir='${sbindir}' --localstatedir="${localstatedir}" \
  --datadir='${prefix}/share' && \
  export PATH="${path_old}"
)}

reconf() {
  (export PATH="${prefix}/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin" && \
  cd ${topdir} && \
  rm -fr ${objdir} && \
  mkdir -p ${objdir} && \
  conf && \
  export PATH="${path_old}"
)}

build() {
  (export PATH="${prefix}/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin" && \
  cd ${objdir} && \
  CFLAGS="${MY_CFLAGS}" make && \
  export PATH="${path_old}" )
}
check() {
  (export PATH="${prefix}/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin" && \
  cd ${objdir} && \
  make ${test_rule} | tee ${checkfile} 2>&1 && \
  export PATH="${path_old}" )
}
clean() {
  (export PATH="${prefix}/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin" && \
  cd ${objdir} && \
  make clean && \
  export PATH="${path_old}" )
}
install() {
  (export PATH="${prefix}/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin" && \
  cd ${objdir} && \
  rm -fr ${instdir}/* && \
  make install INSTALL=install \
    prefix=${instdir}${prefix} \
    exec_prefix=${instdir}${prefix} \
    bindir=${instdir}${prefix}/bin \
    libdir=${instdir}${prefix}/lib \
    mandir=${instdir}${prefix}/share/man \
    infodir=${instdir}${prefix}/share/info \
    sysconfdir=${instdir}${sysconfdir} \
    includedir=${instdir}${prefix}/include \
    tooldir=${instdir}${prefix} && \
  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 ${docprefix}/share/doc/${PKG}-${VER} ${docprefix}/share/doc/Cygwin \
           ${docprefix}/share/info ${docprefix}/bin ${docprefix}/include \
           ${docprefix}/lib ; do \
    if [ ! -d ${instdir}${d} ] ; then \
      mkdir -p ${instdir}${d} ;\
    fi ;\
  done &&\
  if [ -d ${instdir}${prefix}/share/info ] ; then \
    find ${instdir}${prefix}/share/info -name "*.info" | xargs gzip -q >/dev/null 2>&1 ; \
    find ${instdir}${prefix}/share/info -name "*.info-[0123456789]*" | xargs gzip -q >/dev/null 2>&1 ; \
    true
  fi && \
  if [ -d ${instdir}${prefix}/share/man ] ; then \
    find ${instdir}${prefix}/share/man -name "*.1" -o -name "*.3" -o \
      -name "*.3x" -o -name "*.3pm" -o -name "*.5" -o -name "*.6" -o \
      -name "*.7" -o -name "*.8" | xargs gzip -q ; \
  fi && \
  templist="" && \
  real_install_docs="`for i in ${install_docs}; do echo ${srcdir}/$i; done | sort -u`"
  for f in ${real_install_docs} ; do \
    if [ -f $f ] ; then \
      templist="$templist $f"; \
    fi ; \
  done && \
  if [ ! "x$templist" = "x" ]; then \
    /usr/bin/install -m 644 $templist \
         ${instdir}${docprefix}/share/doc/${PKG}-${VER} ; \
  fi && \
  if [ -f ${srcdir}/CYGWIN-PATCHES/${PKG}.README ]; then \
    /usr/bin/install -m 644 ${srcdir}/CYGWIN-PATCHES/${PKG}.README \
      ${instdir}${docprefix}/share/doc/Cygwin/${PKG}-${VER}.README ; \
  elif [ -f ${srcdir}/CYGWIN-PATCHES/README ] ; then \
    /usr/bin/install -m 644 ${srcdir}/CYGWIN-PATCHES/README \
      ${instdir}${docprefix}/share/doc/Cygwin/${PKG}-${VER}.README ; \
  fi && \
  if [ -f ${srcdir}/CYGWIN-PATCHES/${PKG}.sh ] ; then \
    if [ ! -d ${instdir}${sysconfdir}/postinstall ]; then \
      mkdir -p ${instdir}${sysconfdir}/postinstall ; \
    fi && \
    /usr/bin/install -m 755 ${srcdir}/CYGWIN-PATCHES/${PKG}.sh \
      ${instdir}${sysconfdir}/postinstall/${PKG}.sh ; \
  elif [ -f ${srcdir}/CYGWIN-PATCHES/postinstall.sh ] ; then \
    if [ ! -d ${instdir}${sysconfdir}/postinstall ]; then \
      mkdir -p ${instdir}${sysconfdir}/postinstall ; \
    fi && \
    /usr/bin/install -m 755 ${srcdir}/CYGWIN-PATCHES/postinstall.sh \
      ${instdir}${sysconfdir}/postinstall/${PKG}.sh ; \
  fi && \
  if [ -f ${srcdir}/CYGWIN-PATCHES/preremove.sh ] ; then \
    if [ ! -d ${instdir}${sysconfdir}/preremove ]; then \
      mkdir -p ${instdir}${sysconfdir}/preremove ; \
    fi && \
    /usr/bin/install -m 755 ${srcdir}/CYGWIN-PATCHES/preremove.sh \
      ${instdir}${sysconfdir}/preremove/${PKG}.sh ; \
  fi && \
  cp ${instdir}${prefix}/share/info/* ${instdir}${docprefix}/share/info/ && \
  cp ${instdir}${prefix}/bin/*.dll ${instdir}${docprefix}/bin/ && \
  cp ${instdir}${prefix}/lib/*     ${instdir}${docprefix}/lib/ && \
  cp ${instdir}${prefix}/include/* ${instdir}${docprefix}/include/ && \
  la_munge && \
  export PATH="${path_old}" )
}
la_munge() {
  (cd ${instdir}${docprefix}/lib && \
  for fn in *.la ; do
    cat $fn | sed -e "s#${prefix}/lib#${docprefix}/lib#g" > $fn.tmp
         mv $fn.tmp $fn
  done )
}
strip() {
  (export PATH="${prefix}/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin" && \
  cd ${instdir} && \
  find . -name "*.dll" -or -name "*.exe" | xargs strip >/dev/null 2>&1 ; \
  true 
  export PATH="${path_old}" )
}
list() {
  (cd ${instdir} && \
  find . -name "*" ! -type d | sed 's%^\.%  %' ; \
  true )
}
depend() {
  (cd ${instdir} && \
  find ${instdir} -name "*.exe" -o -name "*.dll" | xargs cygcheck | \
  sed -e '/\.exe/d' -e 's,\\,/,g' | sort -bu | xargs -n1 cygpath -u \
  | xargs cygcheck -f | sed 's%^%  %' ; \
  true )
}
pkg() {
  (cd ${instdir} && \
  tar cvjf ${bin_pkg2} ${pnoslash}/bin/*.dll ${dnoslash}/bin/*.dll && \
  tar cvjf ${bin_pkg}  --exclude="${pnoslash}/bin/*.dll" --exclude="${dnoslash}/bin/*.dll" * )
}
mkpatch() {
  (export PATH="${prefix}/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin" && \
  cd ${srcdir} && \
#  find . -name "autom4te.cache" | xargs rm -rf ; \
  unpack ${src_orig_pkg} && \
  mv ${BASEPKG} ../${BASEPKG}-orig && \
  temp=`(cd ../${BASEPKG}-orig ; pwd)` && \
#  patchit $temp 
#  fixup $temp
  cd $temp && \
#  find . -name "autom4te.cache" | xargs rm -rf ; \
  cd ${topdir} && \
  diff -urN -x '.build' -x '.inst' -x '.sinst' -x '*~' -x 'ltmain.sh' \
    -x 'autom4te.cache' -x 'aclocal.m4' -x 'Makefile.in' -x 'acinclude.m4' \
    -x 'config.h.in' -x '*.rej' -x '*.orig' \
    -x 'configure' -x 'Makefile' -x 'stamp-h1' \
    -x 'install-sh' -x 'missing' -x 'mkinstalldirs' \
    -x 'config.h' -x 'config.log' -x 'config.status' \
    ${BASEPKG}-orig ${BASEPKG} > \
    ${src_patch2} ; \
  rm -rf ${BASEPKG}-orig && \
  export PATH="${path_old}" )
}
spkg() {
  (mkpatch && \
  export PATH="${prefix}/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin" && \
  cp ${src_patch} ${srcinstdir}/${src_patch_name} && \
  if [ "${SIG}" -eq 1 ] ; 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` && \
  name=$0 text="SCRIPT" sigfile && \
  if [ "${SIG}" -eq 1 ] ; then \
    cp $0.sig ${srcinstdir}/ ; \
  fi && \
  cd ${srcinstdir} && \
  tar cvjf ${src_pkg} * && \
  export PATH="${path_old}" )
}
finish() {
  rm -rf ${srcdir}
}
sigfile() {
  if [ \( "${SIG}" -eq 1 \) -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"; \
      rm -f $name.sig; \
      /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 ${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 $0.sig ]; then \
      echo "SCRIPT signature follows:"; \
      /usr/bin/gpg --verify $0.sig $0; \
    else \
      echo "SCRIPT 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; \
  else
    echo "You need the gnupg package installed in order to check signatures." ; \
  fi
}
while test -n "$1" ; do
  case $1 in
    prep)		prep ; STATUS=$? ;;
    mkdirs)		mkdirs ; STATUS=$? ;;
    conf)		conf ; STATUS=$? ;;
    configure)		conf ; STATUS=$? ;;
    reconf)		reconf ; STATUS=$? ;;
    build)		build ; STATUS=$? ;;
    make)		build ; STATUS=$? ;;
    check)		check ; STATUS=$? ;;
    test)		check ; STATUS=$? ;;
    clean)		clean ; STATUS=$? ;;
    install)		install ; STATUS=$? ;;
    list)		list ; STATUS=$? ;;
    depend)		depend ; STATUS=$? ;;
    strip)		strip ; STATUS=$? ;;
    package)		pkg ; STATUS=$? ;;
    pkg)		pkg ; STATUS=$? ;;
    mkpatch)		mkpatch ; STATUS=$? ;;
    src-package)	spkg ; STATUS=$? ;;
    spkg)		spkg ; STATUS=$? ;;
    finish)		finish ; STATUS=$? ;;
    checksig)		checksig ; STATUS=$? ;;
    first)		mkdirs && spkg && finish ; STATUS=$? ;;
    all)		checksig && prep && conf && build && install && \
			strip && pkg && spkg && finish ; \
			STATUS=$? ;;
    *) echo "Error: bad arguments" ; exit 1 ;;
  esac
  ( exit ${STATUS} ) || exit ${STATUS}
  shift
done
export PATH="${path_old}"
exit ${STATUS}
