#!/bin/bash

set -e

test $# == 3

URL="http://proj-clhep.web.cern.ch/proj-clhep/DISTRIBUTION/tarFiles"
VER="$2"

UPTAR="clhep-$VER.tgz"
UPDIR="$VER/CLHEP"

ORIGTAR="$3"
ORIGDIR="clhep-$VER"

DESTDIR="$(dirname "$ORIGTAR")"

test ! -f "$DESTDIR/$UPTAR" && wget "$URL/$UPTAR" -O "$DESTDIR/$UPTAR"

test -d "$VER" && rm -rf "$VER"
tar -xf "$DESTDIR/$UPTAR"

mv "$UPDIR" "$ORIGDIR"

test -e "$ORIGTAR" && rm -f "$ORIGTAR"
GZIP=-9 tar --remove-files -zcf "$ORIGTAR" "$ORIGDIR"

rmdir "$VER"

