#!/bin/sh
# +--------------------------------------------------------------------------+
# | Copyright 1992 - 2002 Rational Software Corporation. All Rights Reserved.|
# | This software contains proprietary and confidential information of       |
# | Rational and its suppliers. Use, disclosure or reproduction is           |
# | prohibited without the prior express written consent of Rational.        |
# +--------------------------------------------------------------------------+
#
#@(#)$Revision: 1.34 $  $Date: 07/01/2002 14:30:23 $
#**************************************************************************
#SOHT
#
#NAME
#     make_menus_links - Create apex_menus and html_links files.
#
#SYNOPSIS
#     make_menus_links [<options>] <prod_home>...
#
#DESCRIPTION
#     Create the apex_menus file for the Help:Manuals pull down menu.
#     Create the html_links file to provide navigation links for the
#     on-line documentation.
#
#OPTIONS
#    -a <APEX_HOME>
#          Specify the Apex home directory.  This is only needed if
#          $APEX_HOME is not set and the -lp option is used.
#
#          This command does all of the following. Use one of these 4 options
#          to have the make_menus_links do only the corresponding step:
#    -cl   Create/check links in the *.links directories.
#    -cm   Create/update the apex_menus file and the doc.list files
#    -ci   Create/update the master index files and legend.
#          The -ci option automatically turns on the -log option.
#    -ch   Create/check html_links file in the *.links directories.
#
#    -f    Do not complain or display any errors.
#
#    -h    Show this usage message.
#
#    -log  Show logging messages.
#
#    -lp <prod_home>
#          Specify an Apex layered product home directory.
#
#    -pn <prod_name>
#          Specify the product name.
#
#    -x    Shell trace mode.
#
#    <prod_home>  The default is APEX_HOME if set. Otherwise this is required.
#
#EOHT
#**************************************************************************

FILE=$0
CMD=`basename $FILE`
pid=$$
tmp=/tmp/$CMD.$pid
tmp_dd=/tmp/$CMD.description.$pid
file_new=/tmp/$CMD.new.$pid
temp1=/tmp/$CMD.temp1.$pid
temp2=/tmp/$CMD.temp2.$pid
sedf=/tmp/$CMD.sedf.$pid
def_4_apex_lang_list="ada c++ summit multi"
def_apex_lang_list="$def_4_apex_lang_list nt"
layered_product=false
use_apex_startup=false
insert='insert 0 '
TAB="	"
SPACE=" "
WS="${SPACE}$TAB"
error=echo
set_x=':'
Do_cl=false
Do_cm=false
Do_ci=false
Do_ch=false
No_c_opt=true
LOG=':'

#------------------------------------------------------------------------
Usage() {
#------------------------------------------------------------------------

    # Extract the text between #SOHT and #EOHT in this script.
    if [ $1 = 0 ]
    then
        sed -e '1,/^#SOHT/d' -e '/^#EOHT/,$d' -e 's/^#//' $FILE | more
        exit 0
    else
        $error "Run '$CMD -h' for usage information."
        exit $1
    fi
}

#------------------------------------------------------------------------
display_title() {
#------------------------------------------------------------------------

    if [ "$#" -eq 1 ]
    then
        echo
        echo "$1"
    else
        if [ `echo "$1 $2" | wc -c` -gt 79 ]
        then
            echo
            echo "$1"
            echo "  $2"
        else
            echo
            echo "$1 $2"
        fi
    fi
}

#------------------------------------------------------------------------
find_Doc_description() {
#------------------------------------------------------------------------

    $set_x
    find *.links/*/. -name Doc.description -print 2>/dev/null |
    sed -e 's?/\./?/?'
}

#------------------------------------------------------------------------
find_Doc_html_links() {
#------------------------------------------------------------------------

    $set_x
    find */. -name Doc.html.links -print 2>/dev/null |
    sed -e 's?/\./?/?'
}

#------------------------------------------------------------------------
find_Doc_title() {
#------------------------------------------------------------------------

    $set_x
    find */. -name Doc.title -print 2>/dev/null |
    sed -e 's?/\./?/?'
}

#------------------------------------------------------------------------
get_from_doc_description() {
#------------------------------------------------------------------------

    $set_x
    tag=$1
    set -- 1 `grep "^#default[$WS]*$tag[$WS]" $doc_desc_file |
    sed -e "s?^#default??" -e "s?$tag??"`
    shift
    echo $*
}

#------------------------------------------------------------------------
add_header() {
#------------------------------------------------------------------------

    $set_x
    case $1 in
    links )
        echo "# DO NOT EDIT THIS FILE
# It is generated from the Doc.html.links files found in the doc dir by the
# script $CMD, available in either /ned/sierra/...
# or \$APEX_HOME/install/share.
# ###########################################################################"
        ;;
    menus )
        echo "# DO NOT EDIT THIS FILE
# It is generated from the Doc.description files found in the doc dir
# by the script $CMD, available in either /ned/sierra/...
# or \$APEX_HOME/install/share.
"
        if [ -s $APEX_HOME/editor_files/apex_menus.base ]
        then
            if grep "insert 6 cascade" \
               $APEX_HOME/editor_files/apex_menus.base >/dev/null
            then
                cat $APEX_HOME/editor_files/apex_menus.base
                return
            fi
        fi
        echo "insert 6 cascade *.help.manuals \"Manuals\" a"
        echo
        ;;
    esac
}

#------------------------------------------------------------------------
append_lp_variant() {
#------------------------------------------------------------------------

    $set_x
    case $1 in
        *ada ) title="$title (Ada)" ;;
        *c++ ) title="$title (C/C++)" ;;
        *multi ) title="$title (Duo)" ;;
        *summit ) title="$title (Summit TM)" ;;
        *nt ) title="$title (Ada/NT)" ;;
    esac
}

