#!/usr/bin/ksh
#*BEGIN CMVC */
#**************************************************************************
#
#   Workfile: %W%
#   Last update: %E% %U%
#   SCCS path, id: %F% %I%
#
#*************************************************************************/
#*END CMVC */
#*BEGIN COPYRIGHT */
#**************************************************************************
#
#   Licensed Materials - Property of IBM
#   5697-216
#   (C) Copyright IBM Corp. 1999
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract
#   with IBM Corp.
#
#*************************************************************************/
#*END COPYRIGHT */
#**************************************************************************
# DISCLAIMER
# ----------
# This material contains programming source code for your consideration.
# These examples have not been thoroughly tested under all conditions.
# IBM, therefore, cannot guarantee or imply reliability, serviceability,
# or function of these programs.
# ALL PROGRAMS CONTAINED HEREIN ARE PROVIDED TO YOU "AS IS", WITHOUT ANY
# WARRANTIES (EXPRESS OR IMPLIED) OR SUPPORT WHATSOEVER, INCLUDING BUT
# NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS
# FOR A PARTICULAR PURPOSE.
#*************************************************************************
#END DISCLAIMER

# -------------------------------------------------------------------------
#
# EXMPFMKS FlowMark DB Migration Assistance ksh script
#
# Author ..........:  H. Akermann
# Created at ......:  February 1999
#
# See help for details.
# -------------------------------------------------------------------------

# -------------------------------------------------------------------------
# Set the defaults
# -------------------------------------------------------------------------
dbExp="EXMDB"                           # export FM database name
dbuid="ADMIN"                           # export FM database administrator
dbpwd="password"                        # export FM database password

groupSize="100"                         # number of top level process instances
                                        # exported in one FRL file

cntFile="content"                       # content of export FM database
wkDir=`pwd`                             # default working directory
tmpFile="tmp"                           # temporary working file
fileprefix="exp"                        # FDL/FRL file prefix

fkshExp="rexport"                       # Name of export ksh script
fkshImp="rimport"                       # Name of import ksh script

curTimeStamp=`date`


# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# functions
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------

# -------------------------------------------------------------------------
# Display help
# -------------------------------------------------------------------------
dsp_help()
{
  echo
  echo " EXMPFMKS FlowMark DB Migration Assistance ksh script"
  echo ""
  echo " Purpose:"
  echo " --------"
  echo "   This script is an aid to export an entire FM database and"
  echo "   import the retrieved data in a new FM database."
  echo ""
  echo "   The following FM objects are processed:"
  echo "   Buildtime data:"
  echo "     - all staff objects  (including the password of the persons)"
  echo "     - all process model, program, structure, server"
  echo ""
  echo "   Runtime data:"
  echo "     - all process instances, referenced subprocess and related"
  echo "       process template"
  echo "     - all worklists and process lists of persons"
  echo ""
  echo " Steps:"
  echo " ------"
  echo "   1. Preparation script '$0'"
  echo "      - retrieve the contents of runtime data (process instances)"
  echo "        and generates several control files for runtime export"
  echo "      - generate the export and import scripts"
  echo ""
  echo "   2. Export script '$fkshExp'"
  echo "      - Export all buildtime data using the utility exmpfexp"
  echo "        and write an FDL file"
  echo "      - Export all process instance and worklist data using the"
  echo "        utility exmpfrex and write FRL files"
  echo ""
  echo "   3. Import script '$fkshImp'"
  echo "      - Import all buildtime and runtime data from the previous step"
  echo "        using the utility exmpfimp"
  echo "      - Translate all process models retrieved from the FDL file"
  echo "        using the utility exmpftra"
  echo ""
  echo "   How to customize the 'groupsize' parameter"
  echo "      The specified groupsize determines the number of top level"
  echo "      process instances in one FRL file. Subprocess instances come"
  echo "      together with their parents."
  echo ""
  echo "      Consider the following to specify an appropriate value:"
  echo "      1. During import incomplete objects are kept in memory until the"
  echo "         references from them or to them are resolved."
  echo "         The number of referenced subprocess instances and the"
  echo "         size of container data increase the memory usage."
  echo "         Therefore keep a FRL file small (rule of a thumb: 6MB)."
  echo ""
  echo "      2. If you experience any system shortage during the import"
  echo "         during the import e.g. out of memory, reduce the groupsize"
  echo "         value and redo the export/import again."
  echo ""
  echo ""
  echo " Invocation syntax:"
  echo " ------------------"
  echo "                         +---<-----+"
  echo "              exmpfmks --- options -----------------><                    "
  echo
  echo "  options:                                                                "
  echo
  echo "      +- -d EXMDB  -+                                                   "
  echo "  |---+- -d dbname -+----------------->   export database               "
  echo
  echo "      +- -u ADMIN  -+                                                   "
  echo "  >---+- -u userid -+----------------->   export database               "
  echo
  echo "      +- -p password -+                                                 "
  echo "  >---+- -p password -+--------------->   export database               "
  echo
  echo "      +- -g 100 +                                                       "
  echo "  >---+- -g n  -+--------------------->   Number of TL Process Instance "
  echo "                                            exported in one step          "
  echo
  echo "      +- -w currentDirectory -+                                         "
  echo "  >---+- -w workingdirectory -+------->   Working directory             "
  echo "                                                                          "
  echo
}

