#!/bin/sh

#   Licensed to the Apache Software Foundation (ASF) under one
#   or more contributor license agreements.  See the NOTICE file
#   distributed with this work for additional information
#   regarding copyright ownership.  The ASF licenses this file
#   to you under the Apache License, Version 2.0 (the
#   "License"); you may not use this file except in compliance
#   with the License.  You may obtain a copy of the License at
# 
#       http://www.apache.org/licenses/LICENSE-2.0
# 
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

host="i686-pc-linux-gnu"
prefix="/usr"
exec_prefix="${prefix}"
INSTALLDIR=`eval echo "${exec_prefix}/lib/trafficserver/modules"`
INCLUDEDIR=`eval echo "${prefix}/include"`
CPPFLAGS="$CPPFLAGS -I$INCLUDEDIR"
CFLAGS="-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -march=i586 -pipe -Wall -Werror -feliminate-unused-debug-symbols -fno-strict-aliasing"
CXXFLAGS="-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wno-unused-result -Wno-sizeof-pointer-memaccess -Wno-unused-local-typedefs -march=i586 -pipe -Wall -Werror -feliminate-unused-debug-symbols -fno-strict-aliasing -Wno-invalid-offsetof"
BUILD=
DEBUGECHO=

if [ -z "$CC" ]; then
	CC="gcc"
fi
if [ -z "$CXX" ]; then
	CXX="g++"
fi

debug() {
	if [ -n "$DEBUGECHO" ]; then
		echo $*
	fi
}
bail() {
	echo $*
	exit 1
}

case $host in
*-*-darwin*)
        PICFLAGS="-fPIC"
        MODULELD="$CC -bundle -flat_namespace -undefined suppress"
        MODULELDXX="$CXX -bundle -flat_namespace -undefined suppress"
        MODULEEXT=bundle
        RLDFLAG="-Wl,--rpath="
        ;;
*-*-solaris*)
        PICFLAGS="-fpic"
        if test "x$CC" != "xgcc" ; then
                CFLAGS="$CFLAGS -mt"
        	PICFLAGS="-Kpic"
        fi
        MODULELD="$CC -G"
        MODULELDXX="$CXX -G"
        MODULEEXT=so
        RLDFLAG="-R"
        WHOLE_ARCHIVE="-Wl,-z -Wl,allextract"
        NOWHOLE_ARCHIVE="-Wl,-z -Wl,defaultextract"
        ;;
*-*-linux*)
        LDFLAGS="$LDFLAGS -Wl,-E"
        CPPFLAGS="$CPPFLAGS"
        PICFLAGS="-fpic"
        MODULELD="$CC -shared"
        MODULELDXX="$CXX -shared"
        MODULEEXT=so
        RLDFLAG="-Wl,--rpath="
        WHOLE_ARCHIVE="-Wl,--whole-archive"
        NOWHOLE_ARCHIVE="-Wl,--no-whole-archive"
        ;;
*-*-freebsd*)
        PICFLAGS="-fpic"
        MODULELD="$CC -shared"
        MODULELDXX="$CXX -shared"
        MODULEEXT=so
        RLDFLAG="-Wl,--rpath="
        ;;
*)
        PICFLAGS="-fpic"
        MODULELD="$CC -shared"
        MODULEEXT=so
        RLDFLAG="-Wl,--rpath="
        ;;
esac

LD=$MODULELD
for v in $* 
do
	case $v in 
	-v)
		shift
		DEBUGECHO=1
		;;
	-h)
		cat <<EOF
$0 : a tool to compile, link and install trafficserver plugins.

 compiling/linking:
	-c [ file1.c [ file2.c [ ... ] ] ]      ## compiles C files
	-C [ file1.cc [ file2.C [ ... ] ] ]     ## compiles C++ files
	-o modulename.so                        ## the name of the module
	-I include                              ## add -Iinclude to the compile
	-l lib                                  ## add -llib to the link

 installing:
	-o modulename.so                        ## the name of the module
	-i                                      ## install the object

EOF
		exit
		;;
	-I)
		CPPFLAGS="$CPPFLAGS -I$2"
		shift 2
		;;
	-l)
		LIBS="$LIBS -l$2"
		shift 2
		;;
	-c)
		shift
		while [ -r "$1" ]; do
			SRC=$1
			obj=`echo $SRC | sed -e 's/\.[a-z]*$/\.lo/g;'`
			echo "  compiling $SRC -> $obj"
			debug "$CC $CPPFLAGS $CFLAGS $PICFLAGS -c $SRC -o $obj"
			$CC $CPPFLAGS $CFLAGS $PICFLAGS -c $SRC -o $obj ||
				bail "Compile failed: $CC $CPPFLAGS $CFLAGS $PICFLAGS -c $SRC -o $obj"
			OBJS="$OBJS $obj"
			BUILD=1
			shift
		done
		;;
	-C)
		shift
		while [ -r "$1" ]; do
			SRC=$1
			obj=`echo $SRC | sed -e 's/\.[a-z]*$/\.lo/g;'`
			echo "  compiling $SRC -> $obj"
			debug "$CXX $CPPFLAGS $CXXFLAGS $PICFLAGS -c $SRC -o $obj"
			$CXX $CPPFLAGS $CXXFLAGS $PICFLAGS -c $SRC -o $obj ||
				bail "Compile failed: $CXX $CPPFLAGS $CXXFLAGS $PICFLAGS -c $SRC -o $obj"
			OBJS="$OBJS $obj"
			LD=$MODULELDXX
			BUILD=1
			shift
		done
		;;
	-o)
		OBJ=$2
		shift 2
		;;
	-i)
		INSTALL=1
		shift
		;;
	esac
done

if [ "$BUILD" = "1" -a -n "$OBJ" ]; then
	echo "  linking -> $OBJ"
	debug "$LD $LDFLAGS -o $OBJ $OBJS $LIBS"
	$LD $LDFLAGS -o $OBJ $OBJS $LIBS ||
		bail "Link failed: $LD $LDFLAGS -o $OBJ $OBJS $LIBS"
fi

if [ -n "$OBJ" ]; then
	if [ -r "$OBJ" ]; then
		if [ "$INSTALL" = "1" ]; then
			echo "  installing $OBJ -> $INSTALLDIR/$OBJ"
			cp $OBJ $INSTALLDIR/$OBJ
			chmod 0755 $INSTALLDIR/$OBJ
		fi
	else
		echo "No $OBJ"
	fi
fi
