#!/bin/bash
#
# Upload packages to launchpad. Note: you must configure dput for tvh-X
# as target PPA, and you should also define DEBFULLNAME and DEBEMAIL 
# environment variables
#

# Terminate
function die
{
  echo $*
  exit 1
}

# CMD
CMD=$(basename $0)

# Configuration
TVH_ROOT=$(cd $(dirname $0)/..; pwd)
[ -z "$TVH_DIST" ] && TVH_DIST="lucid natty oneiric precise quantal"
[ -z "$TVH_ARCH" ] && TVH_ARCH="i386 amd64"

# Options
[ ! -z "$1" ] && REL=$1 || REL=master
[ ! -z "$2" ] && PPA=$2 || PPA=unstable

# Setup
cd $TVH_ROOT || exit 1
git checkout $1 && git checkout . || exit 1
NOW=`date -R`
CHANGELOG=$TVH_ROOT/debian/changelog
VERFILE=$TVH_ROOT/src/version.c

# Create version file
VER=$($TVH_ROOT/support/version $VERFILE)

# Fetch scan files
./support/getmuxlist || die "failed to fetch dvb-scan files"

# For each distro
for d in $TVH_DIST; do
  V=${VER}~${d}

  # Create changelog
  $TVH_ROOT/support/changelog "$CHANGELOG" "$d" "$VER" || exit 1
  
  # Build source package
  dpkg-buildpackage -I.git* -S -sgpg -pgpg || exit 1

  # Build
  if [ "$CMD" == "pbuilder" ]; then
  
    for a in $TVH_ARCH; do
      pbuilder-dist $d $a ../tvheadend_${V}.dsc
    done

  # Upload
  else
    dput $DPUT_OPT tvh-${PPA} ../tvheadend_${V}_source.changes || exit 1
  fi

done

# Cleanup
git checkout .