#!/bin/ksh -p
#@(#) $Revision: 1.21 $
###################################
# $Header: create_fileset,v 1.21 93/12/10 12:02:20 jennie Exp $
#
# Program to build filesets to be delivered.
#
# Input: Any number of records of following format ended by EOF on stdin
# GSIDBFORMAT defaults to 0.  It is set based on $BRANCH.
# Pre-10.0 format when GSIDBFORMAT = 0 (default):
# <full-path-target> <fileset> <filetype> <status> <machine> \
# <mode> <owner> <group> <link_source> <relative-path-source> <partner>
# Output: Target files copied under <outdir>/<fileset> with correct m,o,g
#
# 10.0 format when GSIDBFORMAT = 1:
# <full-path-target> <product> <fileset> <filetype> <status> <machine> \
# <mode> <owner> <group> <link_source> <relative-path-source> <partner>
# Output: Target files copied under <outdir>/<product>/<fileset> with 
#         correct m,o,g
#
# 10.0 tarball format when GSIDBFORMAT = 2:
# <full-path-target> <filetype> <status> <machine> \
# <mode> <owner> <group> <link_source> <relative-path-source> <partner>
# Output: Target files copied under <outdir> with correct m,o,g
#
# Behavior: Output directory and source files are relative to <srcdir>
#	given on command line (current dir).  Error messages and an
#       error return code are given if source file is not found or readable.
#       Processing will continue on errors.  Existing files will not
#	be overwritten.  Directories are created as needed.  All input records
#	should represent delivered files (i.e., no directories, no run-time
#	created files, etc.).
#
# Note: chmog not used because it returns 0 on error :-(
#
#   
#
ME=`basename $0`
USAGE="${ME} [-s <dir>] [-i <dir>] [-o <dir>] [-u <file>]
 
				(Use full path for directory names)
-s <dir>   Source directory (default = current directory)
-i <dir>   Make Ident directory (default = <src-dir>/S800)
-o <dir>   Output directory (default = <src-dir>/../FILESETS)
-u <file>  File containing additional unifdef arguments
"

umask 022

SDIR=`pwd`
UNDEFLIST=/dev/null

if [[ -z "$BLDBIN" ]]
then
  echo "${ME}: Using $(whence unifdef)"
  UNIFDEF=unifdef
else
  echo "${ME}: Using ${BLDBIN}/unifdef"
  UNIFDEF=${BLDBIN}/unifdef
fi


if [ $? -ne 0 ]
then
  echo "$USAGE" 1>&2
  exit 61
fi

for i in $*
do
    case $i in
      -s) SDIR="$2"
          shift 2
          ;;
      -i) IDIR="$2"
          shift 2
          ;;
      -o) ODIR="$2"
          shift 2
          ;;
      -u) UNDEFLIST="$2"
          shift 2
          ;;
    esac
done
IDIR=${IDIR:-${SDIR}/S800}
ODIR=${ODIR:-${SDIR}/../FILESETS}

if [ ! -d $ODIR ]
then
  mkdir -p $ODIR
  if [ $? -ne 0 ]
  then
    echo "Can't create output directory $ODIR" 1>&2
    exit 62
  fi
fi

CURRDIR=`pwd`
cd $IDIR
if [ $? -ne 0 ]
then
  echo "Can't cd to objects directory $IDIR" 1>&2
  exit 70
fi
DEFS=`make -f makefile Prod_ident`
DEFS="$DEFS $(cat $UNDEFLIST)"   ## do kernel defines plus standard undefines
echo "DEFS = " ; echo "$DEFS" | adjust 
echo ""

cd $CURRDIR

cd $SDIR
if [ $? -ne 0 ]
then
  echo "Can't cd to source directory $SDIR" 1>&2
  exit 63
fi

RET=0
IFS="${IFS}:"	# GSI Database delimiter is colon

if [[ $GSIDBFORMAT -eq 0 ]]   #### Pre-10.0 format ###
then
   ########### Input format (see header comments)
   # <full-path-target> <fileset> <filetype> <status> <machine> \
   # <mode> <owner> <group> <link_source> <relative-path-source> <partner>
   FORMAT="TARGET SET TYPE STAT MACH MODE OWN GRP LINKSOURCE MISC PARTNER"
