#!/bin/sh

set -e

# Script used to generate the orig source tarball for gpac.

GPAC_SVN_URL="svn://svn.code.sf.net/p/gpac/code/trunk/gpac"
GPAC_BASE_VER="${GPAC_BASE_VER:-0.5.0}"
GPAC_VERSION="${GPAC_BASE_VER}+svn${GPAC_SVN_REVISION}"

svn export -r "$GPAC_SVN_REVISION" "$GPAC_SVN_URL" "gpac-${GPAC_VERSION}" 1>/dev/null 2>&1

# 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" 1>/dev/null 2>&1
find gpac-${GPAC_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 -o -name .svn \
      \) -prune -exec rm -rf "{}" \; \
    \) \
  \)

# Make ./configure executable
chmod 755 gpac-${GPAC_VERSION}/configure

# Remove empty directories
echo "Removing empty directories" 1>/dev/null 2>&1
find gpac-${GPAC_VERSION} -type d -empty -delete

BZIP2=-9f tar --exclude-vcs -cjf "gpac_${GPAC_VERSION}.orig.tar.bz2" \
  "gpac-${GPAC_VERSION}/"
rm -rf "gpac-${GPAC_VERSION}/"
echo "--upstream-version ${GPAC_VERSION} gpac_${GPAC_VERSION}.orig.tar.bz2"
