#!/bin/sh

VAULT=@@RELEASE_VAULT@@

GNUTAR=/usr/latest/bin/tar
BLOCKSIZE=62k

if [ $# != 5 ] ; then
	echo Usage: $0 release bundle host target /dev/norewindtape
	exit 1
fi

set -e

release=$1
bundle=$2
host=$3
target=$4
tape=$5

RELDIR=$VAULT/$release/$bundle/$host
SRCDIR=$VAULT/$release/$bundle

if [ ! -f $RELDIR/${target}/Install ]; then
	echo "$0: can't find install script: $RELDIR/${target}/Install" 1>&2
	exit 1
fi

if [ ! -f $RELDIR/${target}.tar.Z ]; then
	echo "$0: can't find executable image: $RELDIR/${target}.tar.Z" 1>&2
	exit 1
fi

if [ ! -f $SRCDIR/src.tar.Z ]; then
	echo "$0: can't find source image: $SRCDIR/src.tar.Z" 1>&2
	exit 1
fi

# Rewind and retention tape.
mt -f ${tape} ret
mt -f ${tape} rew

# 1st hunk, the Install script
cd $RELDIR
$GNUTAR -cf ${tape} -C $RELDIR/${target} Install

# 2nd hunk, the executables
dd conv=sync if=$RELDIR/${target}.tar.Z of=${tape} obs=${BLOCKSIZE}

# 3rd hunk, the sources
dd conv=sync if=$SRCDIR/src.tar.Z of=${tape} obs=${BLOCKSIZE}

mt -f ${tape} rewind
tar tvvf ${tape}

mt -f ${tape} rewind
mt -f ${tape} fsf 1
dd if=${tape} ibs=${BLOCKSIZE} | compress -d | /bin/tar tvvf -

mt -f ${tape} rewind
mt -f ${tape} fsf 2
dd if=${tape} ibs=${BLOCKSIZE} | compress -d | /bin/tar tvvf -

echo "this tape for $host $target is written"

exit 0
