#!/bin/bash
##############################################################################
#
# Teradici TERA12xx PCoIP Host Linux driver
#
# Copyright(c) 2010 Teradici Corporation
# This program is free software; you can redistribute it and/or modify it
# under the terms and conditions of the GNU General Public License,
# version 2, as published by the Free Software Foundation.
#
##############################################################################
#
# Name: createRPM 
#
# Description:
#   This script creates the source and binary RPM files. It copies the
#   required files into the RPM build directories and then executes the
#   rpmbuild command which creates the source and binary rpm files. It then
#   copies the source and binary RPM files to the obj directory.
#
# Notes:
#   1) This script must be invoked from the directory it resides in.
#   2) If fakeroot is not installed, this script must be run as root.
#      Building RPMs as root is a bad idea. If installed, this script
#      uses the utility fakeroot - which exists for the sole purpose 
#      of permitting package building where files require settings 
#      only attainable by root.  For more info on fakeroot 
#      see  man 5 fakeroot.
#   3) If the parameter -notopo is used with this script, it will include
#      the pcoip_agent version which does not push display topology in 
#      the RPM.
# 

ROOT_CMD="fakeroot"
$ROOT_CMD -v &>/dev/null
  if [ "$?" -eq 0 ] ; then
    echo "using `which fakeroot` for RPM creation."
  else
    if [ `whoami` == "root" ] ; then
      ROOT_CMD=""
    else
      echo "For RPM creation, either run this script as root, or install the fakeroot package."
      exit 1
    fi
  fi

# we select a temporary directory for building the RPMs. 
# the temp directory must be cleaned up after the RPM(s) are built.
TMP_DIR=`mktemp -d -p ~/`
# creating rpmbuild structure under $TMP_DIR
echo " Creating required RPM build directory structure under $TMP_DIR"
for d in {BUILD,RPMS,SOURCES,SPECS,SRPMS}; 
do 
  mkdir $TMP_DIR/$d
  if [ $? != "0" ]; then 
     echo " Failed to create required RPM build directory structure "
     exit 1
  fi 
done

# now backup the users ~/.rpmmacros if it exists and create a new one 
if [ -e ~/.rpmmacros ]; then
  mv ~/.rpmmacros ~/.rpmmacros-backup
fi

# The CentOS 5.10 buildAgent was getting confused about where its
# home directory was. This approach removes $HOME from .rpmmacros
echo "%_topdir $TMP_DIR" > ~/.rpmmacros

RPM_BUILD_DIR="$TMP_DIR"
echo "RPM build directory = $RPM_BUILD_DIR"

# Update the udev rules if necessary. Newer versions of udev have replaced
# the SYSFS directive with the ATTR directive. This change is required on
# RHEL 7, which uses udev version 208. The change is not required on version
# 147, which is installed on CentOS 6.5. The commands below set the default
# version of udev equal to 147. If the udevadm tool is installed the version
# of udev is read. If the version is greater than 147 the rules file is
# updated and SYSFS is replaced with ATTR.
udev_version=147
udevadm -V &>/dev/null
if [ $? -eq 0 ] ; then
  udev_version=$(udevadm -V)
  echo "udev version = $udev_version"
else
  echo "unable to query udev version, setting equal to $udev_version"
fi
if [ $udev_version -gt 147 ] ; then
  sed s/SYSFS/ATTR/g 10-pcoip_host_udev.rules.template >10-pcoip_host_udev.rules
else
  cp 10-pcoip_host_udev.rules.template 10-pcoip_host_udev.rules
fi

# clean the src directory
cd ../src
make clean
cd ../scripts

# clean the obj directory
rm -rf ../obj
mkdir ../obj

pushd ../../../user/linux/pcoip_host_software_gui
# Set HOST_SW_VERSION_STRING and PUSH_TOPO
source ./set_version
popd

# Allow PUSH_TOPO to be overridden 
  if [ "$1" == "-notopo" ] ; then
    echo "============================================================"
    echo " -notopo argument given. Using agent without topology push."
    echo "============================================================"
    export PUSH_TOPO=0
  fi