elif [[ $GSIDBFORMAT -eq 1 ]]   #### 10.0 format ###
then
   ########### Input format (see header comments)
   # <full-path-target> <product> <fileset> <filetype> <status> <machine> \
   # <mode> <owner> <group> <link_source> <relative-path-source> <partner>
   FORMAT="TARGET PRODUCT SET TYPE STAT MACH MODE OWN GRP LINKSOURCE MISC PARTNER"
elif [[ $GSIDBFORMAT -eq 2 ]]   #### 10.0 tarball format ###
then
   ########### Input format (see header comments)
   # <full-path-target> <product> <fileset> <filetype> <status> <machine> \
   # <mode> <owner> <group> <link_source> <relative-path-source> <partner>
   FORMAT="TARGET TYPE STAT MACH MODE OWN GRP LINKSOURCE MISC PARTNER"
fi

while read $FORMAT
do
   if [[ $GSIDBFORMAT -eq 0 ]]   #### Pre-10.0 format ###
   then
      OUTDIR="${ODIR}/${SET}${TARGET%/*}"
      OUTF="${ODIR}/${SET}${TARGET}"
   elif [[ $GSIDBFORMAT -eq 1 ]]   #### 10.0 format ###
   then
      OUTDIR="${ODIR}/${PRODUCT}/${SET}${TARGET%/*}"
      OUTF="${ODIR}/${PRODUCT}/${SET}${TARGET}"
   elif [[ $GSIDBFORMAT -eq 2 ]]   #### 10.0 tarball format ###
   then
      OUTDIR="${ODIR}/${TARGET%/*}"
      OUTF="${ODIR}/${TARGET}"
   fi

   if [ -f "${OUTF}" ]
   then
      echo "Warning: ${OUTF} already exists -- skipping" >&2
      continue    				   ## don't overwrite
   fi
   if [ ! -d "${OUTDIR}" ]
   then
      mkdir -p ${OUTDIR}    ## create target directory
      if [ $? -ne 0 ]
      then
         echo "Error: Can't create target directory:  -- continuing" >&2
         echo "	${OUTDIR}" >&2
         RET=64
         continue    				
      fi
   fi
   ####  Control type is for control script files ####
   if [[ "$TYPE" = "file" ]] || [[ "$TYPE" = "control" ]]
   then
      SOURCE="${MISC##*;}"
      if [[ ! -f "${SOURCE}" ]]
      then
         echo "Error: ${SOURCE} not found -- skipping" >&2
         RET=69
         continue    		
      fi
      if [[ "$SOURCE" = *\.h ]]   ## header file
      then
  	     ${UNIFDEF} $DEFS $SOURCE > ${OUTF}
	     ### can't interpret return code from unifdef
      else
	     /bin/cp $SOURCE ${OUTF}
	     if [ $? -ne 0 ]
	     then
	        echo "Error: Can't copy source file $SOURCE -- continuing" 1>&2
	        RET=65
	        continue    				
	      fi
      fi
   elif [[ "$TYPE" = "sym_link" ]]
   then
      /bin/ln -s $LINKSOURCE ${OUTF}
      if [ $? -ne 0 ]
      then
         echo "Error: Can't symlink target file ${OUTF} -- continuing" 1>&2
         RET=66
         continue    				
      fi
   elif [[ "$TYPE" = "hard_link" ]]
   then
      /bin/ln $LINKSOURCE ${OUTF}
      if [ $? -ne 0 ]
      then
         echo "Error: Can't link target file ${OUTF} -- continuing" 1>&2
         RET=66
         continue    				
      fi
   elif [[ "$TYPE" = "directory" ]]
   then
      /bin/mkdir -p ${OUTF}
      if [ $? -ne 0 ]
      then
         echo "Error: Can't create directory ${OUTF} -- continuing" 1>&2
         RET=66
         continue    				
      fi
   fi
   /bin/chmod $MODE ${OUTF}
   if [ $? -ne 0 ]
   then
      echo "Error: Can't chmod target file ${OUTF} -- continuing" 1>&2
      RET=66
      continue    				
   fi
   /bin/chgrp $GRP  ${OUTF}
   if [ $? -ne 0 ]
   then
      echo "Error: Can't chgrp target file ${OUTF} -- continuing" 1>&2
      RET=67
      continue    				
   fi
   /bin/chown $OWN  ${OUTF}
   if [ $? -ne 0 ]
   then
      echo "Error: Can't chown target file ${OUTF} -- continuing" 1>&2
      RET=68
      continue    				
   fi
done  ### while ###
  
cd $CURRDIR
exit $RET
