#!/bin/sh
#
# Copyright (C) 2012-2014 Embecosm Limited
#
# Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
#
# This file 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., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
# As a special exception to the GNU General Public License, if you
# distribute this file as part of a program that contains a
# configuration script generated by Autoconf, you may include it under
# the same distribution terms that you use for the rest of that program.

# A wrapper for the GCC symlink-tree script, to build a symlink tree of all
# the component directories.

# Usage:

#   symlink-all.sh <basedir> <logfile> <ignore> <unisrc> <component1> \
#                  <component2> ...

# The <unisrc> directory is assumed to exist. symlink-tree is run in that
# directory for each top level directory in each <component> directory,
# excluding any top level files/directories given by the <ignore> argument.

# For some components (binutils, gdb) we add some specific known directories
# to ignore. This is a consequence of their shared upstream repository.

# We assume the component directories are direct subdirs of the base
# directory. We therefore also assume that the symlink-tree script can be
# found in ${basedir}/gcc


################################################################################
#                                                                              #
#			       Shell functions                                 #
#                                                                              #
################################################################################

# Function to check for relative directory and makes it absolute

# @param[in] $1  The directory to make absolute if necessary.
absdir() {
    case ${1} in

	/*)
	    echo "${1}"
	    ;;

	*)
	    echo "${PWD}/${1}"
	    ;;
    esac
}


# Convenience function to copy a message to the log and terminal

# @param[in] $1  The message to log
logterm () {
    echo $1 | tee -a ${logfile}
}


# Convenience function to copy a message to the log only

# @param[in] $1  The message to log
logonly () {
    echo $1 >> ${logfile}
}


# Set the base directory and logfile
basedir="$1"
shift
logfile="$1"
shift
always_ignore="$1"
shift

# Change to the unisrc directory, which can be relative to the base directory.
unisrc_dir=$1
shift
if ! cd ${basedir}
then
    logterm "ERROR: sylink-all: Could not change to base dir ${basedir}"
    exit 1
fi

if ! cd ${unisrc_dir}
then
    logterm "ERROR: symlink-all: Could not change to unified dir ${unisrc_dir}"
    exit 1
fi

# Symlink each tree
for component in $*
do
    logterm "Adding component ${component} to unified source"

    # Special case of some directories to ignore for some components
    case $component in
	binutils)
	    ignore="${always_ignore} gdb sim"
	    ;;

	gdb)
	    ignore="${always_ignore} binutils ld opcodes bfd"
	    ;;

	gcc)
	    ignore="${always_ignore}"
	    ;;

	cgen)
	    ignore="${always_ignore} etc config"
	    ;;

	*)
	    ignore="${always_ignore} sim"
	    ;;
    esac

    if ! ${basedir}/gcc/symlink-tree "${basedir}/${component}" "${ignore}" \
	     >> ${logfile} 2>&1
    then
	exit 1
    fi
done

exit 0