# -------------------------------------------------------------------------
# Generate the export ksh script
# -------------------------------------------------------------------------
genExpScript()
{
  echo "#!/usr/bin/ksh"                                                    >  $fkshExp
  echo "# ------------------------------------------------------------"    >> $fkshExp
  echo "# FlowMark Migration Assistance ksh script"                        >> $fkshExp
  echo "#  Export the an entire FM DATABASE"                               >> $fkshExp
  echo "# FILE...................: $fkshExp"                               >> $fkshExp
  echo "# Generated at...........: $curTimeStamp"                          >> $fkshExp
  echo "#"                                                                 >> $fkshExp
  echo "#"                                                                 >> $fkshExp
  echo "# Before starting FM migration, ensure the following:"             >> $fkshExp
  echo "#  1. TAKE A BACKUP COPY of YOUR FM OPERATIONAL DATABASE"          >> $fkshExp
  echo "#"                                                                 >> $fkshExp
  echo "#  2. Finish as many process instances as you can"                 >> $fkshExp
  echo "#"                                                                 >> $fkshExp
  echo "#  3. Stop the FM server, keep the Database server running"        >> $fkshExp
  echo "#"                                                                 >> $fkshExp
  echo "#   ------------------------------------------------------- "      >> $fkshExp
  echo "#  | If the procedure fails, analyze the export error      |"      >> $fkshExp
  echo "#  | files $fileprefix*.err.                               |"      >> $fkshExp
  echo "#   ------------------------------------------------------- "      >> $fkshExp
  echo "#"                                                                 >> $fkshExp
  echo "# ------------------------------------------------------------"    >> $fkshExp

  echo "#"                                                                 >> $fkshExp
  echo "echo"                                                              >> $fkshExp
  echo "echo Starting $fkshExp ..."                                        >> $fkshExp
  echo "echo DB name: $dbExp"                                              >> $fkshExp

  # -------------------------------------------------------------------------
  # export the FDL
  # -------------------------------------------------------------------------
  echo "#"                                                                 >> $fkshExp
  echo "echo"                                                              >> $fkshExp
  echo "echo ------------------------------------------------------------" >> $fkshExp
  echo "echo Export the FDL"                                               >> $fkshExp
  echo "echo ------------------------------------------------------------" >> $fkshExp

  echo "exmpfexp $fdlFile -m $optExp" >> $fkshExp  # -m export password

  # -------------------------------------------------------------------------
  # export the frl
  # -------------------------------------------------------------------------
  echo "#"                                                                 >> $fkshExp
  echo "echo"                                                              >> $fkshExp
  echo "echo ------------------------------------------------------------" >> $fkshExp
  echo "echo Export the FRLs"                                              >> $fkshExp
  echo "echo ------------------------------------------------------------" >> $fkshExp

  let count=1
  while (( $count < ${#expFiles[*]} )); do
    fn=${expFiles[count]}
    echo "exmpfrex $fn.frl $optExp -c$fn"                                  >> $fkshExp
    let count=$count+1
  done

  # -------------------------------------------------------------------------
  # export the person worklists
  # -------------------------------------------------------------------------
  echo "#"                                                                 >> $fkshExp
  echo "echo"                                                              >> $fkshExp
  echo "echo ------------------------------------------------------------" >> $fkshExp
  echo "echo Export the person worklists"                                  >> $fkshExp
  echo "echo ------------------------------------------------------------" >> $fkshExp

  fn=$fileprefix$count                  # generate command file name
  echo "WORKLIST *" > $fn
  echo "exmpfrex $fn.frl $optExp -c$fn"                                    >> $fkshExp

  # -------------------------------------------------------------------------
  # Reads the FDL file, extract all process names and generate commands
  # to call the translation utility.
  # -------------------------------------------------------------------------
  # -------------------------------------------------------------------------
  # generate the translate process model commands
  # -------------------------------------------------------------------------
  echo "#"                                                                 >> $fkshExp
  echo "echo"                                                              >> $fkshExp
  echo "echo ------------------------------------------------------------" >> $fkshExp
  echo "echo Generate the translate process model commands"                >> $fkshExp
  echo "echo ------------------------------------------------------------" >> $fkshExp

  tra="grep '^PROCESS' $fdlFile | cut -d\' -f2 |"
  tra=$tra"awk '{printf(\"exmpftra \\\"%s\\\" -r $optTra\\\n\",\$0);}'>> $fkshImp"
  echo $tra                                                                >> $fkshExp

  echo "#"                                                                 >> $fkshExp
  echo "echo"                                                              >> $fkshExp
  echo "echo ------------------------------------------------------------" >> $fkshExp
  echo "echo \" ...Invoke '$fkshImp' to start the import  \" "             >> $fkshExp
  echo "echo ------------------------------------------------------------" >> $fkshExp
}


# -------------------------------------------------------------------------
# Generate the import ksh script
# -------------------------------------------------------------------------
genImpScript()
{
  echo "#!/usr/bin/ksh"                                                    >  $fkshImp
  echo "# ------------------------------------------------------------"    >> $fkshImp
  echo "# FlowMark Migration Assistance ksh script"                        >> $fkshImp
  echo "#  Import FDL/FRL file"                                            >> $fkshImp
  echo "# FILE...................: $fkshImp"                               >> $fkshImp
  echo "# Generated at...........: $curTimeStamp"                          >> $fkshImp
  echo "#"                                                                 >> $fkshImp
  echo "#   ------------------------------------------------------- "      >> $fkshImp
  echo "#  | Run this commandfile on the target FM server.         |"      >> $fkshImp
  echo "#  | (DB server is running)                                |"      >> $fkshImp
  echo "#   ------------------------------------------------------- "      >> $fkshImp
  echo "#"                                                                 >> $fkshImp
  echo "#  Before starting this procedure:"                                >> $fkshImp
  echo "#  1. Be sure to have the database server up and running"          >> $fkshImp
  echo "#  2. Be sure to have this procedure and the FDL/FRL files"        >> $fkshImp
  echo "#     in the current directory"                                    >> $fkshImp
  echo "#"                                                                 >> $fkshImp
  echo "#  This procedure performs the following:"                         >> $fkshImp
  echo "#  1. Import the FM Buildtime data w/o translating the process"    >> $fkshImp
  echo "#"                                                                 >> $fkshImp
  echo "#  2. Import the FM Runtime data"                                  >> $fkshImp
  echo "#"                                                                 >> $fkshImp
  echo "#  3. Translate the process model found in '$fdlFile'"             >> $fkshImp
  echo "#"                                                                 >> $fkshImp
  echo "# ------------------------------------------------------------"    >> $fkshImp

  echo "#"                                                                 >> $fkshImp
  echo "dbImp=$dbImp"                                                      >> $fkshImp
  echo "if [ -n \"\$1\" ]; then"                                           >> $fkshImp
  echo "  dbImp=\$1"                                                       >> $fkshImp
  echo "fi"                                                                >> $fkshImp

  echo "#"                                                                 >> $fkshImp
  echo "echo"                                                              >> $fkshImp
  echo "echo Starting $fkshImp ..."                                        >> $fkshImp
  echo "echo DB name: $dbImp"                                              >> $fkshImp

  echo "echo"                                                              >> $fkshImp
  echo "echo ------------------------------------------------------------" >> $fkshImp
  echo "echo Import the FDL files"                                         >> $fkshImp
  echo "echo ------------------------------------------------------------" >> $fkshImp

  echo "exmpfimp $fdlFile $optFDLImp"                                      >> $fkshImp

  echo "#"                                                                 >> $fkshImp
  echo "echo"                                                              >> $fkshImp
  echo "echo ------------------------------------------------------------" >> $fkshImp
  echo "echo Import the FRL files"                                         >> $fkshImp
  echo "echo ------------------------------------------------------------" >> $fkshImp

  let count=1
  while (( $count < ${#expFiles[*]} )); do
    fn=${expFiles[count]}
    echo "exmpfrim $fn.frl $optImp"                                        >> $fkshImp
    let count=$count+1
  done

  echo "#"                                                                 >> $fkshImp
  echo "echo"                                                              >> $fkshImp
  echo "echo ------------------------------------------------------------" >> $fkshImp
  echo "echo Import the person worklists"                                  >> $fkshImp
  echo "echo ------------------------------------------------------------" >> $fkshImp

  fn=$fileprefix$count                  # generate command file name
  echo "exmpfrim $fn.frl $optImp"                                          >> $fkshImp

  echo "#"                                                                 >> $fkshImp
  echo "echo"                                                              >> $fkshImp
  echo "echo ------------------------------------------------------------" >> $fkshImp
  echo "echo Translate the process model extracted from FDL file"          >> $fkshImp
  echo "echo ------------------------------------------------------------" >> $fkshImp
}

# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# Main
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------

# -------------------------------------------------------------------------
# Get the specified options
# -------------------------------------------------------------------------
bwkDir=""

while getopts ":d:u:p:g:w:h" opt; do
echo "opt: $opt optarg $OPTARG"
  case $opt in
    d )  dbExp=$OPTARG;;

    u )  dbuid=$OPTARG;;

    p )  dbpwd=$OPTARG;;

    g )  groupSize=$OPTARG;;

    w )  bwkDir=$OPTARG;;

    h )  dsp_help
         return 1;;

    : )  echo "missing value for $opt"
         dsp_help
         return 1;;

    ? )  echo 'unknown option specified'
         dsp_help
         return 1;;
  esac
