#!/bin/csh -f
#
# All Rights Reserved. Copyright (C) 2003, Hitachi, Ltd.
#

# Set messages
set UsageMsg = "usage : $0:t core-filename loadmodule-name [out-filename] [-l(library-name)...]"
set CantOpenMsg = "$0:t : Cannot open"
set NoFileMsg = ": No such file or directory"
set PermitMsg = ": Permission denied"
set CantCreateMsg = "$0:t : Cannot create"
set StartSeraphMsg = "now processing..."
set EndOkMsg = "done"
set UnmatchWarningMsg = "WARNING : core file may not match loadmodule"
set CantFindSeraphMsg = "$0:t : Intarnal error occur : Cannot find seraph :"
set SeraphErrorMsg = "$0:t : Intarnal error occur : recover core file:"
set CantCreateWorkDir = "$0:t : Cannot create working directory on /tmp : Permission denied"
set BadArgumentMsg = "$0:t : Bad argument :"
set IsDirectoryMsg = "is directory"
set AlreadyExistMsg = ": Already exist"
set IllegalOptMsg = "$0:t : illegal option --"

# Set seraph name
set SeraphName = "linaph"

# Set other environ
set CommandPath0 = "/usr/bin"
set CommandPath = "/bin"
set DefOutFileName = "javatrace.log"
setenv SHL_LOAD 1

