#!/bin/sh
#
# Generic package build script
#
# $Id: generic-build-script,v 1.34 2005/07/30 16:01:12 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}\-//"`
# BASEPKG refers to the upstream base package
# CYGPKG refers to the Cygwin package
# Normally, these are identical, but if the Cygwin package name is different
# from the upstream package name, you will want to redefine BASEPKG.
# Example: For Apache 2, BASEPKG=httpd-2.x.xx but CYGPKG=apache2-2.x.xx
export BASEPKG=${PKG}-${VER}
#export BASEPKG=claws-mail-${VER}
export CYGPKG=${PKG}-${VER}
export FULLPKG=${PKG}-${VER}-${REL}

# determine correct decompression option and tarball filename
#first try the debian tarball name
#export src_orig_pkg_name=${PKG}-gtk2_${VER}.orig.tar.bz2
#then upstream release
export src_orig_pkg_name=${PKG}_${VER}.orig.tar.bz2
if [ -e "${src_orig_pkg_name}" ] ; then
  export opt_decomp=j # 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 bin_pkg_name=${FULLPKG}.tar.bz2
export devel_pkg_name=${PKG}-devel-${VER}-${REL}.tar.bz2
export i18n_pkg_name=${PKG}-i18n-${VER}-${REL}.tar.bz2

export src_pkg=${topdir}/${src_pkg_name}
export src_patch=${topdir}/${src_patch_name}
export bin_pkg=${topdir}/${bin_pkg_name}
export srcdir=${topdir}/${BASEPKG}
export objdir=${srcdir}/.build
export instdir=${srcdir}/.inst
export srcinstdir=${srcdir}/.sinst
export checkfile=${topdir}/${FULLPKG}.check

prefix=/usr/local
sysconfdir=/usr/local/etc
localstatedir=/var
if [ -z "$MY_CFLAGS" ]; then
  MY_CFLAGS="-O2"
fi
if [ -z "$MY_LDFLAGS" ]; then
  MY_LDFLAGS=
fi

prefix_noslash=`echo ${prefix} | sed 's%^/%%'`
export install_docs="\
	ABOUT-NLS \
	ANNOUNCE \
	AUTHORS \
	BUG-REPORTS \
	CHANGES \
	CONTRIBUTORS \
	COPYING \
	COPYRIGHT \
	CREDITS \
	CHANGELOG \
	ChangeLog \
	FAQ \
	HOW-TO-CONTRIBUTE \
	INSTALL \
	KNOWNBUG \
	LEGAL \
	LICENSE \
	NEWS \
	NOTES \
	PROGLIST \
	README \
	README.claws \
	RELEASE_NOTES \
	THANKS \
	TODO \
	TODO-gtk2 \
"
export install_docs="`for i in ${install_docs}; do echo $i; done | sort -u`"
export test_rule=test
if [ -z "$SIG" ]; then
  export SIG=0	# set to 1 to turn on signing by default
fi

# helper functions

# Provide help.
help() {
cat <<EOF
This is the cygwin packaging script for ${FULLPKG}.
Usage: $0 <action>
Actions are:
    help, --help	Print this message
    version, --version	Print the version message
    prep		Unpack and patch into ${srcdir}
    mkdirs		Make hidden directories needed during build
    conf, configure	Configure the package (./configure)
    reconf		Rerun configure
    build, make		Build the package (make)
    check, test	    	Run the testsuite (make ${test_rule})
    clean		Remove built files (make clean)
    install		Install package to staging area (make install)
    list		List package contents
    depend		List package dependencies
    strip		Strip package executables
    pkg, package	Prepare the binary package ${bin_pkg_name}
    mkpatch		Prepare the patch file ${src_patch_name}
    acceptpatch		Apply a patch to the source
    spkg, src-package	Prepare the source package ${src_pkg_name}
    finish		Remove source directory ${srcdir}
    checksig		Validate GPG signatures (requires gpg)
    first		Full run for spkg (mkdirs, spkg, finish)
    almostall		Full run for bin pkg, except for finish
    all			Full run for bin pkg
EOF
}

# Provide version of generic-build-script modified to make this
version() {
    vers=`echo '$Revision: 1.34 $' | sed -e 's/Revision: //' -e 's/ *\\$//g'`
    echo "$0 based on generic-build-script $vers"
}

# 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 x${opt_decomp}f "$1"
}

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

patch() {
  (cd ${srcdir} && \
  if [ -f ${src_patch} ] ; then 
     /usr/bin/patch -p1 < ${src_patch}
  else
      echo "******* WARNING: no patch found for this package. ******"
  fi
  )
}

