#!/bin/sh

usage() {
cat <<EOF
xp - Uncompress one or more source to tarball directory

usage: xp <path-to-tar.bz2>

Examples:
   xp src/cli/CORE/vasm2-5.0-src-vl5.tar.bz2
   xp src/cli/CORE/*.tar.bz2
EOF
}

error()
{
    echoc "ERROR: $1" red
    cd $CWD
    exit 1
}


if [ -z "$1" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
    usage
    exit 0
fi

. ./config.core

## Uncompress packages
CWD=`pwd`
while [ "$1" ] ; do
    SRC=$1
    if [ -f $SRC ] && [ "${SRC##*.}" = "bz2" ]; then
	echo
	DNAME=`dirname $SRC`
	cd $DNAME
	GLONG=`pwd`
	GSHORT=${GLONG#$SRC_BASE/}
	echo "Compressed file = $GSHORT/`basename $SRC`"
	mkdir -p $TAR_BASE/$GSHORT || error "Cannot make $TAR_BASE/$GSHORT"
	tar -C $TAR_BASE/$GSHORT -xjf $SRC || error "Cannot extract $SRC"
	echoc "Extracted to $TAR_BASE/$GSHORT" green
    fi
    shift
done;
