#! /bin/sh
#
# $Id$
#
# Copyright 1989-2016 MINES ParisTech
#
# This file is part of PIPS.
#
# PIPS 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 3 of the License, or
# any later version.
#
# PIPS 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 PIPS.  If not, see <http://www.gnu.org/licenses/>.
#


USAGE="Usage: $0 [-t|b]\n
 orders libraries used by Pips. Should be called after analyze_libraries.\n
 -t top-down (default)\n
 -b bottom-up order"

tmpdir=${PIPS_TEMPDIR:-/tmp}

DEFAULT=TRUE

while getopts tb opt
do
	case $opt in
	t)	DEFAULT=TRUE;;
	b)	DEFAULT="";;
	*)	echo $USAGE >&2
		exit 2;;
	esac
done

if [ -n "$DEFAULT" ]
then
awk '$2 != $3 { print $2 , $3 }' $tmpdir/libs.j | sort -u | tee $tmpdir/libs.s | tsort > $tmpdir/libs.o 2> $tmpdir/libs.c
else
awk '$2 != $3 { print $3 , $2 }' $tmpdir/libs.j | sort -u | tee $tmpdir/libs.s | tsort > $tmpdir/libs.b 2> $tmpdir/libs.c
fi