#!/bin/sh

# Script used to generate the orig source tarball for firmware-crystalhd.

FIRMWARE_CRYSTALHD_GIT_URL="git://linuxtv.org/jarod/crystalhd.git"
FIRMWARE_CRYSTALHD_GIT_COMMIT="fdd2f19ac739a3db1fd7469ea19ceaefe0822e5a"
DATE_RETRIEVED="20120110"
COMMIT_SHORT_FORM="$(echo $FIRMWARE_CRYSTALHD_GIT_COMMIT | \
                     sed -e 's/^\([[:xdigit:]]\{,7\}\).*/\1/')"
FIRMWARE_CRYSTALHD_VERSION="0.0~git${DATE_RETRIEVED}.${COMMIT_SHORT_FORM}"

git clone "$FIRMWARE_CRYSTALHD_GIT_URL" "firmware-crystalhd-${FIRMWARE_CRYSTALHD_VERSION}"
cd "firmware-crystalhd-${FIRMWARE_CRYSTALHD_VERSION}"
git checkout "$FIRMWARE_CRYSTALHD_GIT_COMMIT"
rm -rf .git driver examples export-driver-for-staging.sh filters include \
  linux_lib
find . -name .gitignore -delete
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 "firmware-crystalhd-${FIRMWARE_CRYSTALHD_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 "{}" \; \) \)

# Remove empty directories
echo "Removing empty directories"
find "firmware-crystalhd-${FIRMWARE_CRYSTALHD_VERSION}" -type d -empty -delete

# Generate tarball
tar --exclude-vcs -czf "firmware-crystalhd_${FIRMWARE_CRYSTALHD_VERSION}.orig.tar.gz" \
  "firmware-crystalhd-${FIRMWARE_CRYSTALHD_VERSION}/"
