#!/bin/sh

#set -x

#------------------------------------------------------
#
#  Syntax :
#
#  cmt_make_shlib_common.sh extract-arg package extra-args
#
#   with :
#
#      extract-arg  : either "extract" or "no_extract"
#      package      : the current package name
#      extra-args   : extra link options
#
#------------------------------------------------------


extract=$1
shift

tag=$1
shift

lib=$1
shift

extra=$*

here=`pwd`

ld=`${CMTROOT}/mgr/cmt show macro_value shlibbuilder -tag=${tag}`
ldflags=`${CMTROOT}/mgr/cmt show macro_value shlibflags -tag=${tag}`
suffix=`${CMTROOT}/mgr/cmt show macro_value shlibsuffix -tag=${tag}`

if test "${ld}" = "" ; then
  exit 1
fi

/bin/rm -f lib${lib}.${suffix}

result=0

temp_shlib=${lib}temp_shlib

if test ${extract} = "extract"; then
  trap 'cd ${here}; /bin/rm -rf ${temp_shlib}; exit ${result}' 0 1 2 15

  mkdir -p ${temp_shlib}
  /bin/rm -f ${temp_shlib}/*

  (cd ${temp_shlib}; ar x ../lib${lib}.a)

  eval ${ld} ${ldflags} -o lib${lib}.${suffix} `ls -1 ${temp_shlib} | sed -e 's#^'${lib}'temp_shlib/##'` ${extra}
  result=$?

  /bin/rm -rf ${temp_shlib}
else
  eval ${ld} ${ldflags} -all lib${lib}.a -o lib${lib}.${suffix} ${extra} -rpath `pwd`
  result=$?
fi




