#!/usr/bin/perl

# default values for arguments of command line ########################

$out_file = "dump_file_$$";
$rp_script = "sh realplay";

# function definitions ################################################

sub help_msg
{
	print STDERR "usage: $0 [-d <y|n>] [-p] [-o <output file>] " .
		     "[<realplay script>] <URL>\n" .
	"-d <y|n>          Use -d y, if you want me to run the\n" .
	"                  esd daemon without asking you on the way.\n" .
	"                  Use -d n, for not running the esd daemon.\n" .
	"                  If you forgot this option and you wish to\n" .
	"                  terminate the program when the question\n" .
	"                  comes, press CTRL-D instead of y/n\n" .
	"-p                Use this option, if you want to keep\n" .
	"                  a copy of the whole output of strace\n" .
	"                  for debugging. If anything goes wrong,\n" .
	"                  you have a place to see what happened.\n" .
	"                  The file name is out.\$\$. It's a huge\n" .
	"                  file (ca. 5 times as big as the raw-file).\n" .
	"-o <output file>  Your filename will get the extension .raw,\n" .
	"                  if it doesn't have it already.\n" .
	"                  If you don't use this option the output\n" .
	"                  file will be dump_file_\$\$.raw\n" .
	"<realplay script> If it has another name as realplay or a\n" .
	"                  special path, just say it here.\n" .
	"<URL>             No default for this. Only you know!\n" .
	"                  It could be something like this:\n" .
	"rtsp://mm3.rai.it/radio/grr/gr1/2005/gr1_20050317.1300.rm\n";
	exit $_[0];
}

sub usage_msg
{
	print STDERR "usage: $0 [-d <y|n>] [-p] [-o <output file>] " .
		     "[<realplay script>] <URL>\n" .
		     "Type \"$0 --help\" for more details.\n";
	exit $_[0];
}

sub exist_msg
{
	print STDERR "$0: output file $_[0] exists!\n";
	exit $_[1];
}

# options and parameter check #########################################

$copy_strace = 0;
$choice = "?";

usage_msg 1 if( @ARGV < 1 );
while( $_ = shift @ARGV , /^-/ )
{
	if( $_ eq "--" )
	{
		$_ = shift @ARGV;
		last;
	}
	help_msg 0       if( $_ eq "--help" );
	usage_msg 1      if( $_ =~ /^-[^hpod]/ || $_ eq "-" );
	usage_msg 0      if( $_ eq "-h" );
	$copy_strace = 1 if( $_ eq "-p" );
	if( $_ =~ /^-d/ )
	{
		($choice = $_) =~ s/-d//;
		if( $choice eq "" )
		{
			$choice = shift @ARGV;
		}
		usage_msg 1 if( $choice ne "y" && $choice ne "n" );
	}
	if( $_ =~ /^-o/ )
	{
		($out_file = $_) =~ s/-o//;
		if( $out_file eq "" )
		{
			($out_file = shift @ARGV) =~ s/\.[Rr][Aa][Ww]$//;
		}
		usage_msg 1 if( $out_file eq "" );
		$out_file .= ".raw";
		exist_msg $out_file, 2 if( -e $out_file );
	}
}

usage_msg 1 if( $_ eq "" || @ARGV > 1 );
if( @ARGV == 1 ) { $rp_script = "sh $_"; $url = shift @ARGV; }
else { $url = $_; }
usage_msg 1 if( $url eq "" );

# options for strace
$opts="-f -e trace=write -e write=12";

if( $choice eq "?" )
{
	print STDERR "Do you want me to run the esd daemon? (y/n) ";
	if( ($choice = <STDIN>) eq "" )
	{
		print STDERR "\nYou wish to exit the program. Good bye!\n";
		exit 0;
	}
	chomp($choice);
}
system("esd -terminate&") unless( $choice ne "y" );
             # set the preferences of realplay8 in accordance to this

if( $copy_strace )
{
	$copy = "tee out.$$";
	# if you use the option -p you get a copy of strace
}
else
{
	$copy = "cat";
}

open(FILE,">$out_file");
open(STRACE,"strace $opts $rp_script $url 2>&1 | $copy |");

while( <STRACE> )
{
	if( /^ \| [0-9a-fA-F]{5}/ )
	{
		chomp;
		s/^ \| .{7}(.*).{21}$/$1/;
		s/ //g;
		print FILE (pack "H32", $_);
	}
}
