#!/bin/sh

set -e

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

LIBSHAIRPORT_GIT_URL="git://github.com/amejia1/libshairport.git"
LIBSHAIRPORT_GIT_COMMIT="cbed0c1cf724ec167461ae683d1817954a07ecd2"
DATE_RETRIEVED="20120510"
COMMIT_SHORT_FORM="$(echo $LIBSHAIRPORT_GIT_COMMIT | \
                     sed -e 's/^\([[:xdigit:]]\{,7\}\).*/\1/')"
LIBSHAIRPORT_VERSION="1.2.1~git${DATE_RETRIEVED}.${COMMIT_SHORT_FORM}"

rm -rf "libshairport-${LIBSHAIRPORT_VERSION}"
git clone "$LIBSHAIRPORT_GIT_URL" "libshairport-${LIBSHAIRPORT_VERSION}"
cd "libshairport-${LIBSHAIRPORT_VERSION}"
git checkout "$LIBSHAIRPORT_GIT_COMMIT"
autoreconf -vif
cd ..

# 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 libshairport-${LIBSHAIRPORT_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 -rf libshairport-${LIBSHAIRPORT_VERSION}/.git
rm -f libshairport-${LIBSHAIRPORT_VERSION}/.gitignore

# Remove empty directories
echo "Removing empty directories"
find libshairport-${LIBSHAIRPORT_VERSION} -type d -empty -delete

tar --exclude-vcs -czf "libshairport_${LIBSHAIRPORT_VERSION}.orig.tar.gz" \
  "libshairport-${LIBSHAIRPORT_VERSION}/"
