#!/usr/bin/perl

# Make sure that drawingMapFile is able to be read by Mapserver
# And that directory is writable by this script!

use strict;

use mapscript;
use CGI;
use DBI;

my $drawingMapFile = '/ms4w/apps/GeoMOOSE/maps/internal/draw.map';
my $directory = '/tmp';


# New CGI Object
my $cgi = new CGI;

# Coordinates, in ground units, and the title
my $coordinates = $cgi->param('coords');
my $title = $cgi->param('title');
my $shapeType = $cgi->param('shape');
my $buffer = $cgi->param('buffer');
my $output = $cgi->param('output');

my $layer = $cgi->param('layer');

if(not $output) {
	$output = 'xml';
}
# Unique Id, the Process Number combined with the UNIX time.
my $id = $$.time();

my $shapeFileType;

if($shapeType =~ m/point/i) {
	$shapeType = $mapscript::MS_SHAPE_POINT;
	$shapeFileType = $mapscript::MS_SHAPEFILE_POINT;
} elsif($shapeType =~ m/circle/i) {

} elsif($shapeType =~ m/line/i) {
	$shapeType = $mapscript::MS_SHAPE_LINE;
	$shapeFileType = $mapscript::MS_SHAPEFILE_POLYGON;
	if(not $buffer) {
		$buffer = 1;	# Makes the line work
	}
} else {
	$shapeType = $mapscript::MS_SHAPE_POLYGON;
	$shapeFileType = $mapscript::MS_SHAPEFILE_POLYGON;
}

# Convert the coordinates to a new shape object
my $shape = mapscript::shapeObj->new($shapeType);
my @coords = split(/ /, $coordinates);
my $line = mapscript::lineObj->new();
foreach my $coord (@coords) {
	my ($x, $y) = split(/,/,$coord);
	my $point = mapscript::pointObj->new();
	$point->{x} = $x;
	$point->{y} = $y;
	$line->add($point);
}
$shape->add($line);

$shape->setBounds();
# Buffer as desired.
if($buffer) {
	$shape = $shape->buffer($buffer);
	$shapeFileType = $mapscript::MS_SHAPEFILE_POLYGON;
	if(not $shape) { 
		print "Content-type: text/plain\n\n";
		die 'Buffering Failed!'; 
	}
}

#$shape->setBounds();

my $area = $shape->getArea();
my $perimeter = $shape->getLength();

# Create a new Coordinate string based on any potential buffering.
my $coordString = '';
for(my $line = 0; $line < $shape->{numlines}; $line++) {
	my $lineObj = $shape->get($line);
	for(my $point = 0; $point < $lineObj->{numpoints}; $point++) {
		my $pointObj = $lineObj->get($point);
		$coordString .= $pointObj->{x}.','.$pointObj->{y}.' ';
	}
}
#print $coordString."\n";

# Write out a shapefile
my $shapefile = new mapscript::shapefileObj($directory.'/'.$id, $shapeFileType);
if(not $shapefile) { die "Cannot Open Shapefile! $directory/$id.shp\n"; }
$shapefile->add($shape);

# Store some useful informatio in a Shapefile
my $dbh = DBI->connect("DBI:XBase:$directory");
$dbh->do("create table $id (title char(64), id char(64),area float, perimeter float)");
$dbh->do("insert into $id values ('$title', '$id', $area, $perimeter)");

#open fh, '>'.$directory."/".$id.'.dbf';
#print fh;
#close fh;

#system("touch $directory/$id.dbf");

#$shapefile->free();

# Give the client the ID back so it can reference it later
if($output eq 'text') {
print "Content-type: text/plain\n\n";
print $id;
} elsif($output eq 'xml') {
print "Content-type: text/xml\n\n";
print qq(<return>
	<selection title="$title" id="$id"/>
	<map title="$title" default="true" opacity="50">
		<file>$drawingMapFile</file>
		<param name="ID" value="$id"/>
	</map>
</return>
);
}
