#!/bin/sh

# copyright 2004 vagrant@freegeek.org, distributed under the terms of the
# GNU General Public License version 2 or any later version.

# TODO get defaults from a configuration file.
# FIXME fix buildbot, so autobuild and do_cvs defaults can be set reasonably
do_cvs="false"
autobuild="true"
do_clean="true"
do_checksyntax="true"
while [ -n "$1" ]; do
  case $1 in
    --cvs) do_cvs="true" ;;
    --nocvs) do_cvs="false" ;;
    --autobuild) autobuild="true" ;;
    --noautobuild|--noauto) autobuild="false" ;;
    --clean) do_clean="true" ;;
    --noclean) do_clean="false" ;;
    --checksyntax) do_checksyntax="true" ;;
    --nochecksyntax) do_checksyntax="false" ;;
  esac
  shift
done

if [ "$do_cvs" = "true" ]; then
  echo "updating cvs dir"
  cvs update -P -d
fi

base_dir="$(pwd)"

# try to automatically generate the changelog before building package
if [ "$do_cvs" = "true" ] && [ -n "$(which cvs2cl)" ]; then
  echo "updating ChangeLog"
  cvs2cl
fi

if [ ! -r ChangeLog ]; then
  echo  "creating dummy ChangeLog- install cvs2cl for normal changelog"
  echo "dummy changelog: $(date -R)" > ChangeLog
fi

version="$(head -n 1 distrib/debian/changelog | awk '{print $2}' | sed -e 's/(//g' | sed -e 's/)//g')"
if [ "$autobuild" = "true" ]; then
  # TODO get build version from buildbot, rather than .%H.%M ?
  date="$(date --utc +%y%m%d.%H%M%S)"
  mv -f distrib/debian/changelog distrib/debian/changelog.bak
  # FIXME only change the newest upload queue to "autobuild"
  echo "modifying changelog for autobuild:"
  cat distrib/debian/changelog.bak | sed s/$version/$version.$date/ | sed -e 's/) .*;/) autobuild;/g' > distrib/debian/changelog.new && \
    mv -f distrib/debian/changelog.new distrib/debian/changelog
  head -n1 distrib/debian/changelog
  version="$version.$date"
fi

package="$(head -n 1 distrib/debian/changelog | awk '{print $1}')"
package_dir="$package"-"$version"
source_tarball="$package"_"$version".orig.tar.gz

echo "purging old temp directory"
rm -rf tmp

echo "creating directory for source tarball: $package_dir"
mkdir -p tmp/"$package_dir"
# copy the directories with tar...
tar cpf - --exclude=CVS --exclude=tmp . | tar xpf - -C tmp/"$package_dir"

if [ "$autobuild" = "true" ]; then
  echo "restoring original changelog"
  mv -vf distrib/debian/changelog.bak distrib/debian/changelog
fi

cd tmp

echo "creating source tarball: $source_tarball"
GZIP="--best" tar czpf "$source_tarball" "$package_dir"

cd "$package_dir"

# copy debian directory
test -d debian && rm -rf debian
echo "copying debian directory..."
cp -af distrib/debian debian

fakeroot debian/rules clean

if [ "true" = "$do_checksyntax" ]; then
  if [ -x ./tests/checksyntax ] ; then
    ./tests/checksyntax || exit $?
  elif [ -n "$(which checksyntax)" ]; then
    checksyntax || exit $?
  else
    echo "checksyntax not found, skipping syntax checker..."
  fi
fi

if [ -n "$(which lintian)" ]; then
  debuild_opts="$debuild_opts --lintian" 
  # TODO pass options to lintian, keep woody and sarge compatibility
else
  echo "WARNING: lintian not found. disabling in debuild will not call."
  debuild_opts="$debuild_opts --no-lintian"
fi

if [ -n "$(which linda)" ]; then
  debuild_opts="$debuild_opts --linda"
  # TODO pass options to linda
else
  echo "WARNING: linda not found"
  # FIXME disable linda if debuild doesn't support it
fi

debuild -rfakeroot $debuild_opts -sa -uc -us
status="$?"

# clean up, unless told otherwise
test "$do_clean" = "true" && fakeroot debian/rules clean

cd "$base_dir"
if [ "$autobuild" = "true" ] && [ "$status" = "0" ] && [ -n "$(which dput)" ]; then
  # FIXME it's pretty dumb to assume the user, but something is borked and this might fix it.
  test -z "$USER" && export USER="builder"
  # TODO move dput into separate script
  for changes in tmp/*.changes ; do
    dput_opts="-u -f"
    if [ -r tests/autobuild.dput.cf ]; then
      dput_opts="$dput_opts -c tests/autobuild.dput.cf"
    fi
    dput $dput_opts autobuild "$changes"
    echo "dput status: $?"
  done
fi

exit $status
