#!/usr/bin/perl -w
# -*- perl -*-
use P4CGI ;
use strict ;
#
#################################################################
#  CONFIGURATION INFORMATION 
#  All config info should be in P4CGI.pm
#
#################################################################
#
#  P4 depot browser, top
#
#################################################################


my $goto = &P4CGI::cgi()->param("GOTO") ;
if(defined $goto) {
    &P4CGI::ONLOADSCRIPT("location='$goto';") ;
    print &P4CGI::start_page("Please wait.....") ;
    print &P4CGI::end_page() ;
    exit 0 ;
}

&P4CGI::SKIPHEADERBUTTONS() ;

print &P4CGI::start_page("Add shortcut") ;

my $urlpath=&P4CGI::cgi()->url() ;
$urlpath =~ s|[^/]+$|| ;
	       
my $scurl=&P4CGI::cgi()->param("SC") ;
if(!defined $scurl) {
    &P4CGI::bail("Internal error! Parameter SC not defined!") ;
}
$scurl =~ s/^$urlpath// ;


my $decscurl = $scurl ;
if($decscurl =~ /\?/) {
    $decscurl .= ";" 
}
else {
    $decscurl .= "?" 
}
$scurl = &P4CGI::urlEncode($scurl) ; 

my %shortcuts = &P4CGI::SHORTCUTS() ; 
delete $shortcuts{&P4CGI::STARTPAGE_MARKER()} ;
my $scts = "";
my $name ;
my @sameAs ;
foreach $name (sort { uc($a) cmp uc($b) } keys %shortcuts) {
    if($scts eq "") {
	$scts =  &P4CGI::start_table("") ;
    }
    my $url=$shortcuts{$name} ;
    if(&P4CGI::urlEncode($url) eq $scurl) {
	push @sameAs,"<br><a class=\"normal\" href=\"$url\">$name</a>\n" ;
    }
    &P4CGI::cgi()->param("RMSHORTCUT","$url\:\:\:$name") ;
    my $del = &P4CGI::buttonLink(&P4CGI::cgi()->url(-query=>1),"Remove shortcut $name","Remove Shortcut") ;
    &P4CGI::cgi()->delete("RMSHORTCUT") ;    
    $scts .= "<tr><td><a class=\"normal\" href=\"$url\">$name</a></td><td>$del</td></tr>\n" ;
}
if($scts ne "") {
    $scts .= &P4CGI::end_table() ;
} ;


my $currDepotNo =  &P4CGI::CURR_DEPOT_NO() ;

print &P4CGI::start_framedTable(""),"<tr><td>\n" ;
print &P4CGI::start_table(" class=\"Text\""),
    "<tr><td>";
while(<DATA>) {
    last if /============/ ;
    s/__URL__/$scurl/g ;
    print ;    
}

my $existwarning = "" ;
{
    my $noInWarning ="";
    $noInWarning = "is a shortcut" if @sameAs == 1 ;
    $noInWarning = "are " . scalar @sameAs . " shortcuts" if @sameAs > 1 ;
    my $scts = join("",@sameAs) ;
    while(<DATA>) {
	last if /============/ ;
	s/__SCTS__/$scts/g ;
	s/__N__/$noInWarning/g ;
	$existwarning .= $_ ;
    }
} ;
print $existwarning if @sameAs > 0 ;

my $script = "" ;
while(<DATA>) {
    last if /============/ ;
    s/__SCURL__/$scurl/g ;
    s/__CURR_P4_DEPOT__/$currDepotNo/g ;
    s/__DECURL__/$decscurl/g ;
    $script .= $_ ;
}

while(<DATA>) {
    last if /============/ ;
    s/__SCURL__/$scurl/g ;
    s/__CURR_P4_DEPOT__/$currDepotNo/g ;
    s/__DECURL__/$decscurl/g ;
    s/__SCRIPT__/$script/ ;
    print ;    
}

if($scts ne "") {
    while(<DATA>) {
	last if /============/ ;
	s/__SCTS__/$scts/g ;
	print ;
    }
}

print  "</td></tr>", &P4CGI::end_table() ;
print  "</td></tr>", &P4CGI::end_framedTable() ;

print &P4CGI::end_page() ;

#
# That's all folks
#

__END__
<p class="Text">
    Name the shourtcut and press "Add Shortcut" or "Cancel"
============================================================
<P class="Text">
    There already __N__ to this page:
    __SCTS__<br>
    You can <a class="normal" title="Skip add shortcut" onClick="history.back() ;">Go back</a> or 
    continue and add another shortcut to the same page.
    
============================================================

var scdata='__SCURL__' + ':::' + document.TheForm.SCNAME.value ;
location='addshortcut.cgi?DP=__CURR_P4_DEPOT__;ADDSHORTCUT=' + scdata + '&GOTO=__SCURL__' ;
return false ;
    
============================================================
<p class="Text">
    <form name="TheForm" onsubmit="__SCRIPT__">
    <input class="Text" type="text" size="30" value="" name="SCNAME"><br>
    <button class="Text" type="button" name="addsc" value="addsc"
        title="Add shortcut with specified name" onClick="__SCRIPT__">Add Shortcut</button>
    <button class="Text" type="button" name="cancel" value="cancel"
        title="Do not add shortcut" onClick="location='__DECURL__' ;">Cancel</button>
    </form>

============================================================
    <hr>
<p class="Text">
    You have the following shortcut(s) specified:
    __SCTS__<br>
    If you specify an already existing name it will be modified.