:
# @(#) pcopy - v4.0 - 10/20/91 - Copy a filePro/Profile file
#
# If run from command line _WITH_ arguments _NO_ help is offered.
usage="\nUsage:\t$0 [ -a | -c | -l ] [ fpfile clonefile ]\n"

# NOTE: activate the correct value for "ver" in the next lines:
ver=04.00.00          
# ver=03.00.00
# ver=01.02.00          # any 01.XX.XX value is OK on the T6000

# check to see if you have uncommented "ver".
[ -z "$ver" ] && 
{ echo "\007\n\tERROR: Version number not found.\n
		The filePro/Profile version number needs to be set, 
		please edit \"$0\" and try again.\n" 1>&2 ; exit 1 ; }

# if "ansi" forces operation in dt100 mode.
[ $TERM != "ansi" ] || { TERM="dt100" export TERM ;}

# check to determine if you are using filePro or Profile.
[ -f /etc/default/pfpath ] && {
PD=pf;PF=profile;default=/etc/default/pfpath ;} || {
PD=fp;PF=filepro;default=/etc/default/fppath ;}

PFPROG=${PFPROG:-`head -1 $default`}/$PD
PFDIR=${PFDIR:-`awk 'NR==2||NR==3{printf($0)}' $default`}/$PF

if [ -x $PFPROG/ddefine ]
	then PLUS=d
elif [ ! -x $PFPROG/define ]
	then echo "\n\"Define Files\" - not found." 1>&2 ; exit 1
fi

[ -w $PFPROG/lib/errmsg ] ||
{ echo "\nYou are NOT root or $profile.\nRoot password required.\n\nPlease enter ROOT \c" ; exec su root -c "$0 $*" || exit 1 ;}

# check for options
case $1 in
	-a) ans="Y" ; all="Y" ; shift ;;
	-c) ans="Y" ; all="N" ; link="N" ; shift ;;
	-l) ans="Y" ; all="N" ; link="Y" ; shift ;;
	-??) echo "\nOption Error:$usage";exit 1 ;;
esac
case $# in
	0) echo "\f\t\t\t\t\"$0\"\n\n\t\tfacilitates $profile file facsimiles . . .
\nDo you want instructions ?  y or n : \c"
read choice
	case "$choice" in
		[Yy]*) echo "
  You need to use the $profile file definition program to create a destination 
  file having the desired name. If you haven't already done so, 'pcopy' will 
  do it for you!
  Screens and Output Formats will have NO passwords, and default responses to 
  the queries posed by their creation programs except as noted below:\n
  In version 3.0 and below Output formats offering an optional alignment check
  will have garbage passwords.
  In version 4.0 Output formats NOT offering alignment check will have option 
  \"Remove All Blank lines\" defaulted to YES.\n
  If this is an 'alien' file, and the name of the data file in the clone
  should differ from the source, you will have to edit that 'map' file, 
  or go through its file definition again to assign the correct data file.\n
  You will have an option to copy all files, or not copy the data files and
  indexes (only their keys). If you elect the latter, you will be given an
  option to link, rather than copy, applicable files.\n
  Three option flags are available:
	-a create clone file and copy all data and indexes
	-c create clone file and copy only programs
	-l create clone file and link, not copy, only programs\n
  Press <ENTER> to continue, or <BREAK> to quit. \c"
read akey ;;
esac
echo "\fFilename to Copy: \c"
read FC
      if [ -z "$FC" ]
      then 
         echo "$usage" 1>&2;exit 1
      fi
echo "\nDestination Filename: \c"
read DF
      if [ -z "$DF" ]
      then 
         echo "$usage" 1>&2;exit 1
      elif [ "$FC" = "$DF" ]
      then 
         echo "\n\"$FC\" == \"$DF\"\n$usage" 1>&2;exit 1
      fi
set $FC $DF;;
   2) if [ $2 = $1 ]
        then echo "\n\"$2\" == \"$2\"\n$usage" 1>&2 ; exit 1
      fi;;
   *) echo "\n$usage" 1>&2; exit 1;;
esac

cd $PFDIR

PFNAME=$2 export PFNAME

echo "\nChecking \"$PFDIR\" directory for \"$1\" & \"$2\" . . . \c"
if [ ! -d $1 ]
   then echo "\n\n$0:  \"$1\" - is not a $profile file.\n" 1>&2 ; exit 1
elif [ -s $2 -a ! -d $2 ]
   then echo "\n\n$0:  \"$2\" - is not a $profile file.\n" 1>&2 ; exit 1