#------------------------------------------------------------------------
extract_prod_name() {
#------------------------------------------------------------------------

    $set_x
    case $doc_path in
    */apex_clearcase.* )
        lp_counter=12
        prod_name="Apex/ClearCase"
        ;;
    */apexrt.*|*/rexec* )
        lp_counter=01
        prod_name="Rational Apex Embedded"
        ;;
    */apexvw.*|*/vxworks* )
        lp_counter=01
        prod_name="Rational Apex Embedded"
        ;;
    */apexlx.*|*/lynx* )
        lp_counter=01
        prod_name="Rational Apex Embedded"
        ;;
    */apex_embedded.* )
        lp_counter=01
        prod_name="Rational Apex Embedded"
        ;;
    */rose_cpp.* )
        lp_counter=04
        prod_name="Rose C/C++"
        ;;
    */rose_ada.* )
        lp_counter=05
        prod_name="Rose Ada"
        ;;
    */rose98* )
        lp_counter=12
        prod_name="Rose 98 for UNIX"
        ;;
    */rose2000* )
        lp_counter=13
        prod_name="Rose 2000 for UNIX"
        ;;
    */rose.* )
        lp_counter=06
        prod_name="Rational Rose"
        ;;
    */soda.* )
        lp_counter=07
        prod_name="SoDA"
        ;;
    */testmate.* )
        lp_counter=08
        prod_name="TestMate"
        ;;
    */ada_analyzer.* )
        lp_counter=09
        prod_name="Ada Analyzer"
        ;;
    */axi.* )
        lp_counter=10
        prod_name="AXI"
        ;;
    all/*|ada/*|c++/*|multi/*|summit/* )
        unset lp_counter
        ;;
    ../*|*/rogue_* )
        unset lp_counter
        ;;
    * )
        lp_counter=11
        prod_name="Other Apex Layered Products"
        ;;
    esac

    # Add to list.
    if [ -n "$lp_counter" ]
    then
        index=99$lp_counter
        append_lp_variant $1

        set -- 1 `echo "$prod_list" | grep " : ${prod_name}\$"`
        if [ -n "$2" ]
        then
            # Already encountered this product.
            return
        fi

        # Add to lists.
        prod_list="$prod_list
$index : ${prod_name}"
        index_list="$index_list
$index"
        return
    fi

    epn_tag=$1
    case "$title" in
    Rational\ Apex|*Apex/Summit* )
        _prod_name_="Rational Apex Common"
        ;;
    *C++* )
        _prod_name_="Rational Apex C/C++"
        ;;
    *95/83*|*Ada*|*VADS* )
        _prod_name_="Rational Apex Ada"
        ;;
    *ASIS* )
        _prod_name_="Rational ASIS"
        ;;
    *RCI* )
        _prod_name_="Rational RCI"
        ;;
    * )
        _prod_name_=`get_from_doc_description $epn_tag`
        ;;
    esac

    set -- 1 $_prod_name_
    shift

    case "$*" in
    ""|None|NONE )
        if [ "$epn_tag" = product_name ]
        then
            menu_sel="by $prev_epn_tag"
            case $prev_epn_tag in
            *_ada )
                prod_name="Rational Apex Ada"
                ;;
            *_c++ )
                prod_name="Rational Apex C/C++"
                ;;
            *_multi )
                prod_name="Rational Apex Duo"
                ;;
            *_summit )
                prod_name="Rational Summit"
                ;;
            *_nt )
                prod_name="Rational Apex/Ada NT"
                ;;
            * )
                menu_sel="by title"
                case "$title" in
                *RCI* )
                    prod_name="Rational RCI"
                    ;;
                *ASIS* )
                    prod_name="Rational ASIS"
                    ;;
                *Ada* )
                    prod_name="Rational Apex Ada"
                    ;;
                *C++*|*h++* )
                    prod_name="Rational Apex C/C++"
                    ;;
                *Duo* )
                    prod_name="Rational Apex Duo"
                    ;;
                *Summit* )
                    prod_name="Rational Summit TM"
                    ;;
                * )
                    prod_name="Rational Apex Common"
                    ;;
                esac
                ;;
            esac
        else
            prev_epn_tag=$epn_tag
            unset prod_name
            return
        fi
        ;;
    * )
        menu_sel="by $epn_tag"
        prod_name="$*"
        ;;
    esac

    case "$prod_name" in
    Rational\ Apex|*Apex/Summit )
        prod_name="Rational Apex Common"
        ;;
    esac

    set -- 1 `echo "$prod_list" | grep " : ${prod_name}\$"`
    if [ -n "$2" ]
    then
        # Already encountered this product.
        index=$2
        return
    fi

    # Determine order by product name categories.
    case "$prod_name" in
    *Common )
        com_counter=`expr ${com_counter:-0} + 1`
        index=10$com_counter
        ;;
    *Duo* )
        duo_counter=`expr ${duo_counter:-0} + 1`
        index=20$duo_counter
        ;;
    *95/83*|*Ada* )
        ada_counter=`expr ${ada_counter:-0} + 1`
        index=30$ada_counter
        ;;
    *C++* )
        cpp_counter=`expr ${cpp_counter:-0} + 1`
        index=40$cpp_counter
        ;;
    *Summit* )
        sum_counter=`expr ${sum_counter:-0} + 1`
        index=50$sum_counter
        ;;
    * )
        olp_counter=`expr ${olp_counter:-0} + 1`
        index=80$olp_counter
        ;;
    esac

    # Add to lists.
    prod_list="$prod_list
$index : ${prod_name}"
    index_list="$index_list
$index"
}

#------------------------------------------------------------------------
insert_cascade() {
#------------------------------------------------------------------------
# Only insert cascade if not already done so.

    $set_x
    submenu=manuals.$index
    if [ -f $tmp/cascade.$index ]
    then
        return
    fi

    echo "${insert}cascade *.help.manuals.$index \"$prod_name Manuals\"" \
        > $tmp/cascade.$index
}

#------------------------------------------------------------------------
add_pushbutton() {
#------------------------------------------------------------------------
    echo "append pushbutton *.help.${submenu}.help_$id \"$title\"
action help_$id
  apex_display help $link_ref
end_action"
}

