<?php

// Return the path to the root of the server (where the docs start!)
// Always includes trailing slash!
function GetServerRootFolder()
{   // Fix IIS implementation of DOCUMENT_ROOT

    if (!array_key_exists("DOCUMENT_ROOT",$_SERVER)) $_SERVER["DOCUMENT_ROOT"]=substr($_SERVER['PATH_TRANSLATED'], 0, strlen($_SERVER['SCRIPT_NAME']) * - 1);

    $folder = $_SERVER["DOCUMENT_ROOT"];
    if ($folder[strlen($folder)-1]!="/") $folder.="/";
    return $folder;
}

// Get V4 or V3 manuals location
if ($_GET["version"] >= 4)  { // V4 new location
    $path = "support/manuals-guides/";
} else { // V3 still use old location!
    $path = "softwaremanuals/";
}



// Get passed in filename, remove any extension
$filename = strtoupper($_GET["acq"]);
$ext = substr($filename,strlen($filename)-4,4);
if (substr($ext,0,1)==".") $filename=substr($filename,0,strlen($filename)-4);

// If V4 first check manuals.csv to see a manual with a different name should be used
if ($_GET["version"] >= 4) {
    // open the cvs
    $file = fopen('manuals.csv', 'r'); 
    // examine each line (where $line is an array of the csv elements)
    while (($line = fgetcsv($file)) !== FALSE) {
        // get element 0 (filename), remove its extenstion
        $csvfilename = strtoupper($line[0]);
        $ext = substr($csvfilename,strlen($csvfilename)-4,4);       
        if (substr($ext,0,1)==".") $csvfilename=substr($csvfilename,0,strlen($csvfilename)-4);
        // if filename = cvs filename use the csv manual!
        if ($filename == $csvfilename) {
            if (file_exists($path.$line[1])) {
                    fclose($file);
                    header("location: /".$path.$line[1]);               
                    exit;
                }
        }
    }
    fclose($file);
} 

// If got here then was NOT V4 or WAS V4 but no alternative manual found in CSV... 

// Get directory listing of manuals in the folder
$fle = opendir(GetServerRootFolder().$path);
$files = array();
while (($file = readdir($fle)) !== false) $files[]=$file;
closedir($fle);

// Do any of the manuals in this folder match filename (with extension)?
for ($a=0; $a<count($files); $a++) {

    $f2 = strtoupper($files[$a]);
    
    // manuals can have these extensions
    $t1 = $filename.".PDF";
    $t2 = $filename."_ACQ.PDF";
    $t3 = $filename.".ACQ.PDF";
    $t4 = $filename.".EXE.PDF";
    
    if (($f2==$t1) || ($f2==$t2) || ($f2==$t3) || ($f2==$t4)) {
        header("location: /".$path.$files[$a]);
        exit;
    }
}


// Still not found. Do any of the manuals in this folder begin with filename?
for ($a=0; $a<count($files); $a++) {
    $f2 = strtoupper($files[$a]);
        if (!(strpos($f2,$filename)===false)) {
        header("location: /".$path.$files[$a]);
        exit;
    }
}

// Still, still not found? If looking for V4 give up and try V3 manual instead!
if ($_GET["version"] >= 4) {
    header("location: http://www.acquiredigital.com/download.php?acq=".$_GET["acq"]);
    exit;
}

// If we get here no V4 OR V3 manual was found so show MNF page
header("location: /manualnotfound.php");
exit;

?>