#!/bin/sh

show_usage ()
{
    echo ""
    echo "Usage: `basename $0` [-el] [-j jarSource][-n entryNbr ]  java-class-name"
    echo ""
    echo "  The purpose of this utility is to display java"
    echo "  source code from a java archive file (.jar)."
    echo ""
    echo "  -e temporary extraction of class file, for editor access."
    echo ""
    echo "  -j jar file to examine (default: $defaultjar)"
    echo ""
    echo "  -l list all names in archive which match java-class-name"
    echo ""
    echo "  -n allows selection of which class to expand when -l option"
    echo "     shows more than one match."
    echo ""
    exit 0
}

#-------------------------------------------------------------------------------

isAccessible ()
{
    testFile=$1
    
    #-------------------------------------------------------------------
    # To determine whether or not a file can be placed into a directory,
    # make an attempt to do so (discarding error message, for failures)
    #-------------------------------------------------------------------
    `echo accessibleTest 2>/dev/null >$testFile`
    accessibleStatus=$?

    #-----------------------------------------
    # If the test file was actually created...
    #-----------------------------------------
    if [ -f $testFile ]; then
        rm $testFile  # ... remove it
    fi
    
    return $accessibleStatus
}

#-------------------------------------------------------------------------------

getDestination ()
{
    dirNames="$*"
    directory=""
    
    for name in $dirNames; do
        testName=$name/.junk.
        isAccessible $testName
        
        #------------------------------------
        # When the directory is accessible,
        # a valid destination has been found.
        #------------------------------------
        if [ $? -eq 0 ]; then
            directory=$name
            break
        else
            #-----------------------------------
            # When the attempt to put a file
            # into the directory fails, make
            # an attempt to create the directory
            # in case it did not already exist.
            #-----------------------------------
            if [ ! -d $name ]; then
                mkdir -p $name 2>/dev/null

                if [ $? -eq 0 ]; then
                    directory=$name
                    break
                fi
            fi
        fi
    done
    
    echo $directory
}

#-------------------------------------------------------------------------------

getComponentName ()
{
    echo $5
}

#-------------------------------------------------------------------------------

defaultjar=/usr/java/src.jar

if [ $# -eq 0 ]; then
    show_usage
else
    nbr=1
    searchJar=0
    limitList=0
    extractFile=0
    
    #---------------------
    # Parse script options
    #---------------------
    while [ "$1" != "" ]; do
        case $1 in
            -e)  extractFile=1;;
            -j)  defaultjar=$2; shift;;
            -l)  searchJar=1;;
            -n)  nbr=$2; shift; limitList=1;;
            -*)  show_usage;;
             *)  break;;
        esac
        shift
    done
    
   fileName="$1.java"
   
   if [ $searchJar -eq 1 ]; then
        if [ $limitList -eq 1 ]; then
            unzip -l $defaultjar|grep $fileName|grep -n .|grep "^$nbr:"
        else
            unzip -l $defaultjar|grep $fileName|grep -n .
        fi
    else
        jarComponent=`unzip -l $defaultjar | grep /$fileName | grep -n .|grep "^$nbr:"`

        if [ "$jarComponent" != "" ]; then
            componentName=`getComponentName $jarComponent`

            #--------------------------------
            # Make file available to editing?
            #--------------------------------
            if [ $extractFile -eq 1 ]; then
                defaultDir="/tmp/$USER"
                location=`getDestination $HOME $defaultDir`
                
                if [ "$location" = "" ]; then
                    echo Could not create temporary file for viewing $fileName.
                else
                    pathToFile="$location/$fileName"
                    unzip -p $defaultjar $componentName > $pathToFile
                    less $pathToFile
                    tempDir=`dirname $pathToFile`
                    
                    #---------
                    # Cleanup.
                    #---------
                    if [ "$tempDir" = "$defaultDir" ]; then
                        rm -rf $tempDir
                    else
                        rm $pathToFile
                    fi
                fi
            else
                unzip -p $defaultjar $componentName |less
            fi
        else
            echo Could not find $fileName in $defaultjar
            exit 1
        fi
    fi
fi