# Check number of arguments
if( !( ( $#argv == 2 ) || ( $#argv >= 3 ) ) ) then
    echo "$UsageMsg"
    goto ERROR_END
endif

# Set and check real seraph name
set WhichSeraph = `which "$SeraphName"`
set WhichOut = `echo "$WhichSeraph" | awk '{ print $2 }' -`
if ( "$WhichOut" == "" ) then
    set SeraphCompName = `which "$SeraphName"`
else
    set SeraphCompName = "$0:h/$SeraphName"
endif
if( !( -e "$SeraphCompName" ) ) then
    echo "$CantFindSeraphMsg $SeraphCompName"
    goto ERROR_END 
endif

set isError = 0

# Check first argument
if ( -e "$1" ) then
    if ( !(-r "$1" ) ) then
        echo "$CantOpenMsg $1 $PermitMsg"
        set isError = 1
    endif
    if ( -d "$1" ) then
        echo "$BadArgumentMsg $1 $IsDirectoryMsg"
        set isError = 1
    endif
else
    echo "$CantOpenMsg $1 $NoFileMsg"
    set isError = 1
endif

# Check second argument
set WhichSecondArg = `which "$2"`
set WhichOut = `echo "$WhichSecondArg" | awk '{ print $2 }' -`
if ( "$WhichOut" == "" ) then
    set LoadmoduleName = `which "$2"`
else
    set LoadmoduleName = "$2"
endif
if ( -e "$LoadmoduleName" ) then
    if( !(-r "$LoadmoduleName" ) ) then
        echo "$CantOpenMsg $LoadmoduleName $PermitMsg"
        set isError = 1
    endif
    if ( -d "$LoadmoduleName" ) then
        echo "$BadArgumentMsg $LoadmoduleName $IsDirectoryMsg"
        set isError = 1
    endif
else
    echo "$CantOpenMsg $LoadmoduleName $NoFileMsg"
    set isError = 1
endif

if( $isError != 0 ) then
    goto ERROR_END
endif

# Check third argument
if ( $#argv >= 3 ) then
    set OptName = `echo "$3" | awk '/^-./ { print substr($1,2,1) }'`
    if ("$OptName" != "") then
        if ("$OptName" == "l") then
            set FirstOptNum = 3
        else 
            echo "$IllegalOptMsg $OptName"
            goto ERROR_END
        endif
    else
        set FirstOptNum = 4
    endif
else
    set OptName = ""
    set FirstOptNum = 0
endif

# Set OutFileName
if ( ($#argv >= 3) && ("$OptName" != "l") ) then
    set OutFileName = "$3"
else
    set OutFileName = "$DefOutFileName"
endif
if( -e "$OutFileName" ) then
    echo "$CantCreateMsg $OutFileName $AlreadyExistMsg"
    set isError = 1
else
    set OutDir = "$OutFileName:h"
    if( "$OutDir" == "$OutFileName" ) then
        set OutDir = `$CommandPath/pwd`
    endif
    if ( !( -w "$OutDir/." ) ) then
        echo "$CantCreateMsg $OutFileName $PermitMsg"
        set isError = 1
    endif
endif

if( $isError != 0 ) then
    goto ERROR_END
endif

# Check other arguments
set CurrentArgNum = $FirstOptNum
set lFlag = 0
set NewSeraphArg = ""
while ( ($CurrentArgNum >= 3) && ($CurrentArgNum <= $#argv) )
    set OptName = `echo "$argv[$CurrentArgNum]" | awk '/^-./ { print substr($1,2,1) }'`
    if ("$OptName" != "") then
        if ("$OptName" == "l") then
            set lFlag = 1
        else
            echo "$IllegalOptMsg $OptName"
            goto ERROR_END
        endif
        set LibName = `echo "$argv[$CurrentArgNum]" | awk '/^-./ { print substr($1,3) }'`
    else
        if ($lFlag == 0) then
            echo "$UsageMsg"
            goto ERROR_END
        endif
        set LibName = "$argv[$CurrentArgNum]"
    endif
    # Check library-name
    if ( "$LibName" != "" ) then
        if ( -e "$LibName" ) then
            if ( !(-r "$LibName" ) ) then
                echo "$CantOpenMsg $LibName $PermitMsg"
                goto ERROR_END
            endif
            if ( -d "$LibName" ) then
                echo "$BadArgumentMsg $LibName $IsDirectoryMsg"
                goto ERROR_END
            endif
        else
            echo "$CantOpenMsg $LibName $NoFileMsg"
            goto ERROR_END
        endif
        # Change ARG to ABS_ARG
        set AbsLibName = `echo "$LibName" | awk '/^[\/]+/ {print} /^[^\/]+/ {printf("%s/%s\n",dir,$0)}' "dir=$cwd" -`
        if ( -e "$AbsLibName" ) then
            set NewSeraphArg = `echo "$NewSeraphArg $AbsLibName"`
        endif
    endif

    @ CurrentArgNum ++
end

#
# Change ARG to ABS_ARG
#
# Change first ARG
set AbsFirstArg = `echo "$1" | awk '/^[\/]+/ {print} /^[^\/]+/ {printf("%s/%s\n",dir,$0)}' "dir=$cwd" -`

# Change Second ARG
set AbsSecondArg = `echo "$LoadmoduleName" | awk '/^[\/]+/ {print} /^[^\/]+/ {printf("%s/%s\n",dir,$0)}' "dir=$cwd" -`

# Change third ARG
set AbsThirdArg = `echo "$OutFileName" | awk '/^[\/]+/ {print} /^[^\/]+/ {printf("%s/%s\n",dir,$0)}' "dir=$cwd" -`

# Change Seraph name
set AbsSeraphCompName = `echo "$SeraphCompName" | awk '/^[\/]+/ {print} /^[^\/]+/ {printf("%s/%s\n",dir,$0)}' "dir=$cwd" -`

#
# Move to working directory
#
set WorkDir = "/tmp/javatrace.`$CommandPath/date +%Y%m%d%H%M%S`"
while ( -e "$WorkDir" )
    set WorkDir = "/tmp/javatrace.`$CommandPath/date +%Y%m%d%H%M%S`"
end
$CommandPath/mkdir "$WorkDir"
if( !( -e "$WorkDir" ) ) then
    echo "$CantCreateWorkDir"
    goto ERROR_END
endif
onintr ERROR_RMDIR
set PrevDir = "`$CommandPath/pwd`"
cd "$WorkDir"
onintr ERROR_POP_RMDIR

#
# Call seraph
#
echo "$StartSeraphMsg"
limit coredumpsize 0

echo "quit" | $AbsSeraphCompName -C $AbsFirstArg $AbsSecondArg $NewSeraphArg >& $AbsThirdArg
set SeraphCommandWarning = `grep "WARNING:" $AbsThirdArg | grep "symbol not loaded"`
set SeraphAbort = `$CommandPath/grep "mdmp ABORT" $AbsThirdArg`
set SeraphError = `$CommandPath/grep ERROR $AbsThirdArg`
set SeraphNotCore = `$CommandPath/grep " unknown core type" $AbsThirdArg`
set SeraphRenameInfo = `$CommandPath/grep " current directory in core renamed" $AbsThirdArg | $CommandPath/awk '{ print $9 }' -`
if ( "$SeraphCommandWarning" != "" ) then
    set NameFromCore = `echo "$SeraphCommandWarning" | $CommandPath/awk '{ print $3 }' -`
    if( "$NameFromCore:t" != "'$AbsSecondArg:t'" ) then
        echo -n "$UnmatchWarningMsg"
        echo " (core file from $NameFromCore)"
    endif
endif
if ( ("$SeraphAbort" == "") && ("$SeraphError" == "") && ("$SeraphNotCore" == "") ) then
    if ( "$SeraphRenameInfo" == "" ) then
        echo "$EndOkMsg"
    else if ( !( -e "$SeraphRenameInfo" ) ) then
        echo "$EndOkMsg"
    endif
else
    $CommandPath/grep " unknown core type" "$AbsThirdArg"
    $CommandPath/grep ERROR "$AbsThirdArg"
    $CommandPath/grep "mdmp ABORT" "$AbsThirdArg"
endif

if ( "$SeraphRenameInfo" != "" ) then
    if ( -e "$SeraphRenameInfo" ) then
        echo "$SeraphErrorMsg $1 <- $SeraphRenameInfo"
        $CommandPath/mv -f "$SeraphRenameInfo" "$AbsFirstArg"
        goto ERROR_POP_RMDIR
    endif
endif

END:
    cd "$PrevDir"
    if ( -e "$WorkDir/core" ) then
      rm -f "$WorkDir/core"
    endif
    $CommandPath/rmdir "$WorkDir" 
    exit 0

ERROR_END:
    exit 1

ERROR_RMDIR:
    if ( -e "$WorkDir/core" ) then
      rm -f "$WorkDir/core"
    endif
    $CommandPath/rmdir "$WorkDir"
    exit 1

ERROR_POP_RMDIR:
    cd "$PrevDir"
    if ( -e "$WorkDir/core" ) then
      rm -f "$WorkDir/core"
    endif
    $CommandPath/rmdir "$WorkDir"
    exit 1

