#!/bin/sh

# This script is used to download the upstream source for xbmc and
# generate it into an orig source tarball for Debian.

USAGE="\n\
This script is used to generate the orig tarball used in building\n\
Debian packages for xbmc. This script will create an orig tarball for\n\
Debian/Ubuntu official archive by default.
Usage: xbmc-get-orig-source [OPTION]\n\
\n\
 -h, --help                 Display this help message.\n\
 --remove-upstream-tarball  Remove the upstream source tarball.\n\
 --make-debian-snapshot     Make tarball for official Debian/Ubuntu archive\n\
                            using latest SVN revision.\n\
 --make-ppa                 Make tarball for XBMC's PPA\n\
 --make-ppa-snapshot        Make tarball for XBMC's PPA using latest SVN\n\
                            revision.\n\
 --use-revision [REV]       Use revision specified in [REV].\n\
 --perform-ppa-upload       Perform PPA upload of the resulting source.\n\
 --dput-host <HOST>         The dput host to use (default team-xbmc-svn-ppa).\n"

# Loop that parses options passed to script
while [ "$#" -gt "0" ]
do
    case "$1" in
        --remove-upstream-tarball)
            REMOVE_UPSTREAM_TARBALL=1
            shift
            ;;
        --make-debian-snapshot)
            LATEST_SVN=1
            shift
            ;;
        --make-ppa)
            PPA_ORIG_TARBALL=1
            shift
            ;;
        --make-ppa-snapshot)
            PPA_ORIG_TARBALL=1
            LATEST_SVN=1
            shift
            ;;
        --use-revision)
            SVN_REVISION="$2"
            shift; shift
            ;;
        --perform-ppa-upload)
            PERFORM_PPA_UPLOAD=1
            shift
            ;;
        --dput-host)
            DPUT_HOST="$2"
            shift; shift
            ;;
        -h|--help|*)
            echo "${USAGE}"
            exit 1
            ;;
    esac
done

# Common variables used to ease maintenance of this script
XBMC_TARBALL=""
XBMC_TARBALL_CHECKSUM=""
if [ -z $SVN_REVISION ]; then
  SVN_REVISION="33745"
fi
EPOCH="2"
XBMC_VERSION="10.0~svn$SVN_REVISION~deb1"
DEB_REVISION="1"
XBMC_SVN_URL="http://xbmc.svn.sourceforge.net/svnroot/xbmc/branches/Dharma"
UBUNTU_SUITES="lucid karmic jaunty hardy maverick"
if [ -z $DPUT_HOST ]; then
  DPUT_HOST="team-xbmc-svn-ppa"
fi

# export xbmc source
get_sources() {
    if [ -n "$LATEST_SVN" ]; then
        SVN_REVISION="$(svn info $XBMC_SVN_URL | \
                        grep "Last Changed Rev" | \
                        sed -e 's/^[^0-9]\+//')"
        XBMC_VERSION="10.0~svn$SVN_REVISION~deb1"
    fi
    svn export -r$SVN_REVISION $XBMC_SVN_URL xbmc-$XBMC_VERSION
}