done
shift $(($OPTIND -1))

echo "Used options:"
echo "  dbExp    :  $dbExp "
echo "  dbuid    :  $dbuid "
#echo "  dbpwd    :  $dbpwd "
echo "  groupSize:  $groupSize "
echo "  wkDir    :  $wkDir "

# -------------------------------------------------------------------------
# Main processing
# 1. Cleanup from previous run
# 2. Retrieve the content of the export database
# 3. Split and generate the frl export command files
# 4. Generate the export ksh script
# 5. Generate the import ksh script
#
# -------------------------------------------------------------------------

# -------------------------------------------------------------------------
# 1. Cleanup from previous run
# -------------------------------------------------------------------------
if [ -n "$bwkDir" ]; then                 # a working directory specified
  wkDir=$bwkDir
  if [ -d "$wkDir" ]; then
    echo
    echo "Delete working directory $wkDir"
    rm -R $wkDir                        # cleanup working directory
  fi

  mkdir $wkDir                          # create the working directory
                                        # add wrkDir to used files
else                                    # delete the files in current directory
  echo
  echo "Delete the used files in current directory"
  rm $fkshExp
  rm $fkshImp
  rm $tmpFile
  rm $cntFile
  rm $tmpFile
  ls $fileprefix* | { while read fn; do
                        rm $fn
                      done
                    }