#------------------------------------------------------------------------
add_cond_pushbutton() {
#------------------------------------------------------------------------
# Show pushbutton if the variant doc is active in the user's session.

    echo "#if APEX_LANG == $variant"
    add_pushbutton
    echo "#endif"
}

#------------------------------------------------------------------------
insert_pushbutton() {
#------------------------------------------------------------------------

    n=`expr $n + 1`
    case $id in
    clearcase* )
        # Show pushbutton if the ClearCase is enabled in the user's session.
        echo "#ifdef APEX_CLEARCASE_ENABLED" >> $tmp/$n
        add_endif=true
        ;;
    * )
        add_endif=false
        ;;
    esac

    case $variant in
    all )
        add_pushbutton >> $tmp/$n
        ;;
    * )
        add_cond_pushbutton >> $tmp/$n
        ;;
    esac

    $add_endif && echo "#endif" >> $tmp/$n

    echo "$n $title" >> $tmp/list
}

#------------------------------------------------------------------------
add_to_tmp_doc_list() {
#------------------------------------------------------------------------

    ps_title="$*"
    link_to=`get_from_doc_description link_to`
    for _lang in ${link_to:-$apex_lang_list}
    do
        echo "$ps_title" >> /tmp/$_lang.doc.list.$pid
    done
}

#------------------------------------------------------------------------
append_doc_list() {
#------------------------------------------------------------------------
# Append any .ps.Z files to the doc list.

    ps_files=`/bin/ls ${doc_dir}_ps/*.ps.Z 2>/dev/null`
    set -- `echo "$ps_files" | wc -w`
    n_ps_files=$1

    case $n_ps_files in
    0 )
        # Check for ps.Z files in sub-directories.
        ps_files=`/bin/ls $doc_dir/*_ps/*.ps.Z 2>/dev/null`
        set -- `echo "$ps_files" | wc -w`
        n_ps_files=$1
        # If 1 or more found, postcript files are in sub-directories.
        # Include subdirectory name in the title.
        case $n_ps_files in
        0 )
            ;;
        1 )
            ps_subdir=`dirname $ps_file`
            ps_subdir=`basename $ps_subdir _ps`
            add_to_tmp_doc_list "$ps_files $prod_name: $title ($ps_subdir)"
            ;;
        * )
            for ps_file in $ps_files
            do
                ps_subdir=`dirname $ps_file`
                ps_subdir=`basename $ps_subdir _ps`
                add_to_tmp_doc_list "$ps_file $prod_name: $title ($ps_subdir)"
            done
            ;;
        esac
        ;;
    1 )
        add_to_tmp_doc_list "$ps_files $prod_name: $title"
        ;;
    * )
        # When there is more than one postscript file, include the
        # name in the title to distinguish it from the others.
        for ps_file in $ps_files
        do
            ps_name=`basename $ps_file .ps.Z`
            add_to_tmp_doc_list "$ps_file $prod_name: $title ($ps_name)"
        done
    esac
}

#------------------------------------------------------------------------
set_Apex_Lang() {
#------------------------------------------------------------------------
    case $apex_lang in
    ada )
        Apex_Lang=Ada
        ;;
    c++ )
        Apex_Lang=C/C++
        ;;
    summit )
        Apex_Lang=Summit
        ;;
    nt )
        Apex_Lang=NT
        ;;
    multi )
        Apex_Lang=Duo
        ;;
    * )
        Apex_Lang=$apex_lang
        ;;
    esac

}

