#!/usr/bin/suidperl
# LiveSurround 1.0
# copyright 2002 Manuel Amador (Rudd-O).  This software is under the GPL
# http://www.usm.edu.ec/~amadorm/
# 
# this program lets you enable or disable Dolby Pro Logic surround sound 
# so that you can watch movies like a home theater
#
# this software requires a Sound Blaster Live!, Linux and the emu-tools package from
# http://www.sourceforge.net/projects/emu10k1/

if (@ARGV[0] eq "")
{

print "LiveSurround 1.0

copyright 2002 Manuel Amador (Rudd-O).  This software is under the GPL
http://www.usm.edu.ec/~amadorm/

This program lets you enable or disable Dolby Pro Logic surround sound
so that you can watch movies with surround sound.

This software requires a Sound Blaster Live!, Linux, four working speakers
and the emu-tools package from http://www.sourceforge.net/projects/emu10k1/

usage: surround [on|off]
";

exit 1;
}


$ENV{'PATH'} = "/bin:/usr/bin:/usr/local/bin";
$base = "/usr/local/etc";

$emuconfig = $base . "/" . "emu10k1.conf";
$reloadconfig = $base . "/" . "emu-script";

$aumix = "/usr/bin/aumix";
$savevolume = $aumix . " -f /etc/.aumixrc -S";
$loadvolume = $aumix . " -f /etc/.aumixrc -L";

if (@ARGV[0] eq "on")
{
 $surround = 1;
}
if (@ARGV[0] eq "off")
{
 $surround = 0;
}

#load configuration on an array
  $i = 0;
  open (INPUT, $emuconfig ) || die "can't open emuconfig: $!";
  while (<INPUT>) {
    chomp; $resultado[$i] = $_;  $i++;
  }
  close(INPUT);

  #process configuration in memory
  for $a ( 0 .. $i ) {
    if ( $resultado[$a] =~ /^PROLOGIC/ ) {
      $foundsurround = 1;
      if ($surround == 1) {
        $resultado[$a] = "PROLOGIC=yes";
        print "Turning surround on\n";
      }
      else {
        $resultado[$a] = "PROLOGIC=no";
        print "Turning surround off\n";
      }
    }	
  }
  if ( ($surround == 1) && ($foundsurround != 1) ) {
    print "Turning surround on (added to configuration file)\n";
    $i++;
    $resultado[$i] = "PROLOGIC=yes";
  }
 

#save configuration
  open (OUTPUT, ">", $emuconfig ) || die "can't open emuconfig for saving: $!";
  for $a ( 0 .. $i ) {
#    print $resultado[$a]; print "\n" if $a < $i;
    print OUTPUT $resultado[$a]; print OUTPUT "\n" if $a < $i;
  }
  close(OUTPUT) ;

system $savevolume;
system $reloadconfig;
system $loadvolume;


