#!/usr/bin/perl -w
use strict;
$|++;

#
#   FILE:  newinterface
#
#   Part of CWIS / SPT
#   Copyright 2003 Internet Scout Project
#   http://scout.wisc.edu
#

use Getopt::Std;


my($SelectedUIDir);
my($SelectedTheme);
my($NewUIName);
my($NewUIDir);
my($Debug) = 0;


# list available UIs and themes (if any) and ask user to select starting point
# (NOTE: this subroutine currently unused)
sub AskForStartingPointUI()
{
    my(@AvailableUIDirs);
    my($UIDir);
    my(@AvailableUIs);
    my($UIName);
    my(%UINames);
    my(@AvailableThemeDirs);
    my($ThemeDir);
    my(%ThemeNames);

    # obtain list of UI directories
    @AvailableUIDirs = glob("SPTUI--*");

    # for each UI directory
    foreach $UIDir (@AvailableUIDirs)
    {
        # if UI has page start and end and style sheet
        if ((-f $UIDir."/include/SPT--StandardPageStart.html")
                && (-f $UIDir."/include/SPT--StandardPageEnd.html")
                && ((-f $UIDir."/include/SPT--Stylesheet.css")
                    || (-d $UIDir."/Themes/")))
        {
            # add UI to list of available UIs
            push(@AvailableUIs, $UIDir);
        }
    }

    # for each remaining available UI
    foreach $UIDir (@AvailableUIs)
    {
        # if UI name file available
        if (-r $UIDir."/NAME")
        {
            # read in UI name
            my($UIName) = `cat ${UIDir}/NAME`;
            chomp($UIName);
            $UINames{$UIDir} = $UIName;
        }
        else
        {
            # use latter part of directory name as UI name
            $UINames{$UIDir} = substr($UIDir, 7);
        }
    }

    # ask user to select UI
    # if selected UI includes themes
    if (-d $SelectedUIDir."/Themes")
    {
        # obtain list of theme directories
        @AvailableThemeDirs = glob($SelectedUIDir."/Themes/Theme--*");

        # load theme names
        foreach $ThemeDir (@AvailableThemeDirs)
        {
            if (-r $ThemeDir."/NAME")
            {
                my($ThemeName) = `cat ${ThemeDir}/NAME`;
                chomp($ThemeName);
                $ThemeNames{$ThemeDir} = $ThemeName;
            }
            else
            {
                $ThemeNames{$ThemeDir} = substr($ThemeDir, 7);
            }
        }

        # ask user to select theme
    }
}

{
    # sign in
    print("\n");
    print("SPT/CWIS New User Interface Creation Utility\n");

    # determine UI to use as starting point
    $SelectedUIDir = `grep 'SPT_DefaultUI =' include/SPT--Config.php`;
    chomp($SelectedUIDir);
    $SelectedUIDir = substr($SelectedUIDir, 18);
    $SelectedUIDir =~ s/\";//;
    if ($Debug) {  print("UI starting point = \"${SelectedUIDir}\"\n");  }

    # ask for user interface name
    my($Line);
    print("\n");
    do {
        print("Name of New Interface: ");
        $Line = <STDIN>;
        chomp($Line);
        $Line =~ s/^\s+//;
        $Line =~ s/\s+$//;
        $NewUIName = $Line;
        $NewUIDir = $NewUIName;
        $NewUIDir =~ s/[^a-z0-9]//gi;
        $NewUIDir = "SPTUI--".$NewUIDir;
        if (-d $NewUIDir)
        {
            print("A user interface already exists with that name (or a close facsimile).\n");
        }
    } while (($Line eq "") || (-d $NewUIDir));
    if ($Debug) {  print("New interface name = \"${NewUIName}\"\n");  }
    if ($Debug) {  print("New interface directory = \"${NewUIDir}\"\n");  }

    # create directories
    my(@NewDirs) = ($NewUIDir,
                $NewUIDir."/MetadataTool",
                $NewUIDir."/include",
                $NewUIDir."/images"
                );
    my($NewDir);
    foreach $NewDir (@NewDirs)
    {
        if (! -d $NewDir) {  mkdir($NewDir, 0755);  }
    }

    # create soft links
    if (! -l $NewUIDir."/MetadataTool/images")
    {
        symlink("../images", $NewUIDir."/MetadataTool/images");
    }
    if (! -l $NewUIDir."/MetadataTool/include")
    {
        symlink("../include", $NewUIDir."/MetadataTool/include");
    }
    if (! -l "MetadataTool/".$NewUIDir)
    {
        symlink("../".$NewUIDir."/MetadataTool", "MetadataTool/".$NewUIDir);
    }

    # set up name file
    `echo "${NewUIName}" > ${NewUIDir}/NAME`;

    # copy in standard page files
    `cat ${SelectedUIDir}/include/SPT--StandardPageStart--TEMPLATE.html | sed "s/${SelectedUIDir}\\/include\\/SPT--Stylesheet.css/${NewUIDir}\\/include\\/SPT--Stylesheet.css/" > ${NewUIDir}/include/SPT--StandardPageStart.html`;
    `cp ${SelectedUIDir}/include/SPT--StandardPageEnd--TEMPLATE.html ${NewUIDir}/include/SPT--StandardPageEnd.html`;
    `cp ${SelectedUIDir}/include/SPT--Common--TEMPLATE.html ${NewUIDir}/include/SPT--Common.html`;

    # copy in CSS and any theme-specific graphics
    if (-f $SelectedUIDir."/Themes/Theme--ArcticSilver/SPT--Stylesheet.css")
    {
        `cp ${SelectedUIDir}/Themes/Theme--ArcticSilver/SPT--Stylesheet.css ${NewUIDir}/include/SPT--Stylesheet.css`;
        `cp ${SelectedUIDir}/Themes/Theme--ArcticSilver/*.gif ${NewUIDir}/include/.`;
    }
    else
    {
        `cp ${SelectedUIDir}/include/SPT--Stylesheet.css ${NewUIDir}/include/SPT--Stylesheet.css`;
    }
    
    # set permissions so that user will be able to edit files
    `chmod -R u+w ${NewUIDir}`;
    
    # report results to user
    print("\n");
    print("New interface \"${NewUIName}\" created.\n");
    print("\n");
    print("Standard page HTML for the new interface may be found in:\n");
    print("    ${NewUIDir}/include/SPT--StandardPageStart.html\n");
    print("    ${NewUIDir}/include/SPT--StandardPageEnd.html\n");
    print("    ${NewUIDir}/include/SPT--Common.html\n");
    print("Style sheet settings for the new interface may be found in:\n");
    print("    ${NewUIDir}/include/SPT--Stylesheet.css\n");
}
