#!/bin/sh

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

# TODO: support svk ?

# TODO get defaults from a configuration file.

# set default to UTC, for consistancy.
export TZ=UTC

date_version() {
  date --utc +%y%m%d
}

long_date_version() {
  date --utc +%y%m%d+%H%M%S
}

while [ -n "$1" ]; do
  case $1 in
    --cvs) do_cvs="true" ;;
    --nocvs) do_cvs="false" ;;
    --svn) do_svn="true" ;;
    --nosvn) do_svn="false" ;;
    --clean) do_clean="true" ;;
    --noclean) do_clean="false" ;;
    --checksyntax) do_checksyntax="true" ;;
    --nochecksyntax) do_checksyntax="false" ;;
    --lintian) do_lintian="true" ;;
    --nolintian) do_lintian="false" ;;
    --linda) do_linda="true" ;;
    --nolinda) do_linda="false" ;;
    --dist) dist="$2"
    shift ;; 
    --distrib) distrib="$2" 
    shift ;;
    --date-version) extra_version="$(date_version)" ;;
    --long-date-version) extra_version="$(long_date_version)" ;;
    --extra-version) extra_version="$2"
    shift ;; 
    --upload-queue) upload_queue="$2"
    shift ;; 
  esac
  shift
done

# set default values if unset
test -z "$do_cvs" && do_cvs="true"
test -z "$do_clean" && do_clean="true"
test -z "$do_checksyntax" && do_checksyntax="true"
test -z "$do_lintian" && do_lintian="true"
test -z "$do_svn" && do_svn="false"
test -z "$dist" && dist="woody"
test -z "$distrib" && distrib="distrib/debian"

if [ "$do_cvs" = "true" ]; then
  if [ -z "$(which cvs)" ]; then
    echo "ERROR: cvs requested, but not installed"
    exit 1
  fi
  echo "updating cvs dir"
  cvs -q update -P -d
elif [ "$do_svn" = "true" ]; then
  if [ -z "$(which svn)" ]; then
    echo "ERROR: svn requested, but not installed"
    exit 1
  fi
  echo "updating svn dir"
  svn up
fi

if [ "$dist" = "sarge" ]; then
  # newer debhelper handles conf-files more simply.
  rm -f $distrib/*.conffiles
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"
  cvs2cl
fi

if [ "$do_svn" = "true" ]; then
  echo "updating ChangeLog: svn log"
  test -f ChangeLog && mv -f ChangeLog ChangeLog.old
  svn log -v > ChangeLog
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/changelog | awk '{print $2}' | sed -e 's/(//g' -e 's/)//g')"

if [ -n "$extra_version" ] || [ -n "$upload_queue" ]; then
  cp $distrib/changelog $distrib/changelog.orig
  restore_changelog="true"
fi

if [ -n "$extra_version" ]; then
  new_version="$version+$extra_version"
  echo "modifying changelog: new version: $new_version"
  cp -f $distrib/changelog $distrib/changelog.bak
  cat $distrib/changelog.bak | sed s/$version/$new_version/ > $distrib/changelog.new && \
    mv -f $distrib/changelog.new $distrib/changelog
  head -n1 $distrib/changelog
  version="$new_version"
fi

if [ -n "$upload_queue" ]; then
  echo "modifying changelog: upload queue: $upload_queue"
  cp -f $distrib/changelog $distrib/changelog.bak
  cat $distrib/changelog.bak | sed -e "s/$version) .*;/$version) $upload_queue;/g" > $distrib/changelog.new && \
    mv -f $distrib/changelog.new $distrib/changelog
  head -n1 $distrib/changelog
fi

package="$(head -n 1 $distrib/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=.svn --exclude=tmp . | tar xpf - -C tmp/"$package_dir"

if [ "$restore_changelog" = "true" ]; then
  echo "restoring original changelog"
  mv -vf $distrib/changelog.orig $distrib/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

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 [ -z "$debuild_opts" ]; then
  if [ "$dist" = "sarge" ]; then
    debuild_opts="-rfakeroot -ICVS -I.svn -sa -uc -us"
  else
    debuild_opts="-rfakeroot -sa -uc -us"
  fi
fi

if [ "true" = "$do_lintian" ] && [ -n "$(which lintian)" ]; then
  debuild_opts="--lintian $debuild_opts" 
  # TODO pass options to lintian, keep woody and sarge compatibility
else
  if [ "true" = "$do_lintian" ]; then
    echo "WARNING: lintian not found"
  fi
  echo "NOTE: setting --no-lintian in debuild_opts"
  debuild_opts="--no-lintian $debuild_opts"
fi

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

debuild $debuild_opts
status="$?"

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

# return to where we started
cd "$base_dir"

exit $status
