#!/usr/bin/ksh
#
# SAMPLE NAME: extractFilesForComponentForRelease
#
# FUNCTIONS:   This sample shell script extracts all the files 
#              associated with a specific component and a release 
#              and places them in 
#              the path specified by the relativePathName parameter.
#              The versions of the files can be current or committed.
#
# USAGE: 
# extractFilesForComponentForRelease \
#      componentName releaseName relativePathName [current|committed]
#                                                  *******
# ENVIRONMENT VARIABLE(S): CMVC_FAMILY [CMVC_BECOME]
#
# 5765-207 (C) COPYRIGHT International Business Machines Corp. 1993,1998
# 5765-202 (C) COPYRIGHT International Business Machines Corp. 1993,1998
# 5622-063 (C) COPYRIGHT International Business Machines Corp. 1993,1998
# All Rights Reserved
# Licensed Materials - Property of IBM
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#           NOTICE TO USERS OF THE SOURCE CODE EXAMPLES
#
# INTERNATIONAL BUSINESS MACHINES CORPORATION PROVIDES THE SOURCE CODE
# EXAMPLES, BOTH INDIVIDUALLY AND AS ONE OR MORE GROUPS, "AS IS" WITHOUT
# WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
# LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
# OF THE SOURCE CODE EXAMPLES, BOTH INDIVIDUALLY AND AS ONE OR MORE GROUPS,
# IS WITH YOU.  SHOULD ANY PART OF THE SOURCE CODE EXAMPLES PROVE
# DEFECTIVE, YOU (AND NOT IBM OR AN AUTHORIZED RISC System/6000* WORKSTATION
# DEALER) ASSUME THE ENTIRE COST OF ALL NECESSARY SERVICING, REPAIR OR
# CORRECTION.
#
# * RISC System/6000 is a trademark of International Business Machines
#   Corporation.
#
# /usr/lpp/cmvc/samples/extractFilesForComponentForRelease
#


# Validation of input parameters and environment variables.

if [ $# -eq 0 ]
then
  print "usage: $0 componentName releaseName relativePathName [committed]"
  print "This sample shell script extracts all the files associated with"
  print "a specific component and a release and places them"
  print "in the path specified by the relativePathName parameter."
  print "By default, the current version is extracted, unless"
  print "the committed parameter is specified."
  exit 1
fi

if [ -z "$CMVC_FAMILY" ]
then
  print "The CMVC family must be set with the CMVC_FAMILY environment variable."
  exit 1
fi

#
if [ $# -gt 4 ]
then
  print "Provide a componentName, a releaseName and a relativePathName"
  print "as input parameters.\n"
  exit 1
fi

componentName=$1
releaseName=$2
relative=$3

if [ $# -eq 4 ]
then
  desiredVersion=$4
  if [ "$desiredVersion" = "committed" ] || [ "$desiredVersion" = "current" ]
  then
    print ""     # no-op
  else
    print "The desired version should be either current (default) or committed"
    exit 1
  fi
else
  desiredVersion="current"
fi

RELEASENAME=$releaseName
PATHNAME=
SAVEPATHNAME=

# Find out the committed files in a component and extract them

Report -view FileView -where "compName='$componentName' and releaseName='$releaseName' and dropDate is null order by pathName" -raw > /tmp/cv$$
while read line
do
   PATHNAME=`echo $line | cut -d'|' -f8 `
   if [ "$desiredVersion" = "committed" ] 
   then
     VERSION=`echo $line | cut -d'|' -f4 `
     print "Extracting file: $PATHNAME committed version $VERSION release $RELEASENAME"
   else
     VERSION=`echo $line | cut -d'|' -f9 `
     print "Extracting file: $PATHNAME current version $VERSION release $RELEASENAME"
   fi
   File -extract $PATHNAME -relative $relative -release $RELEASENAME -version $VERSION
done </tmp/cv$$

# Cleanup the temporary file
[ -r /tmp/cv$$ ] && rm -f /tmp/cv$$

print "The extraction of the files is done!"

exit 0

### end of file ###