elif [ ! -d $2 ]
   then echo "\n\n$0:  \"$2\" - does not exist.\n" 1>&2
      [ -z "$ans" ] && \
      { echo "==>  Create \"$2\" . . . Yes or No ?  ( y or n ): \c" ;
      read ans ; } || { echo "==> Creating \"$2\" . . . \c" ; }
      case "$ans" in
      [Yy]*) qlf=`cat -s $1/qualify` 
             [ -n "$qlf" ] && { 
                           case "$ver" in
                              04.*) yes="NNNY$qlf\033\033";;
                                 *) yes="YNNN$qlf\033\033";;
                           esac ; } || { yes="NNNN" ; }
             echo -n "Y1\n\033\033X$yes" | $PFPROG/${PLUS}define 1>/dev/null;;
          *) echo "\n$0:  ABORTED . . . \"$2\" - NOT created.\n" 1>&2
                exit 1 ;;
      esac
fi
warning="\n\n\007WARNING: \"$2\" contains data which will be overwritten. . .  
\n\t  Press <ENTER> to continue, or <BREAK> to quit. . . \c"
[ -s $2/key -o -s $2/data ] && { echo "$warning"; read akey; } ||
{ [ -s $2/qualify ] && for q in `cat $2/qualify`; do
[ -s $2/key$q -o -s $2/data$q ] && { echo "$warning"; read akey ; } done ; }
[ -z "$all" ] &&
{ echo "\n\nCopy DATA & INDEXES as well as programs ?  ( y or n ): \c" ;
read all ; }
        case "$all" in
        [Yy]*) : ok ;;
            *) all="N" ; 
             [ -z "$link" ] &&
              { echo "\nLink, not copy, applicable files ?  ( y or n ): \c" ;
                read link ; } ;;
esac

echo "\n\nAssuring \"$2\" is 'a=rwx' mode . . . \c"
chmod a=rwx $2

trap 'rm -f $PFDIR/$2/map$$ ; trap 0 ; exit' 0 1 2 3 15

echo "\n\nPreserving passwords in the \"$2\" 'map' file . . . \c"
head -1 $2/map >$2/map$$