fi

# -------------------------------------------------------------------------
# Set additional options
# -------------------------------------------------------------------------
                                        # add working directory
wkDir=$wkDir"/"
fkshExp=$wkDir$fkshExp
fkshImp=$wkDir$fkshImp
fileprefix=$wkDir$fileprefix
tmpFile=$wkDir$tmpFile
cntFile=$wkDir$cntFile

#       database name                  no prompting
#       |         userid    password  |     write error msg to file
#       |         |         |         |    |
optExp="-d=$dbExp -u=$dbuid -p=$dbpwd -nop -e"
optFDLImp="-d=\$dbImp -u=$dbuid -o -nop -e"
optImp="-d=\$dbImp -u=$dbuid -p=$dbpwd -nop -e"
optTra="-d=\$dbImp -u=$dbuid -p=$dbpwd -nop"

dbImp=$dbExp"2"                         # default new import FM database name

fdlFile=$fileprefix"0.fdl"

# -------------------------------------------------------------------------
# 2. Retrieve the content of the export database
# -------------------------------------------------------------------------
echo "CONTENT " $cntFile >$tmpFile
contCmd="exmpfrex "$cntFile" "$optExp" -c"$tmpFile

$contCmd

echo

if [[ $? > 0 || ! -f $cntFile ]]; then
  echo "Retrieve of database contents failed."
  return $?
