#!/usr/bin/perl
#------------------------------------------------------------------------------
# Copyright (C) 2001 Thomax Kaulmann
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

use CGI;
use DBI;
use lib '/opt/oma/reboot/perl';
use REBOOT;

local %in, $query;
undef(%in);


##   ##            ##            ######         ##   ##
### ###           ####             ##           ###  ##
#######          ##  ##            ##           #### ##
#######          ##  ##            ##           ## ####
## # ##          ######            ##           ##  ###
##   ##          ##  ##            ##           ##   ##
##   ##          ##  ##          ######         ##   ##

CGI::ReadParse();

$query = new CGI();
#print $query->header('text/html');

$in{'base'} = $REBOOT::HOST if(!$in{'base'});

if($in{'id'}) {
	$db=REBOOT::opendb();
	$st=$db->prepare("select dc__identifier,path,dc__title from reboot_page where id=$in{'id'} AND location='$in{'base'}'");
	$st->execute();
	@row = $st->fetchrow();
	$st->finish();

	$length=(stat("/data/archive/$row[1]$in{id}/$row[0]"))[7];

	my $fname = $row[2];
	$fname =~ s/\W/_/g;
	$fname =~ tr/_/_/s;
	$fname = substr($fname,0,19);
	$fname .= "_$row[0]";
	$fname =~ s/emission_id-/rebootfm/g;
	$fname =~ s/_\d\d\.\d\d\.\d\d//g;
	$fname =~ tr/_/_/s;

	print $query->header("audio/x-ogg\nContent-length: $length\nContent-Disposition: attachment ; filename=\"$fname\"");

	open(F,"< /data/archive/$row[1]$in{id}/$row[0]");
	while(<F>) { print $_; }
	close(F);
}

