#!/bin/sh

# bail out with the first problem
set -e

# script to download and repack source package of opencaster

PKG=`dpkg-parsechangelog | awk '/^Source/ { print $2 }'`
echo "I: PKG: $PKG"

VERSION=`uscan --verbose --force-download | \
    grep "Newest version on remote site is .* local version is .*" | \
    head -n 1 | \
    sed "s/Newest version on remote site is \([-0-9.]\+\),.*/\1/"`
echo "I: version: $VERSION"

# mkdir -p does not fail when directories exist already
mkdir -p ../tarballs
cd ../tarballs
TARBALLDIR=`pwd`

UPSTREAMDIR=${PKG}-${VERSION}
echo "I: upstreamdir: ${UPSTREAMDIR}"

mkdir -p ${UPSTREAMDIR}
cd ${UPSTREAMDIR}

tar --strip-components=1 -zxf ../../${PKG}_${VERSION}.orig.tar.gz

# remove all named pipes
echo "I: `pwd`"
#not anymore: find -type p|xargs rm
# remove all class files
rm -rf tutorials/mhp/ocdir1
rm -rf tutorials/mhp-streamevents/ocdir2
# remove png without preferred form of modification
rm -f tutorials/encodingHD/logo_tv.png
rm -f tutorials/encoding/logo_tv.png
cd ..

GZIP="--best --no-name" tar -czf "$PKG"_"$VERSION"+dfsg.orig.tar.gz "${UPSTREAMDIR}"
rm -rf "${UPSTREAMDIR}"