echo "\n\nCopying the filePro file \"$1\" to \"$2\" . . . \n"
case "$all" in
   [Yy]*) copy -ov $1 $2 ;;
   [Nn]*) cd $1
          for file in `ls map screen.* out.* 2>/dev/null`
          do
             echo "Copying $1/$file to $2/$file"
                  cp $file ../$2
          done
          for file in `ls prc.* tok.* brw.* sel.* edits help qualify 2>/dev/null`
          do
             case "$link" in
                [Yy]*) echo "Linking $1/$file to $2/$file"
                       ln $file ../$2 ;;
                    *) echo "Copying $1/$file to $2/$file"
                       cp $file ../$2 ;;
             esac
          done
          for index in `ls index*.? 2>/dev/null`
          do
             echo "Copying $1/$index \"keys\" to $2/$index"
             ###### with a tip of the hat to Bob Snapp for this idea:
             dd if=$index of=../$2/$index bs=128 count=1 2>/dev/null
          done
          cd ..
          chown $profile $2/*;;
esac

cd $2

case "$all" in
[Nn]*) echo "\n\nRebuilding \"$2\" Indexes . . .\c"
        msg="$msg\n\tIndexes Rebuilt on same keys as \"$1\"\n\n"
        for i in A B C D E F G H I J K L M N O P 0 1 2 3 4 5 6 7 8 9 ; do
                [ -s index.$i ] || continue
                        $PFPROG/${PLUS}xmaint $2 -a -e -r -o$i 1>/dev/null
                [ -s qualify ] || continue
                        for q in `cat qualify` ; do
                        cp index.$i index$q.$i
                        done
          done;;
*) msg="$msg\n\tIndexes NOT Rebuilt\n\n";;
esac

echo "\n\nAssuring \"$2\" files are 'a=rw' mode . . . \c"
chmod a=rw *

echo "\n\nEditing the \"$2\" 'map' file passwords . . . \c"
cat map >>map$$
<map$$ awk -F: '
BEGIN { OFS = ":" }
{
   if ( NR == 1 ) {
      split( $0, field )
      for( i = 5; i <= NF; i++ ) { pwords = pwords ":" field[i] }
      next
   }
   if ( NR == 2 ) { print $1, $2, $3, $4 pwords ; next }
   print
} ' >map

# also a tip of the hat to Nancy Palmquist of Small Computer Company
# for the germ of the idea that's a basis for what follows:

case "$ver" in
0[34].*)
responses=`ls screen.* 2>/dev/null | awk '
   {
      name = substr( $0, 8, length($0) - 7 )
      option = "\024"
      printf "%s%s\nU%sOY\n%c%c%c%c", yes, name, option, 27, 27, 27, 27
      yes = "S\033\133\110"
   }
   END { printf "X" } '`;;

*)
responses=`ls screen.* 2>/dev/null | awk '
   {
      name = substr( $0, 8, length($0) - 7 )
      printf "%s%s\nU%c%cY\nNNN", yes, name, 27, 27
      yes = "Y"
   }
   END { printf "N" } '`;;
esac

case "$responses" in
   [XxNn]) echo "\nNo Screens copied . . .\n" ;;
   *) echo "\n\nRemoving \"$2\" Screen Passwords . . . \c"
      msg="$msg\tScreens are set to defaults, with no passwords.\n\n"
      echo -n "$responses" | $PFPROG/${PLUS}screen 1>/dev/null;;
esac

case "$ver" in
04.*)
responses=`ls -l out.* | sort +4 2>/dev/null | awk '
   {
      name = substr( $NF, 5, length( $NF ) - 4 )
      option = "\024"
      if ( $5 <= 184 ) {
         format = "%s%s\nU%sOY%c%c%c%c"
         printf format, yes, name, option, 27, 27, 27, 27
	     } 
      else {
         bs3 = "\033\133\101\033\133\101\033\133\101"
         bs4 = "\033\133\101\033\133\101\033\133\101\033\133\101"
         format = "%s%s\nU%sO%s%sY\nY%c%c%c%c"
         printf format, yes, name, option, bs3, bs4, 27, 27, 27, 27
         }
      yes = "S"
   }
   END { printf "X" }'`;;

03.*)
responses=`ls -l out.* 2>/dev/null | awk '
   {
      name = substr( $NF, 5, length( $NF ) - 4 )
      if ( $5 == 158 ) {
         format = "%s%s\n%c%c%c%cNNY\n"
         printf format, yes, name, 27, 27, 27, 27
      }
      else {
         format = "%s%s\n%c%c%c%c%c%cNY\nNN"
         printf format, yes, name, 27, 27, 27, 27, 27, 27
      }
      yes = "Y\033\133\110"
   }
   END { printf "N" }'`;;

*)
responses=`ls -l out.* 2>/dev/null | awk '
   {
      name = substr( $NF, 5, length( $NF ) - 4 )
      if ( $5 == 156 ) {
         format = "%s%s\n%c%c%c%cNNY\n"
         printf format, yes, name, 27, 27, 27, 27
      }
      else {
         format = "%s%s\n%c%c%c%c%c%cNY\nNN"
         printf format, yes, name, 27, 27, 27, 27, 27, 27
      }
      yes = "Y"
   }
   END { printf "N" }'`;;
esac

case "$responses" in
 [NnXx]) echo "\nNo Output Formats copied . . .\n";;
 *) echo "\n\nRemoving \"$2\" Output Format Passwords . . . \c"
    msg="$msg\tOutput Formats are set to defaults, with no passwords.\n"
    case "$ver" in
      04.*) msg="$msg\t>>[ Those with NO alignment check option ]<<"
            msg="$msg\n\t>>[ defaulted to Remove All Blank Lines. ]<<\n\n";;
	  *) msg="$msg\t>>[ Except those with an alignment check option. ]<<\n\n";;
    esac
      echo -n "$responses" | $PFPROG/${PLUS}moedef 1>/dev/null;;
esac

echo "\007\n\n\nThe deed is done . . . \"$2\" is ready for you.\n\n$msg\n
Press <ENTER> to continue......\c"
read anything
exit

#########################################################################
#                                                                       #
#     pcopy - Copy a filePro/Profile file                               #
#                                                                       #
#     Usage:  pcopy [ source destination ]                              #
#                                                                       #
#     This procedure facilitates copying a filePro file and all         #
#     of its components to make a 'clone' with a different name.        #
#                                                                       #
#     You must first define a destination file using your filePro       #
#     definition program (define/ddefine), which you may do before      #
#     you run 'pcopy' - or, 'pcopy' will do it for you!                 #
#                                                                       #
#     You may want to edit 'pcopy', because it changes the mode of      #
#     the destination directory to 'a=rwx', and the destination         #
#     files to 'a=rw'.                                                  #
#                                                                       #
#     The password protection scheme of filePro/Profile is better       #
#     designed to frustrate the developer/programmer that to protect    #
#     the programs and data: when one copies an fp file, the screen,    #
#     output format, and map passwords are disturbed.  These are all    #
#     handled for you by 'pcopy' in a somewhat kludge way.  It leaves   #
#     the screens and output formats with NO passwords, but with the    #
#     default responses to the questions asked by the output format     #
#     and screen definition programs.                                   #
#     You may still have to go back and edit screens/output formats     #
#     to prevent record deletion from certain screens, or to prevent    #
#     output formats from sucking up blank lines, etc.  Output          #
#     formats optionally offering an alignment check will have their    #
#     passwords screwed up.  This won't do it all for you, but it       #
#     can be a great time-saver when you need to copy an fp file.       #
#                                                                       #
#     Note:  If you clone a file that already contains a screen or      #
#     output format with a bad checksum (from copying one to a new      #
#     name within a file, and then not adjusting the password) the      #
#     order of prompts presented by the filePro programs may vary       #
#     from what this program anticipates, and it will hang in the       #
#     midst of doing its work.  You may have to issue a 'quit' to       #
#     get out (<CTRL><7> at a T6000 console; <CTRL><\> at a DT-100).    #
#     What can you then do?  Correct them all by hand, or correct       #
#     the one in the source that caused the problem and run it again.   #
#                                                                       #
#     ( Why would one want to make a 'clone' file?  To change a file    #
#     name that was badly selected during one's early experience, or    #
#     for many other reasons.  Often this may be preferable to using    #
#     'qualifier' files, in which case you may want to change the       #
#     copies of the processing tables, edits, browse formats, help      #
#     files, selection tables and tokenized tables, if any, to be       #
#     links to their source-file counterparts, rather than copies       #
#     of them, to facilitate program maintenance as well as saving      #
#     disk space.  A final option in the program automates this. )      #
#                                                                       #
#     There is little, if any, error checking for your responses        #
#     built into this script.                                           #
#     Let me know if you have improvements to what is here.             #
#                                                                       #
#     Bob Stockler [72726,452] - 10/23/1989                             #
#                                                                       #
#     filePro Version 03.00 runs differently than earlier versions,     #
#     which required changes in how the original 'pcopy' responded      #
#     when attempting to change screen and output passwords.            #
#     Changes include a test to determine if you are using filePro or   #
#     Profile, ( variables $pf & $profile ), adding the ability to run  #
#     from the command line or filePro defined menu.                    #
#     Obtains the correct version of filePro/Profile by  _YOU_          #
#     uncommenting the variable '$ver'.                                 #
#     TERM is set to accept any terminal type EXCEPT "ansi", because    #
#     script is set up to accept the "CTRL-T" function to access the    #
#     Extended Functions Menu in define screens.                        #
#     "ANSI" consoles forced to emulate "dt100".                        #
#     Screen display may be eliminated by the addition of '1>/dev/null' #
#     to the end of the appropriate '$response' pipe.                   #
#     All indexes are rebuilt automatically if no data is copied.       #
#     Three option flags have been added they are:                      #
#                                                                       #
#        -a  Which answers "yes" to the prompt:                         #
#                Copy DATA & INDEXES as well as programs ?              #
#                                                                       #
#        -c  Which answers "no" to above and "no" to the prompt:        #
#                Link, not copy, applicable files ?                     #
#                                                                       #
#        -l  Which answers "no" to the "-a" flag but "yes" to:          #
#                Link, not copy, applicable files ?                     #
#                                                                       #
#     In any case where an option is indicated the target file is       #
#     created automatically if it does not exist.                       #
#     Options are available from the command line or may be added       #
#     to the command line of a user defined menu.                       #
#                                                                       #
#     Modified by:                                                      #
#     Michael McConaghy [70363,336] - 01/22/91                          #
#                                                                       #
#     filePro Version 04.00 runs differently than earlier versions,     #
#     which required changes in how the modified 'pcopy' responded      #
#     when attempting to change output format passwords, and            #
#     in the sequence used to create "qualified" files.                 #
#                                                                       #
#     A new section has been added to handle the Output formats,        #
#     it's only draw back being, If NO alignment check is offered,      #
#     will change the default response for the prompt:                  #
#     "Remove All Blank Lines?" from NO to YES.......                   #
#     If your form requires the default, convert it back manually.      #
#                                                                       #
#     The section used to create "qualified" files has been modified,   #
#     The sequence formerly used has been reversed for version 4.0.     #
#                                                                       #
#     The additional Automatic Index letters, supplied with version     #
#     4.0, have been added to the index rebuilding section.             #
#                                                                       # 
#     Every attempted has been made to make this version of 'pcopy'     #
#     as backward compatible as possible, but as always.............    #
#                                                                       #
#                       (all disclaimers apply)                         #
#     Modified by:                                                      #
#     Michael McConaghy [70363,336] - 10/20/91                          #
#                                                                       #
#########################################################################
