#!/bin/ksh

# A simple script to gather up all the lib's referenced from a core file
# it bundles them up in a compressed tar file to be sent with the core file
# to service team, inorder to be used with the edit_core cmd.
#
# This needs the checkcore program as can be found on javaserv
#
# It outputs a file called core-libs.tar.Z
#
# Author: Ben Hardill
# email : hardillb@uk.ibm.com
# version: 0.1 09/12/2002
# updated: 1/8/2002 Roger Leuckie -> embedded checkcore
#

#
# if checkcore does not exist in current directory
# then create it
#

if [ ! -f ./checkcore ]; then
touch -am 1231235999 $$.touch >/dev/null 2>&1
if test ! -f 1231235999 && test -f $$.touch; then
  shar_touch=touch
else
  shar_touch=:
  echo 'WARNING: not restoring timestamps'
fi
rm -f 1231235999 $$.touch
#
# ============= checkcore ==============
if test -f 'checkcore' && test X"$1" != X"-c"; then
  echo 'x - skipping checkcore (File already exists)'
else
  echo 'x - extracting checkcore (binary)'
  sed 's/^X//' << 'SHAR_EOF' | uudecode &&
begin 600 checkcore
M`=\`!#F]#1<```````````!($`<!"P`!```.$````A```&JL(``0:!```0`@
M``\0(``0?``"``$``@`"``0``P`%``,Q3```````````````````````````
M```N=&5X=````!```0`0``$````.$````0`````````````````````@+F1A
M=&$````@``\0(``/$````A````\0````````````````````0"YB<W,`````
M(``1("``$2```&JL`````````````````````````(`N;&]A9&5R````````
M```````$&P``$2```````````````````!```````((B`"A\;AMX?(\C>'RP
M*WA\'O@`D<(``)'B``2",0``@4(`+#D@``!E*00#+1$``)$J``!`@@`02```
MD6````!(```4@^(`.$@``9&`00`4?&X;>$&*`!R`$0``D$$`%'P)`Z:`40`$
M3H`$(8!!`!2`X@!`.0```'W#<WA]Y'MX?@6#>)$'``!(``D18````(#B`"`L
M!P``08(`"(!B`"1(``U)@$$`%'R!"`@```````P@0`````````"L``=?7W-T
M87)T````?`@"IH!B``B4(?_`D$$`%)`!`$B``@`,+(,``(""`#`L````@*,`
M`(%C``A!@@`(D`0``'RH`Z9!A@"@@$,`!$Z``"&`00`4@&(`$"P#``"``P``
M@6,`"'P(`Z9!@@`0@$,`!$Z``"&`00`4@&(`%"P#``"``P``@6,`"'P(`Z9!
M@@`0@$,`!$Z``"&`00`4@&(`&"P#``"``P``@6,`"'P(`Z9!@@`0@$,`!$Z`
M`"&`00`4@&(`'"P#``"``P``@6,`"'P(`Z9!@@`0@$,`!$Z``"&`00`4@8$`
M2#@A`$!]B`.F3H``(````````"!!@````````.@`#E]?=&AR96%D<U]I;FET
M@```-`````"!@@`\D$$`%(`,``"`3``$?`D#IDZ`!"````````R@````````
M```8?`@"IK\!_^"0`0`(E"'_D']D,!"0X0!$.^```&-C``!(``?I@$$`%'Q\
M&WE`@@`D@8$`>(-A`%R#@0!@."$`<'V(`Z8X8``!@^'__$Z``""`8@!(@`,`
M`"P```$L@``"+P```T&"`TQ!A@,\09H`/(!B`$R`@@!0.(0`L#AC`$!(``>Q
M@$$`%(&!`'B#80!<@X$`8#@A`'!]B`.F.&```8/A__Q.@``@@&(`6(/#`!2`
M80!$.(```#B@``!(``>=@$$`%(!A`$1CQ```.*```$@`!XF`00`4@&$`1&.$
M``!C90``2``'G8!!`!1\`]A`08(`+(!B`%`X8P!`2``'K8!!`!2!@0!X."$`
M<+MA_^Q]B`.F.&```4Z``""#P@!H@!X``"P```!!@@`X8YT``(."`&R#8@!P
M@R(`=#M```&`G```@!X``"P$``"0FP``.'@`&"R```!!@@!\2```-&.8``"#
M@@!L@V(`<(,B`'0[0``!@)P``(`>```L!```D)L``#AX`!@L@```08(`2("$
M``1!A@`(.'T`,$@`!T5@````+`,``(`>```X>``8+(```$&"`!B`FP``@(0`
M#)";```L!```2___Q"P:``!(```(+!\``("<```X8``00((!#"P$``!!@@`\
M2``&'8!!`!2`G```D&0`"(!\``"`@P`(D&0`#(!\``"`@P`(D)P``"P$```X
M?0`P08(`<$@``"1(``7E@$$`%)!\``!@9```D'D``"P$```X?0`P08(`3(`>
M```L````08(`*$@`!\&`00`4@)P``)!D``"`G```@&0``"P#``!!@@"`2```
M*#AX`!A(``>9@$$`%("<``"09```@)P``(!D```L`P``08(`6$@`!7&`00`4
M@+P``#B=`#"090`$@!X``(!\```L````@&,`!$&"`"!(``>!8````("<``"`
M'@``+(```$&&`#1(```@.)@`&$@`!V%@````@)P``(`>```L@```088`%(`=
M``!\```T5`#9?T@``!"`&```?```-%0`V7]`@@`X088`$(`=``!_O0(52```
M#(`8``!_&`(508(`'(`>```L!```D)L``#AX`!@L@```2__^1(!B`%`L!```
M@(0`!$&"`&1(``?M@$$`%(!\``"`8P`$2``(!8!!`!2`?```@&,`#)!\``"`
M8P`(2``'[8!!`!2`8@!0@)P``"P$``"`A``$08(`($O__[R`8@"(@\,`%$O_
M_02`8@",@\,`!$O__/@X8```@8$`>#@A`'"[`?_@?8@#IDZ``"`````````@
M08`(!0`````````#U``(<F5A9$QI<W0``+]!_^A\"`*F@^(`2#O```$XH```
MD`$`")0A_R`X@```D&$`N)/?``!(``=]@$$`%(.B`)0L`___.($`0("B`%"0
M?0``@&$`N$""`"@X90`02``$98!!`!2!@0#H."$`X+NA__1]B`.F.&```4Z`
M`"!(``==@$$`%"P#``"#@@",@T(`G(`!`%QCA```@'T``#B@2$"`P@!008(`
M+#AF``1(``05@$$`%(&!`.B#00#(.&```3@A`.!]B`.FNX'_\$Z``""0&@``
M2``#Q8!!`!2@'``"*`-(0(!]```X@```+(```#B@``"`P@!008(`+#AF`!Q(
M``/!@$$`%(&!`.B#00#(.&```3@A`.!]B`.FNX'_\$Z``"!`A@#\2``#28!!
M`!2#8@"(.````I`?``"`?0``8V0``#B@"G!(``-1@$$`%(`;!"`H`PIP08(`
M+(!B`%`X8P`H2``#78!!`!0X8``!@8$`Z#@A`."[0?_H?8@#ID@``@AP```!
M@)L`!#Q@#^\X`]VR?(0``$&"``R`8@!HD\,``$"&`'PX```#@'T``)`?```X
M@```.*```$@``K6`00`4@'T``(-B`%@XH!?@8V0``$@``L6`00`4*`,7X$&"
M`"R`8@!0.&,`-$@``M6`00`4.&```8&!`.@X(0#@NT'_Z'V(`Z9(``&`@!L$
M;'````%!@@`,@&(`:)/#``"`'P``+````2R```(O```#08(`.$&&`)!!F@"P
M@&(`3(""`%`XA`"(.&,`0$@``?V`00`4.&```8&!`.@X(0#@NT'_Z$@``2"`
MW$@L@)P`!#A@2$`X````+`8``$&"``PXH```2```D(#<``@L!@``0(+_\(#<
M2#0L!@``0(+_Y(#<2#PL!@``0(+_V(#:``!\Q?YP+`4``"B%``!`@@!<2```
M5("B`(@X````.&`*<("%`!2`Q0`<@*4`&'S&(!1\I0&42```)("B`%@X````
M.&`7X("%`!2`Q0`<@*4`&'S&(!1\I0&4+`4``"B%``!`@@`(?(8@0$"%`"1\
M!!A`08``'(!Z``!\9_YQ?(`X0$""``A\A!A`080`-(!B`$R`@@!0.(0`3#AC
M`$!(``#]@$$`%#A@``&!@0#H."$`X+M!_^A]B`.F2```((#]```X8```2__X
MI8&!`.@X(0#@NT'_Z'V(`Z9.@``@````````($&`!@$``````````X``"W!R
M;V-E<W-#;W)E````+`,``GP(`J:`8@!0E"'_P)`!`$@X8P!D08(`($@``Y6`
M00`4.&```8&!`$@X(0!`?8@#IDZ``"`X(0!`@&0`!$O__!R!@0!(."$`0'V(
M`Z9.@``@````````($&```(``````````%0`!&UA:6X``(&"`$2000`4@`P`
M`(!,``1\"0.F3H`$(```````#*```````````!B!@@!4D$$`%(`,``"`3``$
M?`D#IDZ`!"````````R@```````````8@8(`7)!!`!2`#```@$P`!'P)`Z9.
M@`0@```````,H```````````&(&"`&"000`4@`P``(!,``1\"0.F3H`$(```
M````#*```````````!B!@@!DD$$`%(`,``"`3``$?`D#IDZ`!"````````R@
M```````````8`````````````````````(%B`'B!BP`$.```"'&,``]!@@"T
M?`$#IGR@&BM\X"(J08(`2'R%.$!_!D!`0(8`?$&:``Q,F```2```<'RC`BM\
MY`(J08(`)#````A\A3A`?P9`0'RC`BM`A@!00)H`1'SD`BI`@O_D?`$"IBP`
M``0@```(5``>^$&!`!1\I00P?.<$,'R%.$!(```<?(4X0'S&!#!]"`0P?P9`
M0$"&``Q,F```088`&$&%``PX8/__3H``(#A@``%.@``@.&```$Z``"`X8___
M.(3__XBC``&(Q``!+`4``'R%,$!!@@`<C*,``4"&`!B,Q``!+`4``'R%,$!+
M___H088`$$&%`!0X8/__3H``(#A@``!.@``@.&```4Z``"````````P@0```
M``````$8``9S=')C;7"!@@!\D$$`%(`,``"`3``$?`D#IDZ`!"````````R@
M```````````8``````````"!8@!X@8L`!#@``"!QC``/08(`*'P!`Z9\H"(K
M?*`=*DV"`"!\I`(K?*,%*C```"!`@O_T3H``('"+``,]0']_(6L`!#E*?W]]
M:0.F?&4;>$&"`""(Q```+`8``)C%```XA``!38(`(#BE``%"`/_H@.0``#"E
M__Q\Z5`X?2E0%'TL4/E`@@`TA00`!)3E``1]"5`X?2E0%'TL4/E`@@`LA.0`
M!)4%``1\Z5`X?2E0%'TL4/E!@O_0?2DX^'TI4'E!@O_$2```%'TI0/A]*5!Y
M08+_S'T'0W@XI0`#=.;_`%3G0#Z<Y0`!0(+_]$Z``"````````P@0```````
M``#P``9S=')C<'F!@@"`D$$`%(`,``"`3``$?`D#IDZ`!"````````R@````
M```````8@8(`A)!!`!2`#```@$P`!'P)`Z9.@`0@```````,H```````````
M&(&"`)"000`4@`P``(!,``1\"0.F3H`$(```````#*```````````!B!@@"8
MD$$`%(`,``"`3``$?`D#IDZ`!"````````R@```````````8@8(`H)!!`!2`
M#```@$P`!'P)`Z9.@`0@```````,H```````````&$`H(RDV,0DQ+C$T("!S
M<F,O8F]S+W5S<B]C8W,O;&EB+VQI8F,O7U]T:')E861S7VEN:70N8RP@;&EB
M8W1H<F0L(&)O<S4P,"P@,#`R.4%?-3`P(#<O,3$O,#`@,3(Z,#0Z,30`````
M(``0?"``$'```````````"5S"@!C:&5C:V-O<F4```!C:&5C:V-O<F4```!C
M:&5C:V-O<F4Z``!C:&5C:V-O<F4Z``!C:&5C:V-O<F4Z``!C:&5C:V-O<F4Z
M``!.;W0@82!V86QI9"!C;W)E(&9I;&4*``!5<V%G93H@8VAE8VMC;W)E(#QC
M;W)E9FEL92!N86UE/@H```!);G9A;&ED(&9O<FUA=#H@3F]T(&$@=F%L:60@
M8V]R92!F:6QE"@``26YV86QI9"!F;W)M870Z($YO="!A('9A;&ED(&-O<F4@
M9FEL90H`````````````$``!`"``$'P0``'((``0?```````````````````
M````````````````````````````````````````(``1(```````````(``/
M$"``#W@````````````````@`!$D`````"``#X@`````(``1*```````````
M`````"``$&`@`"D((``I#"``*1``````````````````````(``I&"``,X@`
M````(`![R``````@`!!D``````````$````2````)0```"\````"```#C```
M`&````.[97)R;F\```````````!`!0````$`````;'-E96L```````````!`
M"@````$`````;W!E;@````````````!`"@````$``````````````"``````
M``!`!`````$`````7VEO8@````````````!`!0````$`````97AI=```````
M``````!`"@````$`````9G)E90````````````!`"@````$`````;6%L;&]C
M``````````!`"@````$`````<W1R;&5N``````````!`"@````$`````9G!R
M:6YT9@````````!`"@````$`````<')I;G1F``````````!`"@````$`````
M<F5A9`````````````!`"@````$`````<&5R<F]R``````````!`"@````$`
M````<W1A=`````````````!`"@````$``````````````#@```````!`"@``
M``$`````7U]C<G0P=@````````!`!0````$``````````````$4```````!`
M!0````$`````7U]S=&%R=``@`!!H``(A"@``````````(``/>`````$?```"
M(``/?`````$?```"(``0:``````?```"(``0;`````$?```"(``0<``````?
M```"(``0=`````$?```"(``0I`````(?```"(``0J````!(?```"(``0K```
M`!,?```"(``0L`````$?```"(``0M`````$?```"(``0N````!$?```"(``0
MO`````,?```"(``0P`````H?```"(``0Q`````(?```"(``0R`````<?```"
M(``0S`````$?```"(``0T`````P?```"(``0U`````(?```"(``0V`````0?
M```"(``0W`````X?```"(``0X`````\?```"(``0Y`````$?```"(``0Z```
M``(?```"(``0[`````(?```"(``0\`````(?```"(``0]`````8?```"(``0
M^`````L?```"(``0_`````T?```"(``1``````D?```"(``1!`````(?```"
M(``1"`````(?```"(``1#`````4?```"(``1$`````(?```"(``1%````!`?
M```"(``1&`````$?```"(``1'`````@?```"+W5S<B]L:6(Z+VQI8CHO=7-R
M+VQP<"]X;$,O;&EB`````&QI8F,N80!S:'(N;P``'$!64$1C:&5C:V-O<F4O
M-3<V-6,S-#`S+S0S,```%E]S>7-T96U?8V]N9FEG=7)A=&EO;@``"U]?;6]D
D7VEN:70``!M?7VUA;&QO8U]U<V5R7V1E9FEN961?;F%M90``
`
end
SHAR_EOF
  $shar_touch -am 01030651103 'checkcore' &&
  chmod 0755 'checkcore' ||
  echo 'restore of checkcore failed'
  shar_count="`wc -c < 'checkcore'`"
  test 5436 -eq "$shar_count" ||
    echo "checkcore: original size 5436, current size $shar_count"
fi

rm -fr 01030651103
fi

# lets do some set up
#


libsdir='/tmp/'$$'corelibs'

trap "rm -fr ${libsdir} >/dev/null 2>&1; exit 1" 2

# look for checkcore
if ! [ -e ./checkcore ] ; then
	echo "[WARNING]: can not find checkcore" >&2
		exit 1;
fi

# look for checkcore
if ! [ -x ./checkcore ] ; then
	echo "Can not execute checkcore"
	exit 1;
fi

#make temp dir
if ! [ -e $libsdir ] ; then
	mkdir $libsdir
else
	echo $libsdir already in existance
	exit 1
fi

#look for corefile name
if ! [ -n $1 ]; then
	coreFile='core'
else
	coreFile=$1
fi


if [ -z "${EXECNAME}" ]; then
	echo "Determine executable ..."
	strings ${coreFile} | egrep "_=" 2>/dev/null | cut -d= -f2 | head -1 | read FULLNAME

	if [ -n "${FULLNAME}" ]; then
		export PATH="${FULLNAME%/*}:${PATH}"
		export EXECNAME="${FULLNAME##*/}"
		echo "Executable = ${FULLNAME}"
	fi
fi

echo "Using : $( which ${EXECNAME:-java} )"
echo "(if not correct, please change PATH variable)"

echo "Grabbing the active libs from core file"
for file in $(./checkcore $coreFile) $( which ${EXECNAME:-java} )
do
	echo $file
	if [ ! -e ${file} ]; then
		ofile="${file}"
		file="$( whence ${file} )"
		if [ -z "${file}" ]; then
			file="${ofile}"
		fi
	fi
	if [ -e $file ]; then
		cp $file $libsdir
	else
		echo "Can not find $file"
	fi
done

home=$PWD
cd $libsdir
tar cf - *  | compress > $home/core-libs.tar.Z
cd $home

rm -rf $libsdir
