function remove_old_files {
typeset OLD_FS='ios.database.rte'
typeset DBNAME="postgres13"
typeset UNPRE_SCRIPT="ios.viodb13.rte.unpre_u"


SAVEDIR="$BASEDIR/ios.database.bk/"
if ! mkdir -p "$SAVEDIR"; then
    echo "ERROR: Cannot create directory $SAVEDIR"
    exit 1
fi

echo "SAVEDIR=$BASEDIR/ios.database.bk/"
OLD_DIRS="

/usr/ios/db/$DBNAME/share/postgresql/timezone/Africa
/usr/ios/db/$DBNAME/share/postgresql/timezone/America/Argentina
/usr/ios/db/$DBNAME/share/postgresql/timezone/America/Indiana
/usr/ios/db/$DBNAME/share/postgresql/timezone/America/Kentucky
/usr/ios/db/$DBNAME/share/postgresql/timezone/America/North_Dakota
/usr/ios/db/$DBNAME/share/postgresql/timezone/America
/usr/ios/db/$DBNAME/share/postgresql/timezone/Antarctica
/usr/ios/db/$DBNAME/share/postgresql/timezone/Arctic
/usr/ios/db/$DBNAME/share/postgresql/timezone/Asia
/usr/ios/db/$DBNAME/share/postgresql/timezone/Atlantic
/usr/ios/db/$DBNAME/share/postgresql/timezone/Australia
/usr/ios/db/$DBNAME/share/postgresql/timezone/Brazil
/usr/ios/db/$DBNAME/share/postgresql/timezone/Canada
/usr/ios/db/$DBNAME/share/postgresql/timezone/Chile
/usr/ios/db/$DBNAME/share/postgresql/timezone/Etc
/usr/ios/db/$DBNAME/share/postgresql/timezone/Europe
/usr/ios/db/$DBNAME/share/postgresql/timezone/Indian
/usr/ios/db/$DBNAME/share/postgresql/timezone/Mexico
/usr/ios/db/$DBNAME/share/postgresql/timezone/Pacific
/usr/ios/db/$DBNAME/share/postgresql/timezone/US
/usr/ios/db/$DBNAME/share/postgresql/timezone
/usr/ios/db/$DBNAME/share/postgresql/extension
/usr/ios/db/$DBNAME/share/postgresql/timezonesets
/usr/ios/db/$DBNAME/share/postgresql
/usr/ios/db/$DBNAME/share/
/usr/ios/db/$DBNAME/lib/postgresql
/usr/ios/db/$DBNAME/lib
/usr/ios/db/$DBNAME/bin
"
SYM_FILE='/usr/ios/db/$DBNAME/share/pg_hba.conf.sample'

#  The location of the inventory database
export ODMDIR=/usr/lib/objrepos
export LC_ALL=C                  # Required, per "The AIX LPP Owner's Guide"
echo > $UNPRE_SCRIPT
echo "SAVEDIR=$BASEDIR/ios.database.bk/" > $UNPRE_SCRIPT

#============================================
# Define the fileset particulars, including
#  the list of files that are being removed.
#============================================
    typeset OLD_PKG_DIR="/usr/lpp/ios.database"
    typeset OLD_AL="$OLD_PKG_DIR/deinstl/$OLD_FS.al"
    typeset OLD_IL="$OLD_PKG_DIR/deinstl/$OLD_FS.inventory"
    typeset OLD_TCB="$OLD_PKG_DIR/deinstl/$OLD_FS.tcb"
    typeset OLD_SEC="/lpp/ios.database/deinstl/$OLD_FS.sec"
    ######################################
    ######################################
    ##
    ## Clean out the inventory database
    ##
    ######################################
    ######################################
    typeset LPP_ID=$(odmget -q name=$OLD_FS lpp | \
                               grep "lpp_id = " | \
                               awk '{print $3}')
    ## if LPP_ID is null, then the fileset is not installed.
    if [[ -n "$LPP_ID" ]]; then
        # $SAVEDIR/$OLD_FS.add is where we will save the inventory, for use if the update is rejected.
        [[ -f "$SAVEDIR/$OLD_FS.add" ]] && rm -f "$SAVEDIR/$OLD_FS.add"
                OLD_FILES=$(odmget -q "lpp_id=$LPP_ID" inventory | grep -e loc0 | grep -e $DBNAME | awk '{ print $3 }' | sed 's/"//g')
        echo "\n Clearing ODM entries ... "
        for FILE in $OLD_FILES; do
            FILE=$FILE
            typeset STANZA=$(odmget -q "loc0=$FILE and lpp_id=$LPP_ID" inventory)
            if [[ -n "$STANZA" ]]; then
                if ! odmget -q "loc0=$FILE and lpp_id=$LPP_ID" inventory >> "$SAVEDIR/$OLD_FS.add" 2>&1; then
                    echo "ERROR: Failed to save ODM entry for $FILE"
                    exit 1
                fi
                if ! odmdelete -o inventory -q "loc0=$FILE and lpp_id=$LPP_ID" >/dev/null 2>&1; then
                    echo "ERROR: Failed to delete ODM entry for $FILE"
                    exit 1
                fi
            fi
        done

        ###############
        #
        #  UNPRE_U
        #
        ###############
        if [[ -f "$SAVEDIR/$OLD_FS.add" ]]; then
            print "
if [ -f '$SAVEDIR/$OLD_FS.add' ]; then
    echo 'Restoring the file(s) to the inventory database...'
    odmadd '$SAVEDIR/$OLD_FS.add'
    if [ \$? -ne 0 ]; then
        echo 'ERROR: unable to restore the ODM stanzas to the inventory database for fileset $OLD_FS.'
        exit 1
    else
        rm -f '$SAVEDIR/$OLD_FS.add'
    fi
fi" >> $UNPRE_SCRIPT

        fi # End of if loop for $SAVEDIR/$OLD_FS.add
    fi  # end of if loop for �~@~Sn $LPP_ID


echo "\n Clearing access file entries ... "

    ######################################
    ######################################
    ##
    ## Clean out the access list
    ##
    ######################################
    ######################################
    if [[ -n "$LPP_ID" && \
          -f "$OLD_AL" && \
          ! -f "$SAVEDIR/$OLD_FS.al" ]]
    then
        for FILE in $OLD_FILES $OLD_DIRS; do
            if ! cp -pf "$OLD_AL" "$OLD_FS.al.$$"; then
                echo "ERROR: Failed to copy $OLD_AL"
                exit 1
            fi
            if ! grep -v "$FILE\$" "$OLD_AL" > "$OLD_FS.al.$$"; then
                rm -f "$OLD_FS.al.$$"
                echo "ERROR: Failed to process $OLD_AL"
                exit 1
            fi
            if (( $? == 0 )); then
                #===============================================================
                # Make sure no more than *one* file got removed! Of course, it's
                # possible for *none* to have been removed, too, so that's okay.
                #===============================================================
                typeset ORIG_SIZE=$(cat "$OLD_AL" | wc -l)
                typeset NEW_SIZE=$(cat "$OLD_FS.al.$$" | wc -l)
                if (( ORIG_SIZE - NEW_SIZE <= 1 )); then
                    grep "$FILE\$" "$OLD_AL" >> "$SAVEDIR/$OLD_FS.al" 2>/dev/null
                    if ! cp -pf "$OLD_FS.al.$$" "$OLD_AL"; then
                        echo "ERROR: Failed to update $OLD_AL"
                        exit 1
                    fi
                fi
                rm -f "$OLD_FS.al.$$"
            fi
        done

        ###############
        #
        #  UNPRE_U
        #
        ###############

        if [[ -f "$SAVEDIR/$OLD_FS.al" ]]; then
            print "
if [ -f '$SAVEDIR/$OLD_FS.al' ]; then
    echo 'Restoring the access list entries...'
    if ! cat '$SAVEDIR/$OLD_FS.al' >> '$OLD_AL'; then
        echo 'ERROR: Failed to restore access list entries'
        exit 1
    fi
    rm -f '$SAVEDIR/$OLD_FS.al'
fi" >> $UNPRE_SCRIPT

        fi # END of if loop to create unpre_u
        fi # END of if loop that the $OLD_AL exists

echo "\n Clearing inventory file entries ..."
    ######################################
    ######################################
    ##
    ## Clean out the inventory list
    ##
    ######################################
    ######################################
    if [[ -n "$LPP_ID" && \
          -f "$OLD_IL" && \
          ! -f "$SAVEDIR/$OLD_FS.inventory" ]]
    then
        if ! cp -pf "$OLD_IL" "$OLD_IL.$$"; then
            echo "ERROR: Failed to copy $OLD_IL"
            exit 1
        fi
        if ! echo -n > "$OLD_IL.$$"; then
            echo "ERROR: Failed to initialize $OLD_IL.$$"
            exit 1
        fi

        for FILE in $OLD_FILES ; do
            if ! /usr/bin/grep -p $FILE: $OLD_IL >> $SAVEDIR/$OLD_FS.inventory; then
                echo "ERROR: Failed to save inventory entry for $FILE"
                exit 1
            fi
            if ! grep -vp $FILE: $OLD_IL > $OLD_IL.$$; then
                echo "ERROR: Failed to process inventory for $FILE"
                exit 1
            fi

            if [[ -f "$OLD_IL.$$" ]]; then
                if ! cp -pf "$OLD_IL.$$" "$OLD_IL"; then
                    echo "ERROR: Failed to update $OLD_IL"
                    exit 1
                fi
                rm -f "$OLD_IL.$$"
            fi
        done

        ###############
        #
        #  UNPRE_U
        #
        ###############
        if [[ -f "$SAVEDIR/$OLD_FS.inventory" ]]; then
            print "
if [ -f '$SAVEDIR/$OLD_FS.inventory' ]; then
    echo 'Restoring the inventory list entries...'
    if ! cat '$SAVEDIR/$OLD_FS.inventory' >> '$OLD_IL'; then
        echo 'ERROR: Failed to restore inventory list entries'
        exit 1
    fi
    rm -f '$SAVEDIR/$OLD_FS.inventory'
fi" >> $UNPRE_SCRIPT
        fi # END of loop to create unpre_u
    fi # END of inventory list processing
### THE code fragment for TCB and privcmds/tsd are from another fileset, and don�~@~Yt have the
### RELOCATABLE enablement
    ######################################
    ######################################
    ##
    ## Clean out the TCB database
    ##
    ######################################
    ######################################
    if [[ -n "$LPP_ID" && \
          -f "$OLD_TCB" ]]; then

    [[ -f "$SAVEDIR/$OLD_FS.tcb" ]] && rm -f "$SAVEDIR/$OLD_FS.tcb"

        if ! cp -pf "$OLD_TCB" "$OLD_TCB.$$"; then
            echo "ERROR: Failed to copy $OLD_TCB"
            exit 1
        fi
        if ! echo > "$OLD_TCB.$$"; then
            echo "ERROR: Failed to initialize $OLD_TCB.$$"
            exit 1
        fi

        for FILE in $OLD_FILES $OLD_DIRS; do
            typeset KEEP=true
            while IFS= read -r LINE; do
                if [[ $LINE == $FILE: ]]; then
                    KEEP=
                          if ! tcbck -d "$FILE" 2>&1; then
                              echo "WARNING: tcbck -d failed for $FILE"
                          fi
                elif [[ $LINE == /* ]]; then
                    KEEP=true
                fi

                if [[ -n "$KEEP" ]]; then
                    if ! print -- "$LINE" >> "$OLD_TCB.$$"; then
                        echo "ERROR: Failed to write to $OLD_TCB.$$"
                        exit 1
                    fi
                else
                    if ! print -- "$LINE" >> "$SAVEDIR/$OLD_FS.tcb"; then
                        echo "ERROR: Failed to write to $SAVEDIR/$OLD_FS.tcb"
                        exit 1
                    fi
                fi
            done < "$OLD_TCB"

            if [[ -f "$OLD_TCB.$$" ]]; then
                if ! cp -pf "$OLD_TCB.$$" "$OLD_TCB"; then
                    echo "ERROR: Failed to update $OLD_TCB"
                    exit 1
                fi
                rm -f "$OLD_TCB.$$"
            fi
            done

        ###############
        #
        #  UNPRE_U
        #
        ###############
        if [[ -f "$SAVEDIR/$OLD_FS.tcb" ]]; then
            print "
if [ -f '$SAVEDIR/$OLD_FS.tcb' ]; then
    echo 'Restoring the tcb entries...'
    if ! cat '$SAVEDIR/$OLD_FS.tcb' >> '$OLD_TCB'; then
        echo 'ERROR: Failed to restore TCB entries'
        exit 1
    fi
    if ! tcbck -a -f '$SAVEDIR/$OLD_FS.tcb' 2>&1; then
        echo 'ERROR: tcbck -a failed'
        exit 1
    fi
    rm -f '$SAVEDIR/$OLD_FS.tcb'
fi" >> $UNPRE_SCRIPT

        fi  # END of unpre_u processing
    fi  # END of TCB processing

### THE code fragment for TCB and privcmds/tsd are from another fileset, and don�~@~Yt have the
### RELOCATABLE enablement

    ######################################
    ######################################
    ##
    ## Clean out the privcmds/tsd.dat database
    ##
    ######################################
    ######################################

    if [[ -n "$LPP_ID" && \
          -f "$OLD_SEC" ]]; then

    [[ -f "$SAVEDIR/$OLD_FS.sec" ]] && rm -f "$SAVEDIR/$OLD_FS.sec"

        if ! cp -pf "$OLD_SEC" "$OLD_SEC.$$"; then
            echo "ERROR: Failed to copy $OLD_SEC"
            exit 1
        fi
        if ! echo > "$OLD_SEC.$$"; then
            echo "ERROR: Failed to initialize $OLD_SEC.$$"
            exit 1
        fi
        for FILE in $OLD_FILES $OLD_DIRS; do
            typeset KEEP=true
            while IFS= read -r LINE; do
                    if [[ $LINE == $FILE: ]]; then
                    KEEP=
                elif [[ $LINE == /* ]]; then
                    KEEP=true
                fi

                if [[ -n "$KEEP" ]]; then
                    if ! print -- "$LINE" >> "$OLD_SEC.$$"; then
                        echo "ERROR: Failed to write to $OLD_SEC.$$"
                        exit 1
                    fi
                else
                    if ! print -- "$LINE" >> "$SAVEDIR/$OLD_FS.sec"; then
                        echo "ERROR: Failed to write to $SAVEDIR/$OLD_FS.sec"
                        exit 1
                    fi
                fi
            done < "$OLD_SEC"

            if [[ -f "$OLD_SEC.$$" ]]; then
                if ! cp -pf "$OLD_SEC.$$" "$OLD_SEC"; then
                    echo "ERROR: Failed to update $OLD_SEC"
                    exit 1
                fi
                rm -f "$OLD_SEC.$$"
            fi
        done

          if [ -f "$SAVEDIR/$OLD_FS.sec" ]; then
                if ! instsecattr -d -f "$SAVEDIR/$OLD_FS.sec" 2>&1; then
                    echo "ERROR: instsecattr -d failed"
                    exit 1
                fi
          fi

        ###############
        #
        #  UNPRE_U
        #
        ###############
        if [[ -f "$SAVEDIR/$OLD_FS.sec" ]]; then
            print "
if [ -f '$SAVEDIR/$OLD_FS.sec' ]; then
    echo 'Restoring the sec entries...'
    if ! cat '$SAVEDIR/$OLD_FS.sec' >> '$OLD_SEC'; then
        echo 'ERROR: Failed to restore sec entries'
        exit 1
    fi
    if ! instsecattr -a -f '$SAVEDIR/$OLD_FS.sec' 2>&1; then
        echo 'ERROR: instsecattr -a failed'
        exit 1
    fi
    rm -f '$SAVEDIR/$OLD_FS.sec'
fi" >> $UNPRE_SCRIPT

        fi  # END of unpre_u processing
    fi  # END of .sec file processing
    ######################################
    ######################################
    ##
    ## Remove the files from the disk
    ##
    ######################################
    ######################################
    if [[ -n "$LPP_ID" && \
          -f "$OLD_IL" ]]
    then


        #==================================================
        # Append a number to the saved filename to avoid
        # collisions. For example, in the case where two
        # identically named files are being removed from
        # different directories!
        #==================================================
        typeset -i UNIQUE=0

        for FILE in $SYM_FILE $OLD_FILES ; do
        FILE=$FILE
            if [[ -f "$FILE" || -L "$FILE" ]]; then
                typeset BASENAME=${FILE##*/}
                                typeset BASEPATH=${FILE%/*}

                (( UNIQUE += 1 ))
                cp -pf "$FILE" "$SAVEDIR/$BASENAME.$UNIQUE" 2>/dev/null
                rm -f "$FILE"

                ###############
                #
                #  UNPRE_U
                #
                ###############
                print "
if [ -f '$SAVEDIR/$BASENAME.$UNIQUE' ]; then
    echo 'Restoring $FILE...'
    mkdir -p '$BASEPATH'
        if ! cp -pf '$SAVEDIR/$BASENAME.$UNIQUE' '$FILE'; then
            echo 'ERROR: Failed to copy backup file'
            exit 1
        fi
        rm -f '$SAVEDIR/$BASENAME.$UNIQUE'
        if [ ! -f '$FILE' ]; then
            echo 'ERROR: unable to restore $FILE.'
            exit 1
        fi
fi
" >> $UNPRE_SCRIPT

            fi # END of loop if file exists
        done # END of loop to process each file

        for DIRECTORY in $OLD_DIRS ; do
            if [[ -d "$DIRECTORY" ]]; then
                DIRECTORY=$DIRECTORY
                rmdir "$DIRECTORY"
            fi
        done

    fi #  END of loop to remove files from disk

    if ! /usr/bin/chmod +x $UNPRE_SCRIPT; then
        echo "ERROR: Failed to make $UNPRE_SCRIPT executable"
        exit 1
    fi

} # End of "remove_old_files()"

# ########################
# MAIN
# ########################
#
#
#

remove_old_files
POSTGRES13="/usr/ios/db/postgres13"

 rm -rf $SAVEDIR
 rm -rf ios.viodb13.rte.unpre_u
if [[ -d "$POSTGRES13" ]]; then
   rm -rf $POSTGRES13
fi
exit 0