#------------------------------------------------------------------------
set_variant() {
#------------------------------------------------------------------------

    $set_x
    case $doc_path in
    */ada/* )
        variant=ada
        ps_variant=ada
        ;;
    */c++/* )
        variant=c++
        ps_variant=c++
        ;;
    */multi/*|*/duo/* )
        variant=multi
        ps_variant=multi
        ;;
    */summit/* )
        variant=summit
        ps_variant=summit
        ;;
    */nt/* )
        variant=nt
        ps_variant=nt
        ;;
    * )
        variant=all
        unset ps_variant
        ;;
    esac
}

#------------------------------------------------------------------------
set_prod_name() {
#------------------------------------------------------------------------

    $set_x
    case $variant in
    ada )
        extract_prod_name product_name_ada
        if [ -z "$prod_name" ]
        then
            extract_prod_name product_name
        fi
        ;;
    c++ )
        extract_prod_name product_name_c++
        if [ -z "$prod_name" ]
        then
            extract_prod_name product_name
        fi
        ;;
    multi )
        extract_prod_name product_name_multi
        if [ -z "$prod_name" ]
        then
            extract_prod_name product_name_duo
            if [ -z "$prod_name" ]
            then
                extract_prod_name product_name
            fi
        fi
        ;;
    summit )
        extract_prod_name product_name_summit
        if [ -z "$prod_name" ]
        then
            extract_prod_name product_name
        fi
        ;;
    nt )
        extract_prod_name product_name_nt
        if [ -z "$prod_name" ]
        then
            extract_prod_name product_name
        fi
        ;;
    * )
        extract_prod_name product_name
        ;;
    esac
    index_prod_name="($index:$prod_name $menu_sel)"
}

#------------------------------------------------------------------------
check_variant_tag() {
#------------------------------------------------------------------------

    $set_x
    test "$variant" = all && return

    if _variant=`grep '<<var.*>>' $doc_desc_file`
    then
        echo "Fixing <<variant>> in $doc_desc_file:" >&2
        sed -e "s?<<variant>>?$variant?" \
            -e "s?<<var>>?$variant?" \
            $doc_desc_file > $tmp_dd
        diff $doc_desc_file $tmp_dd >&2
        cp $tmp_dd $doc_desc_file
        /bin/rm -f $tmp_dd
    fi
}

#------------------------------------------------------------------------
make_menus_n_doc_list() {
#------------------------------------------------------------------------

    $set_x
    mkdir $tmp

    add_header menus

    n=0
    for doc_desc_file in `find_Doc_description`
    do
        id=`get_from_doc_description id`
        test -z "$id" -o "$id" = NONE && continue

        doc_dir=`dirname $doc_desc_file`

        case $doc_dir in
        *master_index )
            continue
            ;;
        esac

        link_ref=`get_from_doc_description link_ref`
        title=`get_from_doc_description title`
        set -- `/bin/ls -ld $doc_dir`
        shift `expr $# - 1`
        doc_path=$1

        if $Using_variants
        then
            set_variant
            set_prod_name
            check_variant_tag
        else
            variant=all
            unset ps_variant
            set_prod_name
        fi
        insert_cascade
        insert_pushbutton
        append_doc_list
    done

    if [ ! -s $tmp/list ]
    then
        rm $file_new
        return
    fi

    for index in `echo "$index_list" | sort +nr`
    do
        if [ -f $tmp/cascade.$index ]
        then
            if grep "help\.manuals\.$index" $tmp/[0-9]* >/dev/null 2>&1
            then
                cat $tmp/cascade.$index
                echo
            fi
        fi
    done

    previous_title=none
    sort +1 $tmp/list | while read n title
    do
        if [ "$title" = "$previous_title" ]
        then
            # Could be a duplicate.
            if diff $tmp/$n $tmp/$p >/dev/null 2>&1
            then
                # Yes it is, skip it.
                p=$n
                continue
            fi
        fi
        cat $tmp/$n
        echo
        previous_title="$title"
        p=$n
    done
    /bin/rm -fr $tmp
}

#------------------------------------------------------------------------
make_menus_docs() {
#------------------------------------------------------------------------

    display_title \
        "Checking if Apex Help Manuals menu and Print Manuals dialog box" \
        "${for_pn}are up-to-date..."

    cd $APEX_DOC
    menus_file=$APEX_HOME/editor_files/apex_menus

    init_editor_files

    make_menus_n_doc_list >> $file_new

    if [ -s $file_new ]
    then
        update_file  $file_new  $menus_file
    else
        $error "No documents found. The $menus_file file not created."
    fi
}

#------------------------------------------------------------------------
update_file() {
#------------------------------------------------------------------------

    $set_x
    tmp_file=$1
    target=$2
    if [ ! -s $tmp_file ]
    then
        /bin/rm -f $target
        return
    fi

    if [ -f $target ]
    then
        if diff $target $tmp_file > /dev/null
        then
            echo up-to-date - $target
        else
            /bin/rm -f $target.old
            /bin/cp $target $target.old
            /bin/rm -f $target
            /bin/cp $tmp_file $target
            echo updated - $target
        fi
    else
        /bin/cp $tmp_file $target
        echo created - $target
    fi
    /bin/rm -f $tmp_file
}

#------------------------------------------------------------------------
update_doc_list() {
#------------------------------------------------------------------------

    $set_x
    for apex_lang in $apex_lang_list
    do
        tmp_doc_list=/tmp/$apex_lang.doc.list.$pid
        if [ -s $tmp_doc_list ]
        then
            apex_doc_list=$APEX_DOC/$apex_lang.links/doc.list
            sed -e "s?.*\.links/??" $tmp_doc_list > $tmp_doc_list.sed
            sort -u $tmp_doc_list.sed > $tmp_doc_list.sortu
            sort +1 $tmp_doc_list.sortu > $tmp_doc_list.sort1
            update_file $tmp_doc_list.sort1 $apex_doc_list
            /bin/rm -f $tmp_doc_list $tmp_doc_list.*
        else
            /bin/rm -f $tmp_doc_list
        fi
    done
}

#------------------------------------------------------------------------
init_editor_files() {
#------------------------------------------------------------------------
# Create the editor_files sub-directory and the apex_menus.base file
# if it does not exist.

    $set_x
    if [ ! -d $prod_home/editor_files ]
    then
        mkdir $prod_home/editor_files
        if [ $? -ne 0 ]
        then
            exit 1
        fi
    fi

    if [ ! -f $prod_home/editor_files/apex_menus.base ]
    then
        if [ -f $prod_home/editor_files/apex_menus ]
        then
            cp $prod_home/editor_files/apex_menus \
               $prod_home/editor_files/apex_menus.base
        else
            touch $prod_home/editor_files/apex_menus.base
        fi
    fi
}

#------------------------------------------------------------------------
make_other_link() {
#------------------------------------------------------------------------

    link_dir=$1
    link_in=$2
    link_prefix=$3

    test -d $link_dir || return
    test -d $link_in  || return

    link_name=`basename $link_dir`
    if [ -d $link_in/$link_name ]
    then
        return
    elif [ $sym_link $link_in/$link_name ]
    then
        /bin/rm -f $link_in/$link_name
        check=`expr $check + 1`
    else
        new=`expr $new + 1`
    fi
    ln -s $link_prefix/$link_dir $link_in/$link_name
}

#------------------------------------------------------------------------
list_all_and_lang_manuals() {
#------------------------------------------------------------------------

    $set_x
    find all $apex_lang -type f -name Doc.description -print
    if $use_apex_startup
    then
        # Find manuals under each Apex Layered Product to link into
        # the Apex documents linkage directory (e.g. multi.links).
        cat $apex_startup | while read _pv _lp
        do
            for _lp_doc_dir in $_lp/doc/html/all $_lp/doc/html/$apex_lang
            do
                test -d $_lp_doc_dir &&
                    find $_lp_doc_dir -type f -name Doc.description -print
            done
        done
    fi
}

#------------------------------------------------------------------------
make_doc_links() {
#------------------------------------------------------------------------

    $set_x
    if [ -n "$PROD_NAME" ]
    then
        display_title "Checking on-line document links from Apex" \
                      "to $PROD_NAME..."
    else
        display_title "Checking on-line document links..."
    fi

    if $layered_product
    then
        touch $prod_doc/.use_apex_links
    fi

    new=0
    check=0
    for apex_lang in $apex_lang_list
    do
        if [ ! -d $APEX_DOC/$apex_lang.links ]
        then
            chmod u+w $APEX_DOC 2>/dev/null
            mkdir $APEX_DOC/$apex_lang.links || continue
        else
            # Check for obsolete links.
            set_Apex_Lang
            echo "  Checking for any obsolete $Apex_Lang on-line document links..."
            obs=`(
            cd $APEX_DOC
            find $apex_lang.links -type l -print |
            while read _link
            do
                if [ -d $_link/. ]
                then
                    continue
                else
                    case $_link in
                    */masterIX.doc.html )
                        ;;
                    * )
                        /bin/rm -f $_link
                        echo 1
                        ;;
                    esac
                fi
            done
            )`
            set -- 1 $obs
            obs=`expr $# - 1`
            case $obs in
            0 ) ;;
            1 ) echo "    Removed $obs obsolete link." ;;
            * ) echo "    Removed $obs obsolete links." ;;
            esac
        fi

        for doc_desc in `list_all_and_lang_manuals 2>/dev/null`
        do
            manual=`dirname $doc_desc`
            if [ $sym_link $manual ]
            then
                continue
            fi

            # For manuals in all, check the link_to directive.
            case $manual in
            all/*|*/all/* )
                link_to=`grep "[$WS]link_to[$WS]" $doc_desc`
                if [ -n "$link_to" ]
                then
                    echo " $link_to " | grep "[$WS]$apex_lang[$WS]" >/dev/null
                    if [ $? -ne 0 ]
                    then
                        # link_to directive does not have apex_lang.
                        continue
                    fi
                fi
                ;;
            esac

            outside_apex=false
            case $manual in
            /* )
                outside_apex=true
                ;;
            */*/* )
                manual=`dirname $manual`
                ;;
            esac
            manual_name=`basename $manual`
            apex_manual=$APEX_DOC/$apex_lang.links/$manual_name

            if [ $sym_link $apex_manual ]
            then
                /bin/rm -f $apex_manual
                check=`expr $check + 1`
            else
                new=`expr $new + 1`
            fi

            if $layered_product
            then
                ln -s $prod_doc/$manual $apex_manual
            elif $outside_apex
            then
                ln -s $manual $apex_manual
            else
                ln -s ../$manual $apex_manual
            fi

            manual_ps=${manual}_ps
            if [ -d $manual_ps/. ]
            then
                apex_manual_ps=${apex_manual}_ps
                if [ $sym_link $apex_manual_ps ]
                then
                    /bin/rm -f $apex_manual_ps
                    check=`expr $check + 1`
                else
                    new=`expr $new + 1`
                fi

                if $layered_product
                then
                    ln -s $prod_doc/$manual_ps $apex_manual_ps
                elif $outside_apex
                then
                    ln -s $manual_ps $apex_manual_ps
                else
                    ln -s ../$manual_ps $apex_manual_ps
                fi
            fi
        done
    done

    # Link special gif directories to specific manuals.
    if $layered_product
    then
        :
    else
        for apex_lang in $apex_lang_list
        do
            lang_dir=$apex_lang.links
            make_other_link  chars  $lang_dir/ada95_lrm   ../..
            make_other_link  chars  $lang_dir/reference   ../..
            make_other_link  chars  $lang_dir/tutorial    ../..
            make_other_link  chars  $lang_dir/users_dev   ../..

            for rogue in rogue_reference rogue_tools_h_users_guide \
                rogue_tools_h_reference rogue_users_guide
            do
                make_other_link  all/stdref/images  $lang_dir/$rogue  ../..
            done
        done
    fi

    # Link master indexes.
    for apex_lang in $apex_lang_list
    do
        mindex=$apex_lang.links/master_index
        test -d $mindex || continue
        test $sym_link $mindex/masterIX.doc.html && continue
        (
        cd $mindex
        ln -s masterIX.html masterIX.doc.html
        )
    done

    case $new in
        0 ) ;;
        1 ) echo "  Created $new link." ;;
        * ) echo "  Created $new links." ;;
    esac

    case $check in
        0 ) ;;
        1 ) echo "  Checked $check link." ;;
        * ) echo "  Checked $check links." ;;
    esac
}

#------------------------------------------------------------------------
make_html_links() {
#------------------------------------------------------------------------

    $set_x
    display_title "Checking if Apex on-line document links file" \
                  "${for_pn}is up-to-date..."
    not_found=true
    links=html_links
    for apex_lang in $apex_lang_list
    do
        links_file=$APEX_DOC/$apex_lang.links/$links
        cd $APEX_DOC/$apex_lang.links

        add_header links > $file_new
        if [ -f copyright/version.doc.html ]
        then
            echo Version copyright/version.doc.html >> $file_new
        fi

        for link_file in `find_Doc_html_links`
        do
            cat $link_file
        done >> $file_new

        if [ -s $file_new ]
        then
            update_file  $file_new  $links_file
            not_found=false
        fi
    done

    if $not_found
    then
        $error "No documents found."
        $error "The $prod_doc/$links file not created."
    fi
}

#------------------------------------------------------------------------
make_readme() {
#------------------------------------------------------------------------

    if [ -x $APEX_HOME/install/menu_setup ]
    then
        steps="cd $APEX_HOME/install
    ./menu_setup"
    else
        steps="$FILE $APEX_HOME"
    fi

    for apex_lang in $apex_lang_list
    do
        readme=$APEX_DOC/$apex_lang.links/README
        if [ -f $readme ]
        then
            continue
        fi

        echo "
This directory is used by the Apex documentation system to link Apex
and Apex layered product on-line documents in one place.  This is
necessary for the HTML links between all of the documents to work.
This directory is created by $CMD.  Commands to recreate this directory:

    $steps
" > $readme
    done
}

#------------------------------------------------------------------------
awk_html_index() {
#------------------------------------------------------------------------
# Does not handle <$nopage> <$startrange> <endrange>
# Does not handle [......]
# Does not handle symbols in a special way

$AWK 'BEGIN {
    prev_index=""
    is_two=0
    is_three=0
    is_four=0
    is_five=0
    level1=" "
    level2=" "
    level3=" "
    level4=" "
    level5=" "
    prev_first_letter=""
    prev_index=" "
    file="master_index_sym.html"
    nav_legend="<a href=\"legend.html\"><img border=\"0\" src=\"../../navbar/legend.gif\"></a>"
    nav_previous="<img border=\"0\" src=\"../../navbar/previous.gif\"></a>"
    nav_next="<img border=\"0\" src=\"../../navbar/next.gif\"></a>"
    nav_doc_list="<a href=\"../reference/Doc_list.html\"><img border=\"0\" src=\"../../navbar/doc.gif\"></a>"
    nav_bar="<img src=\"../../navbar/line.gif\"><br>"
    prev_file=""
    errors="errors.file"
    indent="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
  }
  {
   if (NF <= 3)
   {
     if ( NF > 0)
     {
       printf (" * * Error %s\n", $0) >> errors
     }
   }
   else
   {
     num=3
     index_text=$2
     while (num <= NF-2)
     {
       index_text = sprintf("%s %s",index_text, $(num))
       num=num+1
     }
     first_letter=toupper($1)
     #print first_letter
     if (first_letter<"A")
     {
       print "SYMBOLS NOT SUPPORTED IN THE MASTER INDEX", $0
     }
     else
     {

       #print index_text, first_letter, prev_index
       if (prev_index == index_text)
       {
         printf ("  <A HREF=\"%s\" TARGET=\"_top\">%s</a>\n",$(NF),$(NF-1), code) > file
       }
       else    # new index entry text
       {
         #printf ("Old %s, New:%s\n",prev_index,index_text)
         if (first_letter != prev_first_letter)
         {
           prev_file=file

           is_two=0
           is_three=0
           is_four=0
           is_five=0
           file=sprintf("master_index_%s.html",first_letter)
           if (prev_file != "")
           {
             printf ("</TABLE>") > prev_file
             printf ("<A HREF=\"%s\">%s</a>  ", file, nav_next) > prev_file
             print nav_legend > prev_file
             printf nav_doc_list > prev_file
             #printf ("<A HREF=\"%s\">%s</a>\n",master_thumb,nav_toc) > prev_file
             printf ("</BODY></HTML>\n") > prev_file
             close ( prev_file )
           }
           printf ("<HTML><HEAD><TITLE>Master Index - %s</TITLE></HEAD>",first_letter) > file
           printf ("<BODY BGCOLOR =#ffffff>") > file
           printf ("<BIG><B>Master Index - %s &nbsp;&nbsp;</B></BIG>\n",first_letter) > file
           #printf ("<a href=\"%s\">%s</a>  ",master_thumb,nav_toc) > file
           print nav_doc_list > file
           print nav_legend > file
           printf ("<a href=\"%s\">%s</a></pre>\n",prev_file,nav_previous) > file
           print nav_bar > file
           print "<TABLE>" > file
           #printf ("<h2><a name=\"%s\">%s</a></h2>",first_letter, first_letter) > file
           prev_first_letter=first_letter
         }
         prev_index=index_text
         if (is_two)
         {
           #printf ("\n") > file
           is_two=0
         }
         else if (is_three)
         {
           #printf ("\n") > file
           is_three=0
         }
         else if (is_four)
         {
           printf ("\n") > file
           is_four=0
         }
         else if (is_five)
         {
           printf ("\n") > file
           is_five=0
         }
         # printf ("<dt>") > file
         lev=split (index_text, levels, ":")
         if (lev==1)
         {
           #print "level", level, "level1: ", level1
           if (level1==levels[1])
           {
             printf ("<TR><TD>%s", indent) > file
           }
           else
           {
             printf ("<TR><TD><B>%s</B>",index_text) > file
           }
           printf ("&nbsp;&nbsp;<a href=\"%s\" TARGET=\"_top\">%s</a>\n",$(NF),$(NF-1)) > file
           level1=levels[1]
           level2=" "
           level3=" "
           level4=" "
           level5=" "
         }
         else if (lev==2)
         {
           #print "level", level, "level1: ", level1, "level2: ", level2
           if (level1==levels[1])
           {
             if (level2==levels[2])
             {
               #noop
             }
             else  # level 1 same, level2 different
             {
               printf ("<TR><TD>%s%s",indent,levels[2]) > file
             }

           }
           else  # level 1 different
           {
             printf ("<TR><TD><B>%s</B>\n<TR><TD>%s%s",levels[1],indent,levels[2]) > file
             level1=levels[1]
           }

           printf ("&nbsp;&nbsp;<a href=\"%s\" TARGET=\"_top\">%s</a>\n",$(NF),$(NF-1)) > file
           level2=levels[2]
           level3=" "
           level4=" "
           level5=" "
           is_two=1
         }

         # ** NEW
         else if (lev==3)
         {
           #print "levl: *", lev, "*level1: *", level1, "*level2: *", level2, "*level3: *", level3
           if (level1==levels[1])
           {
             if (level2==levels[2])
             {
               if (level3==levels[3])
               {
                 # noop
               }
               else # level 1, 2 same, level 3 different
               {
                 printf ("<TR><TD>%s%s%s",indent, indent, levels[3]) > file
               }
             }
             else  # level 1 same, level2 different
             {
               printf ("<TR><TD>%s%s<TR><TD>%s%s%s",indent,levels[2],indent,indent,levels[3]) > file
               level2=levels[2]
             }
           }
           else  # level 1 different
           {
             printf ("<TR><TD><B>%s</B><TR><TD>%s%s<TR><TD>%s%s%s",levels[1],indent,levels[2],indent, indent,levels[3]) > file
             level1=levels[1]
             level2=levels[2]
           }

           printf ("&nbsp;&nbsp;<a href=\"%s\" TARGET=\"_top\">%s</a>\n",$(NF),$(NF-1)) > file
           level3=levels[3]
           level4=" "
           level5=" "
           is_three=1

         }
         else if (lev==4)
         {
           #print "level", level, "level1: ", level1, "level2: ", level2
           if (level1==levels[1])
           {
             if (level2==levels[2])
             {
               if (level3==levels[3])
               {
                 if (level4==levels[4])
                 {
                   #noop
                 }
                 else # level 1, 2, 3 same, level 4 diff
                 {
                   printf ("<TR><TD>%s%s%s%s",indent, indent, indent, levels[4]) > file
                 }
               }
               else # level 1, 2 same, level 3 different
               {
                 printf ("<TR><TD>%s%s%s",indent, indent, levels[3]) > file
                 printf ("<TR><TD>%s%s%s%s",indent, indent, indent, levels[4]) > file
                 level3=levels[3]
               }
             }
             else  # level 1 same, level2 different
             {
               printf ("<TR><TD>%s%s",indent, levels[2]) > file
               printf ("<TR><TD>%s%s%s",indent, indent, levels[3]) > file
               printf ("<TR><TD>%s%s%s%s",indent, indent, indent, levels[4]) > file
               level2=levels[2]
               level3=levels[3]
             }
           }
           else  # level 1 different
           {
             printf ("<TR><TD><B>%s</B>",levels[1]) > file
             printf ("<TR><TD>%s%s", indent, levels[2]) > file
             printf ("<TR><TD>%s%s%s",indent, indent, levels[3]) > file
             printf ("<TR><TD>%s%s%s%s",indent, indent, indent, levels[4]) > file
             level1=levels[1]
             level2=levels[2]
             level3=levels[3]
           }

           printf ("&nbsp;&nbsp;<a href=\"%s\" TARGET=\"_top\">%s</a>\n",$(NF),$(NF-1)) > file

           level4=levels[4]
           level5=" "
           is_four=1
         }
         else #(level >5 or more)
         {
           printf ("!!! Warning: Too many levels in %s.\n", $0)
           print   "             This index entry will be skipped."
         }
       }
     }
   }
  }

  END {
    printf ("</TABLE>") > file
    print nav_bar > file
    print nav_doc_list > file
    print nav_legend > file
    printf ("</body></html>\n") > file

  }' $1
  # End of awk script
}

#------------------------------------------------------------------------
awk_sym() {
#------------------------------------------------------------------------
$AWK -F "[" '
  BEGIN {
    errors="errors.file"
  }

  {
    if (NF > 1)
    {
      rest=$2
      err=split(rest,a,"]")
      first_letter=substr(a[1],1,1)
      if (err > 1)
      {
        printf ("%s\~%s %s%s\n", a[1], first_letter, $1, a[2])
      }
      else
      {
        printf ("*** ERROR with line: %s\n", $0) >> errors
      }
    }
    else
    {
      if (substr($0, 1, 2) == "..")
      {
        printf ("*** ERROR with line: %s\n", $0) >> errors
      }
      else
      {
        first_letter=substr($0, 1, 1)
        printf ("%s   %s\n", first_letter, $0)
      }
    }
  }' $1
}

#------------------------------------------------------------------------
awk_symback() {
#------------------------------------------------------------------------
$AWK -F "~" '{
  if ( NF > 1 )
    print $2
  else
    print $0
  } ' $1
}

#------------------------------------------------------------------------
make_lang_master_dir() {
#------------------------------------------------------------------------
# Create index info file.

    $LOG
    if [ -d $master_dir ]
    then
        /bin/rm -f $master_dir/master_index_*.html
    else
        mkdir $master_dir
        $LOG " +++ Created $master_dir directory"
    fi

    $LOG "Done with master index pre-processing."
}

#------------------------------------------------------------------------
make_lang_index() {
#------------------------------------------------------------------------

    files_to_process=$master_dir/files_to_process
    master_file=master.ix.all
    need_file=master.ix.all
    prefix_file=.master_index_prefix

    for doc in $apex_lang.links/*
    do
        test -f $doc/$prefix_file  || continue

        if [ "`cat $doc/$prefix_file`" = "-" ]
        then
            $LOG "&&& Not including $doc in master index" >&2
            continue
        fi

        if [ -s $doc/$master_file ]
        then
            echo "$doc/$master_file"
        else
            $LOG "*** Missing $doc/$master_file" >&2
        fi
    done > $files_to_process
    test -s $files_to_process || return

    (
    # Sort, ignore leading blanks, mix upper and lower
    $LOG "--- Processing the following files ---"
    test "$LOG" = echo && sed -e 's?^?    ?' $files_to_process

    sort -fb `cat $files_to_process` |
        sed -e 's/<.*>//' > $temp2
    echo "s/\\\\xd5 /'/g" > $sedf
    sed -f $sedf $temp2 |
        sed -e 's!^_!-_!g;s!:! a: !g;s/ And / and /g' > $temp1
    awk_sym $temp1 |
        sort -fb > $temp2
    awk_symback $temp2 |
        sed -e 's!^\-_!_!g;s! a: !:!g' |
        sort -fbu > $temp1
    cd $master_dir
    /bin/rm -f errors.file
    awk_html_index $temp1

    display_title \
        "Created master index -" "$master_dir"
    if [ -f errors.file ]
    then
        $LOG "*** Errors found in master index:" `cat errors.file | wc -l `
        $LOG "... See $master_dir/errors.file for details"
        if [ "$LOG" != echo ]
        then
            /bin/rm -f errors.file
        fi
    fi
    )
}

#------------------------------------------------------------------------
make_lang_legend() {
#------------------------------------------------------------------------
# Create a master index legend of all the book files.

    doc_name_file=.doc_name
    prefix_file=.master_index_prefix

    $LOG
    $LOG "--- Creating legend for $apex_lang on `date`"
    legend_file=$apex_lang.links/master_index/legend.html

    (
    echo "
<html><head><title>Legend for the Master Index</title></head>
<body bgcolor=#ffffff>
<h1><center>Legend for the Master Index</center></h1><ul>"

    for doc in $apex_lang.links/*
    do
        test -f $doc/$prefix_file || continue
        $LOG "    .. in $doc" >&2
        cat $doc/$prefix_file | {
            read prefix
            if [ "$prefix" != "-" ]
            then
                cat $doc/$doc_name_file | {
                    read title
                    echo "<li><B>$prefix</B>&nbsp;&nbsp;-&nbsp;&nbsp;$title"
                }
            fi
        }
    done | sort -f
    echo "</ul></body></html>"
    ) > $legend_file
    echo "Created master legend - $legend_file"
}

#------------------------------------------------------------------------
make_master_index_and_legend() {
#------------------------------------------------------------------------

    for apex_lang in $apex_lang_list
    do
        if [ -d $apex_lang.links ]
        then
            master_dir=$apex_lang.links/master_index
            make_lang_master_dir
            make_lang_index
            make_lang_legend
        fi
    done
    if [ "$LOG" != echo ]
    then
        /bin/rm -f errors.file
    fi
}

#------------------------------------------------------------------------
set_apex_startup() {
#------------------------------------------------------------------------

    if [ -f $prod_home/.patch_level ]
    then
        apex_pl=`cat $prod_home/.patch_level`
        _startup_n1=apex.${apex_version}$apex_pl.startup
        _startup_n2=apex.$apex_version.startup
    else
        unset _startup_n1
        _startup_n2=apex.$apex_version.startup
    fi

    _dir1=`dirname $APEX_HOME`
    _dir2=`dirname $_dir1`
    for _config in $_dir1/config $_dir2/config
    do
        for _startup in $_startup_n1 $_startup_n2
        do
            if [ -s $_config/$_startup ]
            then
                use_apex_startup=true
                apex_startup=$_config/$_startup
                break 2
            fi
        done
    done
    unset _startup_n1 _startup_n2 _dir1 _dir2 _startup _config
}

#------------------------------------------------------------------------
cleanup() {
#------------------------------------------------------------------------

    /bin/rm -fr $tmp
    /bin/rm -f /tmp/*.doc.list.$pid $temp1 $temp2 $sedf $file_new $tmp_dd
    exit $1
}

#------------------------------------------------------------------------
# MAIN
#------------------------------------------------------------------------
while true
do
    case "$1" in
    -a* )
        if [ -d "$2" ]
        then
            APEX_HOME=$2
            shift
        else
            $error "$CMD: Option '$1' problem. $2 not found."
            Usage 1
        fi
        ;;
    -ch )
        Do_ch=true
        No_c_opt=false
        ;;
    -ci )
        Do_ci=true
        No_c_opt=false
        LOG=echo
        ;;
    -cl )
        Do_cl=true
        No_c_opt=false
        ;;
    -cm )
        Do_cm=true
        No_c_opt=false
        ;;
    -f* )
        error=':'
        ;;
    -h* )
        Usage 0
        ;;
    -log )
        LOG=echo
        ;;
    -lp )
        LP_HOME=$2
        prod_home=$2
        layered_product=true
        shift
        ;;
    -pn )
        PROD_NAME="$2"
        for_pn="for $2 "
        shift
        ;;
    -x )
        set_x='set -x'
        $set_x
        ;;
     /*|"" )
         break
         ;;
    * )
        $error "$CMD: option '$1' not recognized."
        Usage 1
        ;;
    esac
    shift
done

if [ -n "$LP_HOME"  -a  -z "$APEX_HOME" ]
then
    $error "The -lp option requires APEX_HOME be set or specify the -a option."
    Usage 1
fi

if [ $# -eq 1 ]
then
    prod_home=$1
fi

if [ -z "$prod_home" ]
then
    if [ -n "$APEX_HOME" ]
    then
        prod_home=$APEX_HOME
    else
        $error "$CMD: APEX_HOME not set."
        Usage 1
    fi
fi

if [ ! -d $prod_home ]
then
    $error "$prod_home not found."
    exit 1
fi

prod_doc=$prod_home/doc/html
if [ ! -d $prod_doc ]
then
    $error "$prod_doc not found."
    exit 0
fi

if [ -f $prod_home/.apex_version ]
then
    APEX_HOME=$prod_home
    apex_version=`cat $prod_home/.apex_version`
    set_apex_startup
fi

if [ -z "$APEX_HOME" ]
then
    $error "$CMD: APEX_HOME not set."
    $error "$CMD: Either set APEX_HOME or use -a option."
fi

APEX_DOC=$APEX_HOME/doc/html
if [ ! -d $APEX_DOC ]
then
    $error "$CMD: $APEX_DOC not found."
    exit 1
fi

# Make a list of language variants that were installed.
Using_variants=false
for apex_lang in $def_apex_lang_list
do
    if  [ -d $APEX_DOC/$apex_lang ]
    then
        Using_variants=true
        apex_lang_list="$apex_lang_list $apex_lang"
    fi
done
$Using_variants || apex_lang_list=multi

cd $prod_doc
chmod u+w . * 2>/dev/null

# Set O/S specific parameters and command names.
sym_link='-h'
AWK=nawk
case `uname -s` in
OSF1 )
    sym_link='-L'
    ;;
AIX )
    sym_link='-L'
    ;;
HP-UX )
    AWK=awk
    ;;
esac

trap 'cleanup 0' 0
trap 'cleanup 1' 1 2 3 4 15

if $No_c_opt
then
    # If no -c? options specified, then turn all true.
    Do_ch=true
    Do_ci=true
    Do_cl=true
    Do_cm=true
fi

if $Do_cl
then
    # Create/check links in the *.links directories.
    make_doc_links
    # Plant a README file in each of these .links directories.
    make_readme
fi

if $Do_cm
then
    # Create/update the apex_menus file and the doc.list files
    make_menus_docs
    update_doc_list
fi

if $Do_ci
then
    # Create/update the master index files.
    make_master_index_and_legend
fi


if $Do_ch
then
    # Create/check html_links file in the *.links directories.
    make_html_links
fi
