#! /usr/bin/perl

BEGIN { require "../../perl/lib/Sitedefs.pm"; }

use strict;

use ErrorLog;
use Setting;
use Studio;
use PosterShop;
use CGITracker;

$ENV{'XAUTHORITY'}="/home/www/www.scienceu.com/init/Xauthority";

#
# studio.cgi adds new images and displays the personal gallery
#
#   add     -- add an image and display the studio
#              called from external scripts.
#              Optional "returnurl=URL" and "returnmsg=mesg" args
#              can be passed with the form data to specify
#              a return link
#
# The main studio page also serves as a menu to all other studio
# functionality:
#
#   delete  -- calls delete.cgi when a pic's delete icon is clicked
#   open    -- calls effects.cgi when a pic itself is clicked
#
# On the naviagtion bar:
#
#   posters -- goes to poster entry point
#   tshirts -- goes to tshirt entry point
#   effects -- another entry point to the effects
#   locker  -- goes to the locker subsystem


#########################################################
#  Set up local variables

my(@tobeadded,
   $p,
   );

my $q = new CGITracker;

my $s = new Studio( $q->GetClientId() );

#########################################################
#  Debug info

ERRLOG "clientid = ", $q->GetClientId(), "\n";

foreach $p ($q->param()) {
    ERRLOG "param $p = >", $q->param($p), "<\n";
}

#########################################################
# Process args -- we only look for "add" args and ignore
#                 any extraneous args

# If there are no args, show the current gallery immediately.

if (! $q->param()) {
    $s->Show($q);
    exit(0);
}


#### Scan args for images to be added
#
#    Just look for any 'add' params and add whatever you find

@tobeadded = ();
foreach $p ($q->param()) {
    if ($q->param($p) eq "add") {
	push(@tobeadded, $p);
    }
}

my $command;
my @added_indices = ();
foreach $command (@tobeadded) {
    my $filename =
      Studio::Setting::DirPath($q->GetClientId()) . "/" . $$;
    my ($path, $width, $height, $format) =
	$s->GenerateRepresentation($command, 100, 100,
				   $filename, "GIF");
    my $index =
	$s->AddEntry($command, $path, "MOVE", $width, $height);

    # Append the index of the just-added image to the @added_indices
    # array.
    push(@added_indices, $index);
}

# Pass the 'added_indices' array on to $q->Show, so it can choose to
# display something special for these images if it wants (as is the
# case with the new 'focus' parameter that I'm adding right now,
# to all for putting emphasis on something like ordering a t-shirt.
# mbp Thu Nov 12 15:41:05 1998

$q->param('added_indices', \@added_indices);

# Return the current Studio page

$s->Show($q);
