#!/bin/sh
# -*- perl -*-
# This code allows us to start perl from our path or an environment variable
# rather than hardcoding a path into the #! line.  It works from sh or csh.
(exit $?0) && eval 'exec ${QPERLQ-perl} -x $0 ${1+"$@"}'
if (! $?QPERLQ) setenv QPERLQ perl
exec $QPERLQ -x $0 $argv:q

#!/usr/local/bin/perl -w
#
# $Id: double_tar,v 1.2 1996/01/25 21:40:01 ejb Exp $
# $Source: /home/ejb/scripts/RCS/double_tar,v $
# $Author: ejb $
#

$whoami = ($0 =~ m,([^/]*)$,) ? $1 : $0;

$default_size = 1474560;	# 1.4 megabytes
&usage if ((@ARGV < 1) || (@ARGV > 2));

$file = shift(@ARGV);
$size = $ARGV[0] || $default_size;

END { defined($tmpfile) && unlink $tmpfile; }
$SIG{'INT'} = $SIG{'HUP'} = $SIG{'TERM'} = sub { exit 0; };

$tmpfile = "/tmp/dt.$$";
(system("tar cvf $tmpfile $file 1>&2") == 0) ||
    die "$whoami: tar cvf $tmpfile $file failed\n";

@stat = stat $tmpfile or die "$whoami: stat $tmpfile failed: $!\n";
$tsize = $stat[7];
$size /= 2;
die "$tsize > $size" if ($tsize > $size);
while ($tsize <= $size / 2)
{
    $size /= 2;
}
$leftover = $size - $tsize;
$buf = "\000" x $leftover;
open(TMP, ">>$tmpfile") || die "$whoami: can't append $tmpfile: $!\n";
print TMP $buf;
close(TMP);
system("cat $tmpfile $tmpfile");


sub usage
{
    print STDERR <<EOF;
Usage: $whoami file size
Make a tarfile containing file, double it, and put the result to stdout.
The tarfile is written once, padded with zeroes, and written again.
The size of the padding is chosen such that each tarfile occupies
a power of two fraction of size.  The default size is $default_size.
EOF
    ;
    exit 1;
}
