// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) deadcode 
// Source File Name:   TutorialBuilder.java

package com.ptc.windchill.enterprise.tutorials.server;

import java.io.File;
import java.io.PrintStream;
import java.util.Enumeration;
import java.util.Vector;
import wt.util.WTProperties;

// Referenced classes of package com.ptc.windchill.enterprise.tutorials.server:
//            TutorialXmlProcessor, TutorialFileProcessor, TutorialMenuBuilder

public class TutorialBuilder
{

    public TutorialBuilder(String windchillHome, boolean zipIt)
    {
        zipTutorials = zipIt;
        String codebase = windchillHome + fileSep + "codebase";
        HELP_DIR = new File(codebase + HELP_PATH);
        TUTORIAL_INDEX_DIR = new File(codebase + TUTORIAL_INDEX_PATH);
        if(HELP_DIR.exists() && TUTORIAL_INDEX_DIR.exists())
            processHelpLocaleDirectories();
        else
        if(VERBOSE)
        {
            if(!HELP_DIR.exists())
                System.err.println("The help directory " + HELP_DIR + " was not found.");
            if(!TUTORIAL_INDEX_DIR.exists())
                System.err.println("The tutorials index directory " + TUTORIAL_INDEX_DIR + " was not found.");
            System.err.println("Aborting Tutorial Builder.");
            System.exit(1);
        }
        if(areFilesMissing)
            System.exit(1);
    }

    public static void main(String args[])
    {
        TutorialBuilder tb;
        if(args.length > 0 && args[0] != null)
        {
            tb = new TutorialBuilder(args[0], args.length > 1 && args[1].equalsIgnoreCase("zip"));
        } else
        {
            System.err.println("TutorialBuilder: Need to pass the directory that represents Windchill Installation point.");
            System.err.println("Aborting Tutorial Builder.");
            System.exit(1);
        }
    }

    private void processHelpLocaleDirectories()
    {
        if(VERBOSE)
            System.out.println("TutorialBuilder: Processing help home " + HELP_DIR);
        Vector helpLocaleDirectories = TutorialFileProcessor.getDirectories(HELP_DIR);
        if(helpLocaleDirectories.size() > 0)
        {
            for(Enumeration e = helpLocaleDirectories.elements(); e.hasMoreElements(); processHelpLocaleTutorials((File)e.nextElement()));
        } else
        {
            System.err.println("TutorialBuilder: There was no help_XX directory to process at " + HELP_DIR);
            areFilesMissing = true;
        }
    }

    private void processHelpLocaleTutorials(File helpDirectory)
    {
        if(!helpDirectory.getName().startsWith("help_"))
            return;
        if(VERBOSE)
            System.out.println("TutorialBuilder: Processing help directory " + helpDirectory.getName());
        File tutorialDirectory = new File(helpDirectory.getPath() + fileSep + "tutorials");
        if(tutorialDirectory != null && tutorialDirectory.exists())
        {
            String locale = helpDirectory.getName().replaceFirst("help_", "_");
            File indexFiles[] = new File[3];
            indexFiles[0] = new File(TUTORIAL_INDEX_DIR.getPath() + fileSep + "interopIndex" + locale + ".xml");
            indexFiles[1] = new File(TUTORIAL_INDEX_DIR.getPath() + fileSep + "pdmIndex" + locale + ".xml");
            indexFiles[2] = new File(TUTORIAL_INDEX_DIR.getPath() + fileSep + "pjlIndex" + locale + ".xml");
            StringBuffer localeTutorialData = new StringBuffer("tutorialMenuArray = [new tutorialMenu( \"mainMenu\", \"\", \"Menu\" )");
            boolean atLeastOneIndexFound = false;
            for(int i = 0; i < indexFiles.length; i++)
                if(indexFiles[i].exists())
                {
                    atLeastOneIndexFound = true;
                    localeTutorialData.append(getValidTopics(indexFiles[i], tutorialDirectory.getPath() + fileSep));
                } else
                {
                    System.err.println("TutorialBuilder: WARNING - index file" + indexFiles[i] + " was not found.");
                    areFilesMissing = true;
                }

            localeTutorialData.append("]");
            if(atLeastOneIndexFound)
            {
                String filePath = tutorialDirectory.getPath() + fileSep + "menuObjects.js";
                if(VERBOSE)
                    System.out.println("TutorialBuilder: Writing javascript to " + filePath);
                TutorialFileProcessor.writeJavascriptFile(TutorialFileProcessor.createFile(filePath), localeTutorialData.toString());
            } else
            {
                System.err.println("TutorialBuilder: No index files found for " + helpDirectory.getName());
            }
            if(zipTutorials)
            {
                if(VERBOSE)
                    System.out.println("TutorialBuilder: Creating zip file of tutorials in: " + tutorialDirectory.getParentFile().getPath());
                TutorialFileProcessor.buildZip(tutorialDirectory);
            }
        } else
        {
            System.err.println("TutorialBuilder: No tutorials directory found in " + helpDirectory);
            areFilesMissing = true;
        }
    }

    private String getValidTopics(File indexFile, String tutorialsPath)
    {
        TutorialXmlProcessor txp = new TutorialXmlProcessor();
        TutorialFileProcessor tfp = new TutorialFileProcessor();
        TutorialMenuBuilder tmb = new TutorialMenuBuilder();
        Vector Topics = new Vector();
        if(txp.buildDocument(indexFile, TUTORIAL_INDEX_DIR + fileSep + INDEX_DTD))
        {
            if(VERBOSE)
                System.out.println("TutorialBuilder: Processing " + indexFile);
            if(VERBOSE)
                System.out.println("TutorialBuilder: Removing topics if their resource file doesn't exist.");
            topics = txp.processDocument(txp.getMainDocument(), indexFile.getName());
            Vector validTopics = tfp.validateResourceFiles(topics, tutorialsPath);
            int invalidTopics = topics.size() - validTopics.size();
            if(invalidTopics > 0)
            {
                System.err.println("TutorialBuilder: MISSING FILES Removed " + invalidTopics + " topic(s) from " + indexFile.getName());
                areFilesMissing = true;
            }
            return tmb.createTutorialMenus(validTopics, indexFile.getName());
        } else
        {
            System.err.println("TutorialBuilder: ERROR Problem parsing " + indexFile + ". File skipped.");
            areFilesMissing = true;
            return "";
        }
    }

    private Vector topics;
    private static boolean VERBOSE;
    private static boolean zipTutorials = true;
    private static String HELP_PATH = null;
    private static File HELP_DIR;
    private static String TUTORIAL_INDEX_PATH = null;
    private static File TUTORIAL_INDEX_DIR = null;
    private static String INDEX_DTD = null;
    private static final String fileSep;
    private static boolean areFilesMissing = false;

    static 
    {
        fileSep = File.separator;
        try
        {
            WTProperties properties = WTProperties.getLocalProperties();
            VERBOSE = properties.getProperty("com.ptc.windchill.enterprise.tutorials.verbose", false);
            HELP_PATH = properties.getProperty("windchill.help.home", fileSep + "wt" + fileSep + "helpfiles");
            TUTORIAL_INDEX_PATH = properties.getProperty("com.ptc.windchill.enterprise.tutorials.index", fileSep + "registry" + fileSep + "tutorials");
            INDEX_DTD = properties.getProperty("com.ptc.windchill.enterprise.tutorials.schema", "tutorialIndexDtd.dtd");
        }
        catch(Throwable t)
        {
            System.err.println("There was an Error initializing the TutorialBuilder file");
        }
    }
}