echo "create tarball of source files and Makefile and put it in the SOURCES directory"
# no need for the source to be owned by root or group'ed to root - owned by user now.
install -d pcoip_host-$RELEASE_VERSION
install -m 664 ../src/* pcoip_host-$RELEASE_VERSION
install -m 664 ../../../common/linux/inc/* pcoip_host-$RELEASE_VERSION
tar -cjf $RPM_BUILD_DIR/SOURCES/pcoip_host-$RELEASE_VERSION.tar.bz2 pcoip_host-$RELEASE_VERSION
rm -rf pcoip_host-$RELEASE_VERSION

# Put the version number into the pcoip_hostcheck script
sed s/SEDHCAVERSIONREPLACE/$RELEASE_VERSION/g pcoip_hostcheck.template >pcoip_hostcheck

# Put the version number into the pcoip_host.spec
sed s/SED_TERA_VERSION_REPLACE/$RELEASE_VERSION/g pcoip_host.spec >pcoip_host.1.spec
sed s/SED_TERA_RELEASE_REPLACE/$BUILD_VERSION/g pcoip_host.1.spec >pcoip_host.2.spec

echo "Copy required scripts/rules to the SOURCES directory"
# user and group ownership should be set in the SPEC file for these
install -m 775 pcoip_host $RPM_BUILD_DIR/SOURCES
install -m 664 10-pcoip_host_udev.rules $RPM_BUILD_DIR/SOURCES
install -m 775 pcoip_hostcheck $RPM_BUILD_DIR/SOURCES
install -m 775 pcoip_lockscreen $RPM_BUILD_DIR/SOURCES
install -m 775 pcoip_xauthority $RPM_BUILD_DIR/SOURCES
install -m 775 pcoip_starthostsw $RPM_BUILD_DIR/SOURCES

echo "Select the agent executable and copy to the SOURCES directory"
export AGENT=../../../user/linux/pcoip_host_software_gui/bin/Release/pcoip_agent
export AGENT_X=../../../user/linux/pcoip_host_software_gui/bin/Release/pcoip_agent_xrr
export AGENT_NX=../../../user/linux/pcoip_host_software_gui/bin/Release/pcoip_agent_no_xrr
if [ -e $AGENT_X ] ; then
    if [ -e $AGENT_NX ] ; then
        # Both executables are present, pick with PUSH_TOPO
        if [ $PUSH_TOPO -eq 1 ] ; then
            echo "Using pcoip_agent with libxrandr api"
            cp $AGENT_X $AGENT
        else
            echo "Using pcoip_agent without libxrandr api"
            cp $AGENT_NX $AGENT
        fi 
    else
        # Only pcoip_agent_xrr is present
        cp $AGENT_X $AGENT
    fi
else
    # Only pcoip_agent_noxrr is present
    cp $AGENT_NX $AGENT
fi
        
install -m 755 ../../../user/linux/pcoip_host_software_gui/bin/Release/pcoip_agent $RPM_BUILD_DIR/SOURCES

echo "Copy the host SW daemon executable to the SOURCES directory"
# Note: the script assumes the executable is in the daemon directory
install -m 755 ../../../user/linux/daemon/pcoiphostswd $RPM_BUILD_DIR/SOURCES

echo "Copy the spec file to the SPECS directory"
install -m 664 pcoip_host.2.spec $RPM_BUILD_DIR/SPECS

# Now that all the binaries and source are set - build the RPMS
echo "Creating the binary and source RPM files"
pushd .

cd $RPM_BUILD_DIR/SPECS

$ROOT_CMD rpmbuild -ba pcoip_host.2.spec
RPMBUILDRES="$?"

popd

echo "Move the binary RPM file to the obj directory"
mv $RPM_BUILD_DIR/RPMS/x86_64/pcoip_host-$RELEASE_VERSION-$BUILD_VERSION*x86_64.rpm \
   ../obj/pcoip_host-$HOST_SW_VERSION_STRING.x86_64.rpm

# Cleanup TMP build directory - unless env says not to 
if [ x"$DONT_CLEANUP_RPM_TMP" == x ]; then
  # if we had a clean build of the RPM - then nuke the temp directory 
  if [ "$RPMBUILDRES" == "0" ]; then
    rm $TMP_DIR -rf
  fi
fi

# now restore rpmmacros backup if it exists 
if [ -e ~/.rpmmacros-backup ]; then
  mv ~/.rpmmacros-backup ~/.rpmmacros 
fi

