#! /bin/bash
search1="<install_dir>"
search2="<version>"
search3="<compiler>"
srcdir=$1       # source directory
installdir=$2   # installation directory
version=$3      # CDF version
compiler=$4     # compiler
file=$5         # .pc file
which_compiler=`which $compiler`

echo "Modifying the pkgconfig file $srcdir/$file ..."

ext="${file##*.}"
file2=$file
if [[ "$ext" == "in" ]]; then
  file2="${file%.*}"
fi
cp $srcdir/$file $installdir/lib/pkgconfig
if `type ed > /dev/null 2>&1`
  then
    ed $installdir/lib/pkgconfig/$file << editend &> /dev/null
      1,\$s:$search1:$installdir:g
      1,\$s:$search2:$version:g
      1,\$s:$search3:$which_compiler:g
      w $installdir/lib/pkgconfig/$file.new
      q
editend
    mv $installdir/lib/pkgconfig/$file.new $installdir/lib/pkgconfig/$file2
    rm $installdir/lib/pkgconfig/$file
elif `type sed > /dev/null 2>&1`
  then
    sed -e "s,$search1,$installdir,g" \
	-e "s,$search2,$version,g" \
	-e "s,$search3,$which_compiler,g" \
	< $installdir/lib/pkgconfig/$file > $installdir/lib/pkgconfig/$file.new
    mv $installdir/lib/pkgconfig/$file.new $installdir/lib/pkgconfig/$file2
    rm $installdir/lib/pkgconfig/$file
else
  echo Not modified... as both "ed" and "sed" line editor not installed.
  echo Use any available text editor to manually modify "$search1" to 
  echo "$installdir" and "$search2" to "$version".
fi

