#!/bin/bash
#
# This script cleans the "rfs" directory, then copies makepp built programs and
# template files to the "rfs" directory in the format needed to add to a pristine
# OS install.  The format of the command is:
#
#	populate_smc_mpp {build-type}
#
# If build-type is left blank, it defaults to "ZoomRelease"; otherwise, it is the
# type of build to pull from the workspaces.  For example, to pull the Debug files
# for the target:
#
#	populate_smc_mpp ZoomDebug
#
# After populating the rfs directory with populate_smc_mpp, the user should invoke
# "maketar" to build an installable compressed tar ball.
#
BUILD="$1"

if [ -z "$BUILD" ]
then
	BUILD=ZoomRelease
fi

XETC=`readlink -m ./rfs/etc`
XHOME=`readlink -m ./rfs/home`
XUSR=`readlink -m ./rfs/usr`
XVAR=`readlink -m ./rfs/var`
SMCHOME=`readlink -m ./rfs/home/smc`
SMC0HOME=`readlink -m ./rfs/home/smc0`
WKSP=`readlink -m ../mpp2/common`
START=`pwd`
TEMPFILE=/tmp/temp`date +'%Y%m%d%H%M%S%N'`

echo $SMC0HOME
echo $SMCHOME

# clean the rfs directory and re-create
sudo rm -rf rfs
mkdir rfs

# use cpio to copy the content of the template directory (less the .svn and
# .makepp directories and vi backup files) to rfs
cd template
find * -print | egrep -v '/[.]svn|/[.]makepp|~$' | cpio -pv ../rfs

# create an archive of the CORBA libraries and extract under rfs
NEEDCORBA=0
#if [ "${BUILD:0:2}" = "zz" ]
#then
  echo "Including CORBA"
  NEEDCORBA=1
  if [ "${BUILD:0:2}" = "zz" ]
  then
    CORBADIR=`readlink -f $START/../../omniORB/zzZoom6.0.0`
  else
    CORBADIR=`readlink -f $START/../../omniORB/Zoom`
  fi
  cd "$CORBADIR"
  find * -print | egrep -v '/[.]svn|/[.]makepp' > "$TEMPFILE"
  tar -cvjf $START/omni.tar.bz2 -T "$TEMPFILE" --no-recursion
  rm "$TEMPFILE"
  mkdir -p $START/rfs/usr/local
  cd $START/rfs/usr/local
  tar xvjpf $START/omni.tar.bz2
#else
#  echo "Not including CORBA"
#fi
cd $START

# determine which files (shared objects and executables) should be copied from the
# workspace build directories
candidate_files=`find $WKSP -type f -print | egrep "/$BUILD/" | egrep -v "/$BUILD/"'.+/'`
built_libs=`file $candidate_files | grep 'shared object' | sed 's/[:].*//g'`
built_execs=`file $candidate_files | grep 'executable' | sed 's/[:].*//g' | egrep -v EncryptedStartup`
built_execs0=`file $candidate_files | grep 'executable' | sed 's/[:].*//g' | egrep EncryptedStartup`

# indicating which files are being pulled
echo "LIBS: $built_libs"
echo "EXECS: $built_execs $built_execs0"

# copying the files to lib and bin subdirectories of smc home
sudo cp $built_libs $SMCHOME/lib
sudo cp $built_execs $SMCHOME/bin

# copying EncryptedStartup smc0
if [ -n "$build_execs0" ]
then
  sudo cp $built_execs0 $SMC0HOME/bin
fi

# make files and directories 700 under /home/smc
sudo find ./rfs/home/smc -printf '"%p"\n' | sudo xargs chmod g-rwx,o-rwx

# make files and directories 700 under /home/smc0
sudo find ./rfs/home/smc0 -printf '"%p"\n' | sudo xargs chmod g-rwx,o-rwx

# in general, everything under rfs will be owned by root/root
sudo find ./rfs -printf '"%p"\n' | sudo xargs chown root
sudo find ./rfs -printf '"%p"\n' | sudo xargs chgrp root

# items under SMC home will be owned by smc/smc and bin directory
# files are executable (+x)
sudo find $SMCHOME/bin -type f -printf '"%p"\n' | sudo xargs chmod u+x
sudo find $SMCHOME -printf '"%p"\n' | sudo xargs chown smc:smc

# items under SMC0 home will be owned by smc0/smc0 and bin directory
# files are executable (+x)
sudo find $SMC0HOME/bin -type f -printf '"%p"\n' | sudo xargs chmod u+x
sudo find $SMC0HOME -printf '"%p"\n' | sudo xargs chown smc0:smc0

# the sudoers file must be rr-
sudo chmod 440 ./rfs/etc/sudoers

# configure the home directory for use with ssh
if sudo test ! -d ./rfs/home/smc/.ssh
then
  sudo mkdir ./rfs/home/smc/.ssh
  sudo chown smc:smc ./rfs/home/smc/.ssh
fi
sudo chmod 700 ./rfs/home/smc
sudo chmod 700 ./rfs/home/smc/.ssh
