#! /bin/bash
###########################################################################
#   Copyright (C) 2009 by Hessel Hoogendorp                               #
#   bugs.ccc@gmail.com                                                    #
#                                                                         #
#   This program is free software; you can redistribute it and/or modify  #
#   it under the terms of the GNU General Public License as published by  #
#   the Free Software Foundation; either version 2 of the License, or     #
#   (at your option) any later version.                                   #
#                                                                         #
#   This program is distributed in the hope that it will be useful,       #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#   GNU General Public License for more details.                          #
#                                                                         #
#   You should have received a copy of the GNU General Public License     #
#   along with this program; if not, write to the                         #
#   Free Software Foundation, Inc.,                                       #
#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
###########################################################################

# -----------------------------------------------------------------------------
# This script wraps ar.
#
# First, it makes the original call to ar, and, if that succeeds, then calls
# the call information extractor with the same parameters.
# -----------------------------------------------------------------------------


# -----------------------------------------------------------------------------
# Call the original ar archiver.
# -----------------------------------------------------------------------------

# Find the installation path of ar.
AR=$(whereis ar)
AR=${AR#*: }         # Strip off 'ar: '.
AR=${AR%% *}         # Strip off any secondary paths.

# Test whether we found an existing, executable program.
if [ ! -x $AR ]; then
	echo "ERROR: The ar wrapper script could not find a usable archiver."
	exit 1
fi

# Supply the archiver with all arguments passed to this script.
CMD="$AR $@"
$CMD

# Check whether the call to ar was successful. If not, return with the
# archiver's exit code.
exitCode=$?
if [ $exitCode -ne 0 ]; then
	exit $exitCode
fi


# Shift off the mode of operation.
shift

# Compose the name of the cci-archive from the name of the archive. Then shift
# off the argument holding the name of the archive.
CCI_ARCHIVE="$1.ccia"
shift

# If there are no more arguments, there is nothing left to do.
if [ $# -eq 0 ]; then
	exit 0
fi

CCI_OBJECTS=
while [ $# -gt 0 ]; do

	# It might be that this is actually a link to an object file, not the object file
	# itself. Therefore, resolve the file to its canonical form (the actual file).
	OBJ=$(readlink -m $1)

	# Test whether the .i.cci or the .ii.cci object file corresponding to this
	# object file exists.
	CCI_OBJECT_C=${OBJ%.o}.i.cci
	CCI_OBJECT_CPP=${OBJ%.o}.ii.cci
	shift

	CCI_OBJECT=
	if [ -e $CCI_OBJECT_C ]; then
		CCI_OBJECT=$CCI_OBJECT_C
	elif [ -e $CCI_OBJECT_CPP ]; then
		CCI_OBJECT=$CCI_OBJECT_CPP
	else
		continue
	fi

	# Add the existing cci-object file to the string of archive members.
	CCI_OBJECTS="$CCI_OBJECTS $CCI_OBJECT"
done

# Archive the members.
$AR cru $CCI_ARCHIVE $CCI_OBJECTS

# The archiver returned success, so return success.
exit 0

