#! /usr/bin/perl -T

# The -T is used to turn on PERL's automatic tainting

# Path where VLC is located
$VLC_PATH = '/usr/bin';

# We are going to use a pipe to fool VLC
# command line interface's need for STDIN
close (STDIN);

# Get the temporary filename Mozilla handed you
$file = $ARGV[0];
# Untaint filename
# Make sure it contains only "word characters",
# a hyphen, a dot, or a /
if ($file =~ /^([-\/\w.]+)$/)
{
  $file = $1;
}
else
{
  exit (1);
}
  
# Create a pipe to connect Parent's Writer
# to Child's Reader
pipe ( Reader, Writer );

# Create parent and child processes
$pid = fork();

if ($pid)
{
  # Parent
  
  # Unidirectional
  close (Reader);
  
  # Wait for vlc to play file
  wait;
  
  # Remove temporary copy of file
  unlink ($file);
  
  exit (0);
}
else
{
  # Child
  
  # Unidirectional
  close (Writer);

  # Explicitly set path to VLC's Path 
  $ENV{PATH} = $VLC_PATH; 

  # Start vlc with command line interface
  # Tell it:
  #   1. To keep quiet
  #   2. Fake a tty
  #   3. The filename given by Mozilla
  #   4. To quit and exit after playing the given file
  exec ('vlc' , '--quiet', '--fake-tty', $file, 'vlc:quit');
}
