#!/usr/bin/perl -I.
# Dreamcast Version
# SONIC ADVENTURE
# VMU Download Routine
# 


use strict;

use CGI;
use	DC;
use DB;


## Set up variables
use vars	(	'$dc',	# DC object
				'@IN',
				'$LONG',
				'$errorURL',
				'$vmsurl',
				'$vmsdir',
				'$browserid',
			);

#...............................................

	my $q = new CGI;
	$dc = new DC($q);
	$dc->q->import_names('IN');
	$browserid = -1;

	$errorURL = "community.html";
	$vmsurl = 'http://sonic.games.dreamcast.com/tmp';
	$vmsdir = '/var/www/html/sonic.games.dreamcast.com/tmp';
	my $fileid = sprintf "%08d", $$;

	my ($errN, $errstr, $dbh, $sth, @row);

    &check_data;

	($dbh, $errstr) = DB::openOracleConnection();
	if( !$dbh )
	{
		$dc->error("[getfile] Error connecting to database: $errstr", $errorURL);
	}

    ($errN, $errstr, @row) = DB::getRow_LongByNameAndID( $dbh, $dc->DBaseTable('LONG'), $IN::filename, $browserid );
	
	if ($errN)
	{
		$dc->error("[getfile] Fetching Long info: $errN .... $errstr", $errorURL);
	}

	my $len = scalar (@row);
	if( $len <= 0 ) # There is no row for ID >= 0 try now for ID = -1
	{
		
		($errN, $errstr, @row) = DB::getRow_LongByName( $dbh, $dc->DBaseTable('LONG'), $IN::filename);		
		if ($errN)
		{
			$dc->error("[getfile] Re Fetching Long info: $errN .... $errstr", $errorURL);
		}
		
	}
	
	if ($row[$dc->LongOff('NAME')] eq "")
	{
		$dc->error("[getfile] Filename does not exist \"$IN::filename\".", $errorURL);
	}

	DB::closeOracleConnection( $dbh );
	
	make_vmsfile($fileid, $row[$dc->LongOff('NAME')], $row[$dc->LongOff('VMS')]);

    # Send VMU Data
	my $location = "${vmsurl}/${fileid}.VMI";
	print $dc->q->redirect(-location=>$location);

exit(0);

#--------------------------------------------------------
# Data Check
#--------------------------------------------------------
sub check_data {
    if($IN::filename eq "") {
        $dc->error("[getfile] Invalid filename was given \"$IN::filename\".", $errorURL);
    }

	# if ($dc->q->referer() !~ m(^http://sonic\.games\.dreamcast\.com/))
	# {
		# $dc->error("[getfile] File request denied.<BR>Only authorized URLs allowed.", $errorURL);
	# }

	$browserid = $dc->get_browser();
	# if ($browserid == -1)
	# {
		# $dc->error("[getfile] File request denied.<BR>Only authorized browsers allowed.", $errorURL);
	# }
}

#--------------------------------------------------------
# Cancel Unnecessary VMS Files
#--------------------------------------------------------
sub del_vmsfile {
	my $filename = shift;

	unlink("${vmsdir}/${filename}.VMI");
	unlink("${vmsdir}/${filename}.VMS");
}

#--------------------------------------------------------
# Create the VMS files for download
#--------------------------------------------------------
sub make_vmsfile {
	my $filename = shift;
	my $vmuname = shift;
	my $LONG = shift;

	# Clear out old VMS and VMI files sitting on the system
	&del_vmsfile($filename);
    
	#Create the VMS file
    if(open(VMSFILE,">${vmsdir}/${filename}.VMS")) {
        print VMSFILE $LONG;
        close(VMSFILE);
        chmod(0666,"${vmsdir}/${filename}.VMS");
    } else {
        $dc->error("[getfile] Failure to create VMS file \"${filename}\".", $errorURL);
    }

	# Create the VMI file
    if(open(VMIFILE,">${vmsdir}/${filename}.VMI")) {
		my $vmidata = "";

        $vmidata = &make_vmidata($filename, 0, int(length($LONG)/512)*2);
        print VMIFILE $vmidata;

        close(VMIFILE);
        chmod(0666,"${vmsdir}/${filename}.VMI");
    } else {
        $dc->error("[getfile] Failure to create VMI file \"${filename}\".", $errorURL);
    }

}

#........................................................
# Creating VMI data.
sub make_vmidata {
	my $filename = shift;
	my $checksum = shift;
	my $filesize = shift;

    my ($i, @var, $version, $FILE);
	my @VMIDATA = ();

    my (@vmiheader) = (	
        83, 111, 110, 105, 99, 32, 65, 100, 118, 101,110,116,117, 114,101, 32,
          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        83, 101,103, 97,32, 69,110, 116,101,114,112, 114,105, 115,101,115,
        0,0,0, 0,0, 0,0, 0,  0,  0,  0,  0,  0,  0,  0,  0
        );

    for($i=0;$i<68;$i++) {
        $VMIDATA[$i+4] = $vmiheader[$i];
    }

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    $mon++;
    $VMIDATA[68]= ($year+1900 & 255);	# year (Low Byte)
    $VMIDATA[69]= ($year+1900 >> 8);	# year (High Byte)
    $VMIDATA[70]= $mon;			# month
    $VMIDATA[71]= $mday;		# day
    $VMIDATA[72]= $hour;		# hour
    $VMIDATA[73]= $min;			# minute
    $VMIDATA[74]= $sec;			# second
    $VMIDATA[75]= $wday;		# day of the week
    $VMIDATA[76]= 0;			# Version (superior rank)
    $VMIDATA[77]= 0;			# Version (inferior rank)
    $VMIDATA[78]= 1;			# # of files (inferior rank)
    $VMIDATA[79]= 0;			# # of files (superior rank)

    @var=split(//,$filename);
    for($i=0;$i<8;$i++) {
        $VMIDATA[$i+80] = ord($var[$i]);	# File names on server
    }

	$version = $dc->get_browser();
	if ($version == 0)
	{
		# US - Planet Web Version
		$VMIDATA[0] = $VMIDATA[80] & 65;		# Checksum
		$VMIDATA[1] = $VMIDATA[81] & 71;		#
		$VMIDATA[2] = $VMIDATA[82] & 69;		#
		$VMIDATA[3] = $VMIDATA[83] & 83;		#
	} else {
		# Japanese and European Version
		$VMIDATA[0] = $VMIDATA[80] & 83;		# Checksum
		$VMIDATA[1] = $VMIDATA[81] & 69;		#
		$VMIDATA[2] = $VMIDATA[82] & 71;		#
		$VMIDATA[3] = $VMIDATA[83] & 65;		#
	}

	$FILE = $IN::filename;
    $FILE =~ y/a-z/A-Z/;

    @var=split(//,$FILE);
    for($i=0;$i<12;$i++) {
        $VMIDATA[$i+88] = ord($var[$i]);	# VMS file names
    }

    $VMIDATA[100] = 0;			# Attributes
    $VMIDATA[101] = 0;			# Attributes
    $VMIDATA[102] = 0;			# Attributes
    $VMIDATA[103] = 0;			# Attributes
    $VMIDATA[104] = 0;			# File size
    $VMIDATA[105] = $filesize;	# File size
    $VMIDATA[106] = 0;			# File size
    $VMIDATA[107] = 0;			# File size

	my $vmidata = "";
	for($i=0;$i<108;$i++) {
        $vmidata .= pack("C",$VMIDATA[$i]);
    }

    return $vmidata;
}

#------------------------ EOF --------------------------