# This will prepare the sources to create a tarball from
prepare_orig_tarball() {
    # Run bootstrap
    cd xbmc-$XBMC_VERSION
    ./bootstrap
    cd ..

    if [ -z "$PPA_ORIG_TARBALL" ]; then
        # Remove non-free components
        echo "Removing non-free components"
        rm -rf xbmc-$XBMC_VERSION/xbmc/cores/dvdplayer/Codecs/libdvd/libdvdcss
        rm -rf xbmc-$XBMC_VERSION/xbmc/xbmc/lib/UnrarXLib
        rm -rf xbmc-$XBMC_VERSION/xbmc/xbmc/lib/libXBMS

        # Remove components not needed for Linux when using external libs
        echo "Removing components not needed for Linux when using external libs"
        rm -rf xbmc-$XBMC_VERSION/lib/bzip2
        rm -rf xbmc-$XBMC_VERSION/lib/enca
        rm -rf xbmc-$XBMC_VERSION/lib/freetype
        rm -rf xbmc-$XBMC_VERSION/lib/fribidi
        rm -rf xbmc-$XBMC_VERSION/lib/libass
        rm -rf xbmc-$XBMC_VERSION/lib/libcdio
        rm -rf xbmc-$XBMC_VERSION/lib/libcrystalhd
        rm -rf xbmc-$XBMC_VERSION/lib/libcurl-OSX
        rm -rf xbmc-$XBMC_VERSION/lib/libiconv
        rm -rf xbmc-$XBMC_VERSION/lib/liblame
        rm -rf xbmc-$XBMC_VERSION/lib/libmicrohttpd
        rm -rf xbmc-$XBMC_VERSION/lib/libmodplug
        rm -rf xbmc-$XBMC_VERSION/lib/librtmp
        rm -rf xbmc-$XBMC_VERSION/lib/libSDL-OSX
        rm -rf xbmc-$XBMC_VERSION/lib/libssh_win32
        rm -rf xbmc-$XBMC_VERSION/lib/libvpx
        rm -rf xbmc-$XBMC_VERSION/lib/pcre
        rm -rf xbmc-$XBMC_VERSION/libmicrohttpd_win32
        rm -rf xbmc-$XBMC_VERSION/libmysql_win32
        rm -rf xbmc-$XBMC_VERSION/xbmc/cores/dvdplayer/Codecs/liba52
        rm -rf xbmc-$XBMC_VERSION/xbmc/cores/dvdplayer/Codecs/libdts
        rm -rf xbmc-$XBMC_VERSION/xbmc/cores/dvdplayer/Codecs/libfaad2
        rm -rf xbmc-$XBMC_VERSION/xbmc/cores/dvdplayer/Codecs/libmad
        rm -rf xbmc-$XBMC_VERSION/xbmc/cores/dvdplayer/Codecs/libmpeg2
        rm -rf xbmc-$XBMC_VERSION/xbmc/cores/paplayer/FLACCodec/flac-1.2.1
        rm -rf xbmc-$XBMC_VERSION/xbmc/cores/paplayer/ogg
        rm -rf xbmc-$XBMC_VERSION/xbmc/cores/paplayer/vorbisfile
        rm -rf xbmc-$XBMC_VERSION/xbmc/lib/boost
        rm -rf xbmc-$XBMC_VERSION/xbmc/lib/libenca
        rm -rf xbmc-$XBMC_VERSION/xbmc/lib/libmms
        rm -rf xbmc-$XBMC_VERSION/xbmc/lib/libPython/Python
        rm -rf xbmc-$XBMC_VERSION/xbmc/lib/libsamplerate
        rm -rf xbmc-$XBMC_VERSION/xbmc/lib/zlib
    fi

    # Remove temp files and other cruft from source tarball
    # The find command snippet here was taken from debhelper's dh_clean command
    # with some modification to delete more unneeded files.
    echo "Removing temp files and other cruft from source tarball"
    find xbmc-$XBMC_VERSION \( \( -type f -a \
        \( -name '#*#' -o -name '.*~' -o -name '*~' -o -name DEADJOE \
        -o -name '*.orig' -o -name '*.rej' -o -name '*.bak' \
        -o -name '.*.orig' -o -name .*.rej -o -name '.SUMS' \
        -o -name TAGS -o \( -path '*/.deps/*' -a -name '*.P' \) \
        -o -name config.status -o -name config.cache -o -name config.log \
        \) -exec rm -f "{}" \; \) -o \
        \( -type d -a -name autom4te.cache -prune -exec rm -rf "{}" \; \) \)
    rm xbmc-$XBMC_VERSION/.gitignore

    # Remove empty directories
    echo "Removing empty directories"
    find xbmc-$XBMC_VERSION -type d -empty -delete
}

# Function to create tarball
create_tarball() {
    echo "Create orig tarball"
    tar -czf xbmc_$XBMC_VERSION.orig.tar.gz \
        xbmc-$XBMC_VERSION/
}

# Function that performs a PPA upload. It will ensure the version is less than
# the version specified in <XBMC_VERSION>-<DEB_REVISION> and upload a package
# for each supported Ubuntu suite.
perform_ppa_upload() {
    echo "Performing PPA upload."
    cd xbmc-$XBMC_VERSION
    cp -r tools/Linux/packaging/debian .
    for SUITE in $UBUNTU_SUITES; do
      local N="1";

      # Update changelog for uploads to PPA.
      dch --force-distribution -D $SUITE \
        -b -v "${EPOCH}:${XBMC_VERSION}-${DEB_REVISION}~0${SUITE}${N}" \
        "Upload to XBMC PPA for Ubuntu $SUITE." 2>/dev/null

      # Create source package
      if [ "$SUITE" = "lucid" ]; then
        # First suite should always include source tarball
        dpkg-buildpackage -rfakeroot -us -uc -S -sa
      else
        # source format 3.0 (quilt) not supported in suites < lucid
        echo "1.0" >debian/source/format
        dpkg-buildpackage -rfakeroot -us -uc -S -sd
      fi

      # Clean changelog
      perl -e 'local $/;
        my @records, $fh;
        open $fh, "<", "debian/changelog";
        @records = split /^\b/msxg, <$fh>;
        close $fh;
        open $fh, ">", "debian/changelog";
        shift @records;
        foreach my $record (@records)
        {
          print $fh "$record";
        }'

      # Sign source package and upload.
      local SOURCE_CHANGES="xbmc_${XBMC_VERSION}-${DEB_REVISION}~0${SUITE}${N}_source.changes"
      cd ..
      debsign "$SOURCE_CHANGES"
      dput "$DPUT_HOST" "$SOURCE_CHANGES"
      cd xbmc-$XBMC_VERSION
    done
}

get_sources
prepare_orig_tarball
create_tarball
if [ -n $PERFORM_PPA_UPLOAD ]; then
  perform_ppa_upload
fi
