#!/usr/bin/perl

print "removeprefs.pl\n";

my $debug = 0;

@prefs = (  'tcp_max_thin_delay_tolerance', 
            'tcp_min_thin_delay_tolerance',
            'tcp_thick_min_interval',
            'default_window_size',
            'overbuffer_bucket_interval',
            'auto_delete_sdp_files',
            'reliable_udp_dirs',
            'tcp_max_video_delay_tolerance',
            'tcp_min_video_delay_tolerance',
            'tcp_max_audio_delay_tolerance',
            'tcp_min_audio_delay_tolerance',
            'quality_check_interval',
            'drop_all_packets_delay',
            'thin_all_the_way_delay',
            'optimal_delay',
            'start_thicking_delay',
            'alt_transport_src_ipaddr',
            'relay_prefs_file',
            'modAccess_groupsfilepath',
            'modAccess_usersfilepath',
            'module_folder',
            'modAccess_groupsfilepath',
            'modAccess_usersfilepath',
            'max_tcp_buffer_size',
            'flow_control_probe_interval',
            'tcp_seconds_to_buffer',
            'run_user_name',
            'run_group_name',
            'enable_monitor_stats_file',
            'rtp_timeout');


if(open(XMLFILE, "$ARGV[0]"))
{
    $buf = "";

    while ($line = <XMLFILE>)
    {
	$buf .= $line;
    }
    
    close (XMLFILE);
    
    my $in = 0;

    for ( $in = 0; $in <= $#prefs; $in++ )
    {
		$name = $prefs[$in];
		
		
		if ( $buf =~ m/^(.*?)<PREF NAME="$name"(.*?)>(.*?)<\/PREF>(.*)/s )
		{   print "Setting: $name\n"; 
			$buf = $1 . $4;
		}
		else
		{
			if ($debug)  { print "Pref not found: $name\n"; }
		}
    }

    if ( $buf =~ m/^(.*?)<PREF NAME="rtsp_port" TYPE="UInt16"(\s*)>(.*?)<\/PREF>(.*)/s )
    {
        if ( $3 != 8000 )
		{
			$buf = $1 . qq(<LIST-PREF NAME="rtsp_port" TYPE="UInt16"><VALUE>$3</VALUE><VALUE>8000</VALUE></LIST-PREF>) . $4;
		}
    }
    elsif ( $buf =~ m/^(.*?)<LIST-PREF NAME="rtsp_port" TYPE="UInt16"(\s*)>(.*?)<\/LIST-PREF>(.*)/s )
    {
		$buf = $1 .  qq(<LIST-PREF NAME="rtsp_port" TYPE="UInt16">$3);
		$ports = $3;
		$portexists = 0;
		$remaining = $4;
		while ($ports =~ m/^(.*?)<VALUE>(.*?)<\/VALUE>(.*)/s)
		{
			if ($2 == 8000) { $portexists = 1; }
			$ports = $3;
		}
		
		if ($portexists == 0)
		{
			print "adding port 8000 \n"; 
			$buf .= qq(<VALUE>8000</VALUE>);
		}
		
		$buf .= qq(</LIST-PREF>) . $remaining;
    }

    if(open(WRITEFILE, ">$ARGV[1]"))
    {
	print WRITEFILE $buf;
	close(WRITEFILE);
    }
}