prep() {
  (cd ${topdir} && \
  unpack ${src_orig_pkg} && \
  mkdirs && \
  patch)
}
conf() {
  (cd ${objdir} &&
  CFLAGS="${MY_CFLAGS}" LDFLAGS="${MY_LDFLAGS}" X_LIBS="-L/usr/X11R6/lib" \
  ${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' \
  --enable-openssl --disable-ldap --enable-aspell --enable-compface \
  --enable-image-viewer-plugin --enable-dillo-viewer-plugin --enable-trayicon-plugin --enable-spamassassin-plugin \
  --disable-crash-dialog --disable-gnomeprint
  )
}
reconf() {
  (cd ${topdir} && \
  rm -fr ${objdir} && \
  mkdir -p ${objdir} && \
  conf )
}
build() {
  (cd ${objdir} && \
  CFLAGS="${MY_CFLAGS}" make )
}
check() {
  (cd ${objdir} && \
  make ${test_rule} | tee ${checkfile} 2>&1 )
}
clean() {
  (cd ${objdir} && \
  make clean )
}
install() {
  install_part1 && install_part2 
}

install_part1() {
  (cd ${objdir} && \
  rm -fr ${instdir}/* && \
  make -i install DESTDIR=${instdir})
}

# - install spamassassin_gtk.dll (due to some reason, libtool --install would fail on this plugin)
# - install tools scripts
# - install documents
# - install pre/post-install pre/post-remove scripts
install_part2() {  
  (cd ${objdir} && 
  if [ ! -d ${instdir}${prefix}/share/claws-mail/themes ]; then
  	mkdir -p ${instdir}${prefix}/share/claws-mail/themes 
  fi && \
  echo "Repairing some plugins' installation..." && \
  /usr/bin/install -c ${objdir}/src/plugins/pgpinline/.libs/pgpinline.dll \
                      ${objdir}/src/plugins/pgpinline/pgpinline.la \
  		   ${instdir}${prefix}/lib/claws-mail/plugins/ && \
  /usr/bin/install -c ${objdir}/src/plugins/pgpmime/.libs/pgpmime.dll \
                      ${objdir}/src/plugins/pgpmime/pgpmime.la \
  		   ${instdir}${prefix}/lib/claws-mail/plugins/ && \
  echo "Installing export library of claws-mail.exe..." && \
  /usr/bin/install -c ${objdir}/src/libclaws-mail.a ${instdir}${prefix}/lib/libclaws-mail.a && \
  echo "Installing additional tool scripts..." && \
  mkdir -p ${instdir}${prefix}/share/claws-mail/tools && 
  for f in ${srcdir}/tools/*; do
     [ -f $f ] &&  /usr/bin/install $f ${instdir}${prefix}/share/claws-mail/tools
  done && \
  rm -f ${instdir}${prefix}/share/claws-mail/tools/Makefile.* && \
  mkdir -p ${instdir}${prefix}/share/claws-mail/tools/kdeservicemenu && \
  /usr/bin/install ${srcdir}/tools/kdeservicemenu/* ${instdir}${prefix}/share/claws-mail/tools/kdeservicemenu && \
  rm -f ${instdir}${prefix}/share/claws-mail/tools/Makefile.* && \
  mkdir -p ${instdir}${prefix}/share/pixmaps && \
  echo "Installing additional documents..." && \
  /usr/bin/install ${srcdir}/*.png ${instdir}${prefix}/share/pixmaps/ && \
  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} ${prefix}/share/doc/Cygwin ; 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 -r gzip -q 2>/dev/null ; \
  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 -r gzip -q 2>/dev/null; \
  fi && \
  templist="" && \
  for fp in ${install_docs} ; do \
    case "$fp" in \
      */) templist="$templist `cd ${srcdir} && find $fp -type f`" ;;
      *)  for f in ${srcdir}/$fp ; do \
            if [ -f $f ] ; then \
              templist="$templist $f"; \
            fi ; \
          done ;; \
    esac ; \
  done && \
  if [ ! "x$templist" = "x" ]; then \
    /usr/bin/install -m 644 $templist \
         ${instdir}${prefix}/share/doc/${PKG} ; \
  fi && \
  if [ -f ${srcdir}/share/doc/${BASEPKG}/ChangeLog* ]; then\
    gzip ${srcdir}/share/doc/${BASEPKG}/ChangeLog* ;\
  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}.README ; \
  elif [ -f ${srcdir}/CYGWIN-PATCHES/README ] ; then \
    /usr/bin/install -m 644 ${srcdir}/CYGWIN-PATCHES/README \
      ${instdir}${prefix}/share/doc/Cygwin/${PKG}.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 &&
  if [ -f ${srcdir}/CYGWIN-PATCHES/manifest.lst ] ; then
    if [ ! -d ${instdir}${sysconfdir}/preremove ]; then
      mkdir -p ${instdir}${sysconfdir}/preremove ;
    fi &&
    /usr/bin/install -m 755 ${srcdir}/CYGWIN-PATCHES/manifest.lst \
      ${instdir}${sysconfdir}/preremove/${PKG}-manifest.lst ;
  fi )
}
strip() {
  (cd ${instdir} && \
  find . -name "*.dll" -or -name "*.exe" | xargs -r strip 2>&1 ; \
  true )
}
list() {
  (cd ${instdir} && \
  find . -name "*" ! -type d | sed 's%^\.%  %' ; \
  true )
}
depend() {
  (cd ${instdir} && \
  find ${instdir} -name "*.exe" -o -name "*.dll" | xargs -r cygcheck | \
  sed -e '/\.exe/d' -e 's,\\,/,g' | sort -bu | xargs -r -n1 cygpath -u \
  | xargs -r cygcheck -f | sed 's%^%  %' | sort -u ; \
  true )
}
pkg() {
  (cd ${instdir} && \
  echo "Packaging ${bin_pkg_name}..." && \
  tar cjf ${bin_pkg} ${prefix_noslash}/bin \
        ${prefix_noslash}/lib/claws-mail/plugins \
        ${prefix_noslash}/share/doc \
        ${prefix_noslash}/share/man \
        ${prefix_noslash}/share/pixmaps \
	${prefix_noslash}/share/doc/claws-mail/manual/en \
        ${prefix_noslash}/share/claws-mail/themes \
        ${prefix_noslash}/share/claws-mail/tools  && \
  echo "Packaging ${devel_pkg_name}..." && \
  tar cjf ${topdir}/${devel_pkg_name} ${prefix_noslash}/include \
   	${prefix_noslash}/lib/lib*.a \
   	${prefix_noslash}/lib/pkgconfig && \
  echo "Packaging ${i18n_pkg_name}..." && \
  tar cjf ${topdir}/${i18n_pkg_name} \
   	--exclude ${prefix_noslash}/share/claws-mail/manual/en \
	${prefix_noslash}/share/locale \
  	${prefix_noslash}/share/doc/claws-mail/manual \
  	)
}
mkpatch() {
  (cd ${srcdir} && \
  find . -name "autom4te.cache" | xargs -r rm -rf ; \
  cd ${srcinstdir} && unpack ${src_orig_pkg} && \
  mv ${BASEPKG} ${topdir}/${BASEPKG}-orig && \
  cd ${topdir} && \
  diff -urNp -x '.build' -x '.inst' -x '.sinst' -x '*.orig' -x '*.rej' -x 'tags' -x '*~' \
    ${BASEPKG}-orig ${BASEPKG} > \
    ${src_patch_name} ; \
  rm -rf ${BASEPKG}-orig )
}
# Note: maintainer-only functionality
acceptpatch() {
  cp --backup=numbered ${srcinstdir}/${src_patch_name} ${topdir}
}
spkg() {
  (mkpatch && \
  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} * )
}
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
    help|--help)	help ; STATUS=$? ;;
    version|--version)	version ; STATUS=$?;;
    prep)		prep ; STATUS=$? ;;
    patch)              patch; STATUS=$? ;;
    mkdirs)		mkdirs ; STATUS=$? ;;
    conf)		conf ; STATUS=$? ;;
    config)		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_part1 && install_part2;  STATUS=$? ;;
    install_part1)	install_part1 ; STATUS=$? ;;
    install_part2)      install_part2 ; STATUS=$? ;;
    list)		list ; STATUS=$? ;;
    depend)		depend ; STATUS=$? ;;
    strip)		strip ; STATUS=$? ;;
    package)		pkg ; STATUS=$? ;;
    pkg)		pkg ; STATUS=$? ;;
    mkpatch)		mkpatch ; STATUS=$? ;;
    acceptpatch)	acceptpatch ; STATUS=$? ;;
    src-package)	spkg ; STATUS=$? ;;
    spkg)		spkg ; STATUS=$? ;;
    finish)		finish ; STATUS=$? ;;
    checksig)		checksig ; STATUS=$? ;;
    first)		mkdirs && spkg && finish ; STATUS=$? ;;
    almostall)		checksig && prep && conf && build && install && \
			strip && pkg && spkg ; 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

