#! /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 is a simple wrapper script that helps you call the C/C++ call graph
# Constructor.
# Just pass arguments to this script as you would to ccc, only exclude the
# input files: This script will add the input files.
# -----------------------------------------------------------------------------


# By default, the current directory is the only input directory.
CALLINFO_DIRS=$(pwd)

# By default, ccc is executed
EXECUTE=1


# 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


# Process command line arguments.
while [ $# -gt 0 ]
do

	# Check what parameter has been passed to ccc-run, if any.
	case $1 in
	-l)
		# List the targets in the current directory and its subdirectories and exit.
		find . -type f -regex './.*\.ccia'
		exit 0
		;;
	-h|--help)
		# Print usage information and exit.
		echo "Usage: ccc-run [OPTION1] TARGET [OPTION2]..."
		echo "Wrap ccc to automatically provide it with input files required to"
		echo "construct a call graph for TARGET."
		echo ""
		echo "OPTION1 is an option for ccc-run, OPTION2 are options passed on to ccc."
		echo "Try ccc --help for a list of valid values for OPTION2. A valid value for"
		echo "OPTION1 is one of the following:"
		echo "  -l        : List the files that can be used as the TARGET." 
		echo "  -h, --help: Print this help message and exit."
		echo ""
		exit 0
		;;
	-*)
		echo "Unknown option '$1'. Did you specify the target file before any options to ccc?"
		exit 1
		;;
	*)
		# A target file was specified. Store its name and process it.
		TARGET=$1
		# Shift off the argument we just processed.
		shift

		# Make sure the target file exists.
		if [ ! -e $TARGET ]; then
			echo "The target file '$TARGET' does not exist."
			return 1
		fi

		# Randomly generate a name for the temporary directory.
		TMPDIR="ccc-$RANDOM.$RANDOM.$RANDOM-ccc"

		# Make sure the temporary directory exists.
		if [ ! -d $TMPDIR ]; then
			mkdir $TMPDIR
		fi

		# Copy the target archive into the temporary directory.
		cp $TARGET "$TMPDIR/."

		# Change into the temporary directory and extract all the files from the
		# target archive.
		cd $TMPDIR
		$AR x $(basename $TARGET)

		# Change back to the parent directory.
		cd ..

		# Call the constructor with the supplied arguments and the extracted .cci files.
		ccc "$@" $(find . -type f -regex "./$TMPDIR/.*\.cci")

		# Remove the temporary directory and everything in it.
		if [ -d $TMPDIR ]; then
			rm -r $TMPDIR
		fi

		exit 0
		;;
	esac
done

