#!/bin/sh

set -e

ORIG=$1
CURDIR=`pwd`
WORKDIR=`dirname $ORIG`
ORIGFILE=`basename $ORIG`
VERSION=`echo "$ORIGFILE" | sed "s/^why-\([0-9\.]\+\)\.tar\.gz$/\1/"`

cd $WORKDIR

safe_remove ()
{
  rm -rf $1 || echo $1 not found
}

if test -d why-$VERSION; then
  echo "Directory why-$VERSION exists"
  exit 1
fi

tar xzf $ORIGFILE

safe_remove why-$VERSION/doc/manual.ps

tar czf why_$VERSION.dfsg.orig.tar.gz why-$VERSION/
rm -rf why-$VERSION

cd $CURDIR
