#!/bin/bash
# --- BEGIN COPYRIGHT BLOCK ---
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation;
# version 2.1 of the License.
# 
# This library 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
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA  02110-1301  USA 
# 
# Copyright (C) 2009 Red Hat, Inc.
# All rights reserved.
# --- END COPYRIGHT BLOCK ---

###############################################################################
##  (1)  Check command line arguments to see how many were passed in.        ##
###############################################################################

if [ $# -eq 5 ]
then
	NUXWDOGCLIENT_BUILD_PREFIX=$1
	VERSION=$2
	RELEASE=$3
	ARCHITECTURE=$4
	NUXWDOGCLIENT_STAGING_PATH=$5
else
	echo
	echo "Usage:  $0 NUXWDOGCLIENT_build_prefix version release architecture"
	echo "        NUXWDOGCLIENT_staging_path"
	echo

	exit 255
fi


###############################################################################
##  (2)  Specify variables used by this script.                              ##
###############################################################################

# specify generic helper functions
usage() {
	if [ $# -gt 0 ] ; then
		echo
		echo "$1"
	fi
	echo
	echo "Usage:  $0 NUXWDOGCLIENT_build_prefix version release architecture"
	echo "        NUXWDOGCLIENT_staging_path"
	echo
	echo "        where architecture MUST be 'intel',"
	echo "                                   'sparc', or"
	echo "                                   'sparcv9'."
	echo
	echo "        NOTE:  For 'intel' architectures, only the 'i386' and"
	echo "               the 'x86_64' architectures are currently supported."
	echo
}

# specify generic helper variables
if  [ ${ARCHITECTURE} = "intel" ] ; then
	# Since "rpmbuild" fails to process "%ifarch" macros inside the
	# "%install" section of a spec file, the actual hardware
	# architecture will be determined at this point in time.
	ARCHITECTURE=`uname -i`
    DLL_SUFFIX="so"
	if   [ ${ARCHITECTURE} = "i386" ] ; then
		LIB_DIR="lib"
                BIN_DIR="bin"
	elif [ ${ARCHITECTURE} = "x86_64" ] ; then
		LIB_DIR="lib64"
                BIN_DIR="bin"
	else
		usage "ERROR:  Unsupported intel architecture '${ARCHITECTURE}'!"
		exit 255
	fi
elif [ ${ARCHITECTURE} = "sparc" ] ; then
	# Note that "pkgbuild" successfully processes "%ifarch" macros
	# inside the "%install" section of a spec file.
	LIB_DIR="lib"
        BIN_DIR="bin"
    DLL_SUFFIX="so"
elif [ ${ARCHITECTURE} = "sparcv9" ] ; then
	# Note that "pkgbuild" successfully processes "%ifarch" macros
	# inside the "%install" section of a spec file.
	LIB_DIR="lib/sparcv9"
        BIN_DIR="bin"
    DLL_SUFFIX="so"
else
	usage "ERROR:  Unsupported architecture '${ARCHITECTURE}'!"
	exit 255
fi

# break the VERSION number into its various components
MAJOR_VERSION=`echo ${VERSION} | awk -F. '{ print $1 }'`
MINOR_VERSION=`echo ${VERSION} | awk -F. '{ print $2 }'`
PATCH_VERSION=`echo ${VERSION} | awk -F. '{ print $3 }'`

PRODUCT_VERSION=${MAJOR_VERSION}.${MINOR_VERSION}


# comply with standard FHS 2.3 binary locations (executables)
NUXWDOGCLIENT_BIN_DIR=${NUXWDOGCLIENT_BUILD_PREFIX}/usr/${BIN_DIR}

# comply with standard FHS 2.3 library locations
NUXWDOGCLIENT_LIB_DIR=${NUXWDOGCLIENT_BUILD_PREFIX}/usr/${LIB_DIR}
NUXWDOGCLIENT_LOCAL_DIR=${NUXWDOGCLIENT_BUILD_PREFIX}/usr

# comply with standard JPackage 1.6.0 jar locations
NUXWDOGCLIENT_JAR_DIR=${NUXWDOGCLIENT_BUILD_PREFIX}/usr/lib/java

# comply with standard FHS 2.3 binary locations (wrappers)

# comply with standard FHS 2.3 shared data locations (templates)

# comply with standard FHS 2.3 start/stop script locations

# comply with standard FHS 2.3 configuration file locations

# comply with standard FHS 2.3 documentation locations
NUXWDOGCLIENT_DOCUMENTATION=${NUXWDOGCLIENT_BUILD_PREFIX}/usr/share/doc/nuxwdog-${VERSION}

# comply with standard FHS 2.3 log file locations

# comply with default FHS 2.3 instance locations

#include files for devel package
NUXWDOGCLIENT_INCLUDE_DIR=${NUXWDOGCLIENT_BUILD_PREFIX}/usr/include/nuxwdog

###############################################################################
##  (3)  Create the appropriate subdirectories.                              ##
###############################################################################

##
## System:
##

mkdir -p ${NUXWDOGCLIENT_DOCUMENTATION}
mkdir -p ${NUXWDOGCLIENT_LIB_DIR}
mkdir -p ${NUXWDOGCLIENT_BIN_DIR}
mkdir -p ${NUXWDOGCLIENT_JAR_DIR}
mkdir -p ${NUXWDOGCLIENT_INCLUDE_DIR}
mkdir -p ${NUXWDOGCLIENT_LOCAL_DIR}/share/man/man3/
mkdir -p ${NUXWDOGCLIENT_LIB_DIR}/perl5

##
## Product
##


##
## Subsystem
##


##
## Initial Instance
##


###############################################################################
##  (4)  Unpack the package contents to the appropriate subdirectories.      ##
###############################################################################

##
## Executables
##
cp -p  ${NUXWDOGCLIENT_STAGING_PATH}/bin/nuxwdog ${NUXWDOGCLIENT_BIN_DIR}

##
## Libraries
##

cp -p ${NUXWDOGCLIENT_STAGING_PATH}/${LIB_DIR}/libnuxwdog.${DLL_SUFFIX}  ${NUXWDOGCLIENT_LIB_DIR}
cp -rp ${NUXWDOGCLIENT_STAGING_PATH}/local/${LIB_DIR}/perl5/site_perl/* ${NUXWDOGCLIENT_LIB_DIR}/perl5
 

##
## Jars
##

cp -p ${NUXWDOGCLIENT_STAGING_PATH}/jars/nuxwdog.jar ${NUXWDOGCLIENT_JAR_DIR}


##
## Wrappers
##


##
## Shared Data
##

cp -rp ${NUXWDOGCLIENT_STAGING_PATH}/doc/LICENSE         ${NUXWDOGCLIENT_DOCUMENTATION}
cp -p ${NUXWDOGCLIENT_STAGING_PATH}/local/share/man/man3/* ${NUXWDOGCLIENT_LOCAL_DIR}/share/man/man3
 
## 
## header files
## 
cp -rp  ${NUXWDOGCLIENT_STAGING_PATH}/include/nuxwdog-1.0/nuxwdog/*.h    ${NUXWDOGCLIENT_INCLUDE_DIR}

###############################################################################
##  (5)  Unpack the package contents to the initial instance directories.    ##
###############################################################################

##
## Start/Stop Script
##


##
## Configuration
##


##
## Logs
##


##
## Default Instance
##


###############################################################################
##  (6)  Rename the extracted contents following appropriate naming rules.   ##
###############################################################################

# comply with standard Linux/UNIX shared library naming conventions
cd ${NUXWDOGCLIENT_LIB_DIR} ; mv libnuxwdog.${DLL_SUFFIX} libnuxwdog.${DLL_SUFFIX}.${VERSION}

# comply with standard JPackage 1.6.0 jar naming conventions
cd ${NUXWDOGCLIENT_JAR_DIR} ; mv nuxwdog.jar nuxwdog-${VERSION}.jar

# strip symbolic information from libraries
#cd ${NUXWDOGCLIENT_LIB_DIR} ; strip libnuxwdog.${DLL_SUFFIX}.${VERSION}


###############################################################################
##  (7)  Create a command wrapper for each specified command.                ##
###############################################################################



###############################################################################
##  (8)  Create useful symbolic links as appropriate.                        ##
###############################################################################

# create shared library sans version "linker-name" to protect this namespace
cd ${NUXWDOGCLIENT_LIB_DIR}                                                ;
ln -s libnuxwdog.${DLL_SUFFIX}.${VERSION} libnuxwdog.${DLL_SUFFIX}.${PRODUCT_VERSION}       ;
ln -s libnuxwdog.${DLL_SUFFIX}.${PRODUCT_VERSION} libnuxwdog.${DLL_SUFFIX}.${MAJOR_VERSION} ;
ln -s libnuxwdog.${DLL_SUFFIX}.${MAJOR_VERSION} libnuxwdog.${DLL_SUFFIX}

# create jar sans version to be used by classpath
cd ${NUXWDOGCLIENT_JAR_DIR} ; ln -s nuxwdog-${VERSION}.jar nuxwdog.jar

# create assorted symbolic links to various file dependencies (Tomcat)


###############################################################################
##  (9)  Successfully exit from this setup script.                           ##
###############################################################################

exit 0

