#!/bin/sh



usage(){
    printf "$0 [ -t <output tarfile> ] <input dump>\n"
    exit 2
}


args=`/usr/bin/getopt t: $*`
if [ $? -ne 0 ]; then
    usage
fi

for i; do
    case $i
    in
        -t) OTAR=$(realpath $2); 
            shift;shift;;
        *) IDUMP=$(realpath $1); 
            shift;break;;
    esac
done

[ -z "$OTAR" ] && OTAR="${IDUMP%.dump}.tar.gz"

echo $OTAR
echo $IDUMP

dumpsize=$(/usr/bin/stat -f %z $IDUMP)
mdsize=$(((($dumpsize/100*20)+$dumpsize)/1024/1024))
export TMPDIR=/place/tmp/
tmpdir=$(/usr/bin/mktemp -d -t `basename $0`)

echo "Creating memory disk"
dd if=/dev/zero of=${tmpdir}/mdfile bs=1m count=$mdsize > /dev/null 2>&1
mdn=$(mdconfig -a -t vnode -f ${tmpdir}/mdfile); mdn=${mdn#md}

newfs -o time -m 9 /dev/md$mdn 
mount /dev/md$mdn /mnt/tmp
last=`pwd`

echo "Restoring dump"
cd /mnt/tmp

restore -r  -f ${IDUMP} > /dev/null
tar czf $OTAR .
cd $last
umount /mnt/tmp
mdconfig -d -u $mdn

echo $OTAR
rm -Rf $tmpdir
