#!/usr/bin/perl

($a1, $mediatype) = split(/\?/, $ARGV[0]);
$fname = $ARGV[1];

$version = "vers1";
$session_name = "";
$session_info = "";
$url = "";
$owner = "";
$session_id = 0;
$session_version = 0;
$org_address = "";
$email_address = "";
$phone_number = "";
$multicast_address = "";
$ttl = "";
$start_time = "";
$stop_time = "";
$encryption_key = "";
$portnum = "";
$media_id = 0;
$media_format = "";
undef @attrib;

$media_file = "/usr/local/lib/netscape/mbone-fmt.txt";
$current_media = "";

open(ANNOUNCE, "<$fname") || &error_block("Cannot open $fname.");
$i = 0;
while (<ANNOUNCE>) {
    chop;
    if ($i == 0) {
	$version = $_;	# vers1, vers2, radio
	$i++;
	next;
    }
    ($control, $value) = split(/=/);
    if ($control eq "s") {
	$session_name = $value;
    }
    elsif ($control eq "i") {
	if ( ($version eq "vers1") || ($version eq "vers2") ) {
	    ($url) = ($value =~ m#(ftp://[^>\s]*|http://[^>\s]*)#i);
	}
	$session_info = $value;
    }
    elsif ($control eq "u") {
	$url = $value;
    }
    elsif ($control eq "o") {
	if ($version eq "vers1") {
	    $owner = $value;
	}
	else {
	    ($owner, $session_id, $session_version, $a1, $a2, $a3) =
							split(/\s+/, $value);
	    if ($a1 eq "IP4") {
		$org_address = $a2;
	    }
	    elsif ( ($a1 eq "IN") && ($a2 eq "IP4") ) {
		$org_address = $a3;
	    }
	    else {
		### Error
		&error_block("Cannot parse packet: $value");
	    }

	}
    }
    elsif ($control eq "e") {
	$email_address = $value;
    }
    elsif ($control eq "p") {
	$phone_number = $value;
    }
    elsif ($control eq "h") {
	$org_address = $value;
    }
    elsif ($control eq "c") {
	if ($multicast_address ne "") {
	    # This is the control info related to individual media
	    if ( ($current_media eq $mediatype) ||
		 (($current_media eq "whiteboard") && ($mediatype eq "wb")) ) {
	    }
	    else {
		next;
	    }

	}
	if ($version eq "vers1") {
	    # address ttl start-time stop-time
	    ($multicast_address, $ttl, $start_time, $stop_time) =
							split(/\s+/, $value);
	}
	elsif ($version eq "vers2") {
	    # IP4 address ttl
	    # IP4 address/ttl
	    # IN IP4 address ttl
	    # IN IP4 address/ttl

	    if ($value =~ /^IN\s+IP4/) {
		($a1, $a2, $multicast_address, $ttl) =
						split(/[\/\s]+/, $value);
	    }
	    elsif ($value =~ /^IP4/) {
		($a1, $multicast_address, $ttl) = split(/[\/\s]+/, $value);
	    }
	    else {
		### Error
		&error_block("Cannot parse packet: $value");
	    }
	}
	else {
	    ($a1, $multicast_address, $ttl) = split(/[\/\s]+/, $value);
	}
    }
    elsif ($control eq "b") {
	# bandwidth modifier
	next;
    }
    elsif ($control eq "t") {
	if ($version eq "vers2") {
	    ($start_time, $stop_time) = split(/\s+/, $value);
	}
    }
    elsif ($control eq "k") {
	$encryption_key = $value;
    }
    elsif ($control eq "m") {
	if ($version eq "vers1") {
	    # media port id
	    ($a1, $a2, $a3) = split(/\s+/, $value);
	    $current_media = $a1;
	    if ( ($a1 eq $mediatype) ||
	       ( ($a1 eq "whiteboard") && ($mediatype eq "wb") ) ) {
		$portnum = $a2;
		$media_id = $a3;
	    }
	}
	elsif ($version eq "vers2") {
	    # media port RTP format
	    ($a1, $a2, $a3, $a4) = split(/\s+/, $value);
	    $current_media = $a1;
	    if ( ($a1 eq $mediatype) ||
	       ( ($a1 eq "whiteboard") && ($mediatype eq "wb") ) ) {
		$portnum = $a2;
		$media_format = $a4;
	    }
	}
	elsif ($version eq "radio") {
	    # audio port RTP format
	    ($a1, $a2, $a3, $a4) = split(/\s+/, $value);
	    $current_media = $a1;
	    if ( ($a1 eq "audio") && ($mediatype eq "radio") ) {
		$portnum = $a2;
		$media_format = $a4;
	    }
	    else {
		&error_block("Cannot parse radio packet");
	    }
	    $x1 = &convert_ipnum($multicast_address);
	    $x2 = &convert_ipnum("239.7.7.0");
	    $media_id = $x1 - $x2;
	}
    }
    elsif ($control eq "a") {
	if ( ($version eq "vers1") || ($version eq "vers2") ) {
	    if ( ($value eq "sendrecv") || ($value eq "recvonly") ) {
		$attrib{"control"} = $value;
		next;
	    }
	    ($a1, $a2) = split(/:/, $value);
	    $attrib{$a1} = $a2;
	}
    }
    $i++;
}
close(ANNOUNCE);

# &print_packet;
&launch_application;

sub launch_application {
    if ($media_format eq "") {
	$media_format  = $attrib{"fmt"};
    }
    &create_transformations;
    &create_argument_list;
}

sub create_argument_list {
    local(@def_args);
    undef @def_args;
    undef @real_args;
    if (open(FMT, "<$media_file") ) {
	while (<FMT>) {
	    chop;
	    if (/^\s*#/) {
		next;
	    }
	    ($frmt, $tool, $toolpath, $rest) = split(/:/);

	    if ($mediatype eq "radio") {
		if ($frmt eq "default-radio") {
		    @def_args = split(/\s+/, $rest);
		    $save_toolpath = $toolpath;
		}
	    }
	    elsif ($mediatype eq "audio") {
		if ($media_format ne "") {
		    if ($frmt eq "default-audio") {
			@def_args = split(/\s+/, $rest);
			$save_toolpath = $toolpath;
		    }
		}
		else {
		    if ($frmt eq "default-audio-nofmt") {
			@def_args = split(/\s+/, $rest);
			$save_toolpath = $toolpath;
		    }
		}
	    }
	    elsif ($mediatype eq "video") {
		if ($frmt eq "default-video") {
		    @def_args = split(/\s+/, $rest);
		    $save_toolpath = $toolpath;
		}
	    }
	    elsif ($mediatype eq "wb") {
		if ($frmt eq "default-wb") {
		    @def_args = split(/\s+/, $rest);
		    $save_toolpath = $toolpath;
		}
	    }
	    # Need to do a case insensitive string compare
	    if (($media_format eq $frmt) && ($tool ne "") && ($toolpath ne "")) {
		@real_args = split(/\s+/, $rest);
		last;
	    }
	}
	close(FMT);
    }
    else {
	&error_block("Unable to open file $media_file");
    }
    if ($#real_args < 0) {
	@real_args = @def_args;
	$toolpath = $save_toolpath;
    }
    if ($#real_args < 0) {
	&error_block("No matching entry found in $media_file");
    }
    $argument_list = " "; # Leading speace needed
    for ($i = 0; $i <= $#real_args; $i++) {
	if ($real_args[$i] =~ /^\+/) {
	    $_ = $real_args[$i];
	    if (/\//) {
		foreach (split(/\//, $real_args[$i])) {
		    $argument_list .= "$transform{$_}/";
		}
		chop($argument_list);
	    }
	    else {
		$argument_list .= "$transform{$_} ";
	    }
	}
	else {
	    $argument_list .= "$real_args[$i] ";
	}
    }
    $tool_launch = "";
    $tool_launch = $toolpath.$argument_list;
    `$tool_launch`;
}

sub print_packet {
    print STDERR "version: ", $version, "\n";
    print STDERR "session_name: ", $session_name, "\n";
    print STDERR "session_info: ", $session_info, "\n";
    print STDERR "url: ", $url, "\n";
    print STDERR "owner: ", $owner, "\n";
    print STDERR "session_id: ", $session_id, "\n";
    print STDERR "session_version: ", $session_version, "\n";
    print STDERR "org_address: ", $org_address, "\n";
    print STDERR "email_address: ", $email_address, "\n";
    print STDERR "phone_number: ", $phone_number, "\n";
    print STDERR "multicast_address: ", $multicast_address, "\n";
    print STDERR "ttl: ", $ttl, "\n";
    print STDERR "start_time: ", $start_time, "\n";
    print STDERR "stop_time: ", $stop_time, "\n";
    print STDERR "encryption_key: ", $encryption_key, "\n";
    print STDERR "portnum: ", $portnum, "\n";
    print STDERR "media_id: ", $media_id, "\n";
    print STDERR "media_format: ", $media_format, "\n";

    print STDERR "Attributes: ", %attrib, "\n";
}

sub convert_ipnum {
    local($targ, $arg, @flds);

    ($arg) = ($_[0] =~ /^\s*(\S*)\s*$/);

    if ($arg =~ /\./) {
	$targ = 0;

	@flds = split(/\./, $arg);

	return -4 if $#flds != 3;

	foreach (@flds) {
	    return -3 if ! /\d+/;

	    return -2 if $_ > 255;
	    $targ = $targ * 256 + $_;
	}

	$targ;
    }
    else {
	if ($arg =~ /^0[xX]([0-9a-fA-F]*)/) {
	    hex($arg);
	}
	else {
	    -1;
	}
    }
}

sub error_block {
    print STDERR "\nError: $_[0]\n";
    exit 1;
}

sub create_transformations {
    %transform = ( "+ttl", $ttl,
		   "+format", $media_format,
		   "+port", $portnum,
		   "+mult_addr", $multicast_address,
		   "+mult_id", $media_id
		 );
}