fi

echo "  DB contains" `grep "TEMPLATE" $cntFile | wc -l` "process templates."
echo "  DB contains" `grep "INSTANCE_TL" $cntFile | wc -l` "top level process instances."
echo "  DB contains" `grep "INSTANCE " $cntFile | wc -l` "process instances."
echo

# -------------------------------------------------------------------------
# 3. Split and generate the frl export command files
#
# Extract all toplevel process instances names
#   Layout of content record
#   token value
#       1 INSTANCE INSTANCE_TL TEMPLATE
#       2 name
#       3 (STATE_TL
#       4 RUNNING) FINISHED)
# -------------------------------------------------------------------------
rm $tmpFile
grep "INSTANCE_TL" $cntFile | cut -d":(" -f1 > $tmpFile

expFiles=""                             # array of generated files
fn=""
let iLine=0
let iFile=1

while read curLine ; do
  let iLine=iLine+1
  fn=$fileprefix$iFile                  # generate command file name
  expFiles[$iFile]=$fn                  # save the command file name

  if (( $iLine % $groupSize > 0 )); then
    echo $curLine >> $fn
  else
    echo $curLine >> $fn
    print "Generated successful commandfile (...$iLine): $fn"
    let iFile=iFile+1
    fn=$fileprefix$iFile
  fi
done < $tmpFile

if (( $iLine % $groupSize > 0 )); then  # some left
  print "Generated successful commandfile (...$iLine): $fn"
  expFiles[$iFile]=$fn                  # save the command file name
fi

# -------------------------------------------------------------------------
# 4. Generate the export ksh script
# -------------------------------------------------------------------------
genExpScript

chmod +x $fkshExp                        # Make the ksh script executable
echo
echo "Generated successful export script:" $fkshExp

# -------------------------------------------------------------------------
# 5. Generate the import ksh script
# -------------------------------------------------------------------------
genImpScript

chmod +x $fkshImp                       # Make the ksh script executable
echo
echo "Generated successful import script:" $fkshImp

echo
echo $0 "sucessfully finished."
echo
echo " ------------------------------------------------------------"
echo " ...Invoke '$fkshExp' to start the export"
echo " ------------------------------------------------------------"
