#!/bin/bash
# CreateLocalFilesUpdated: for SyncDirs10.0 testing, create copies of local.files.<dir> files, as they would appear
# after updates have been applied, and obsolete files have been deleted.
# Version 1.0

# Copyright (C) 2006 Free Software Foundation, Inc.
# Author: Geoff Farrell

# This file is part of SyncDirs10.0.

# SyncDirs10.0 is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

# SyncDirs10.0 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

# You should have received a copy of the GNU General Public License along with SyncDirs10.0; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

# Required files:
#       SyncDirs10.0
#       ExportRpms
#       CreateLocalFilesUpdated (for testing only)
#       SyncDirs10.0-Test (for testing only)

#**--------------------------------------------------------------------------------------------------------------
#** This script enables testing for correct treatment of the remote backups directory, while not actually downloading files
#** from the ftp site. Need to create files that represent what the local updates directory would contain after the
#** updates were applied, then input these files to Section 8 of SyncDirs10.0 ('synchronise remote backups').
#** The updated local files are needed in two stages:
#**     1. After the updates have been downloaded to the local updates directory, and
#**     2. After the obsolete files have been deleted from the local updates directory.
#** In Stage 1, files in ftp.fil.<dir> need to be added to local.files.<dir>, with the result left in local.files3.<dir>.
#** In Stage 2, the files in local.remove.<dir> need to be removed from local.files3.<dir>, with the result placed in local.files4.<dir>
#**
#** The files are created as local.files3.delta and local.files4.delta (etc) which are not overwritten by SyncDirs10.0, and whose
#** 'sync remote backups' sections are pointed, in Testing, to local.files4.<dir> rather than to local.files.<dir>.
#** This script creates the required local.files3.<dir> and local.files4.<dir> files.
#**
#** Method: Calculate two different versions of local.files.<dir> to suit these two different stages.
#** Stage 1: The local files remaining after an update has occurred will be equal to:
#**             local.files.<dir> + ftp.fil.<dir>  (output saved in local.files3.<dir>)
#**     Adding ftp.fil.<dir> files can be achieved by using the 'cat' command.
#** Stage 2: The local files remaining after the obsolete files have been deleted will be equal to:
#**             local.files3.<dir> - local.remove.<dir>  (output saved in local.files4.<dir>)
#**     This calculation can be achieved by 'comm -23 local.files3.<dir> local.remove.<dir>'
#**             (ie, suppress files unique to file2, and common to both. This leaves files unique to local.files3.<dir>)
#**
#** This script takes a command line argument of '1' or '2' to pick which stage to execute
#**
#** Note: the 'local.files3.<dir>' files are an internal mechanism to this script. Creating these is an intermediate step
#** to creating the 'local.files4.<dir>' files, which are used in both SyncDirs10.0 and SyncDirs10.0-Test.
#**--------------------------------------------------------------------------------------------------------------

# This first declaration is the only one needing user customisation
# It needs to match the declaration in SyncDirs10.0 applicable to Testing mode.
WK_DIR=/tmp/SyncDirs                                            # Working Directory where all files are saved

# The remaining declarations should not need changing
LOCAL_FILES=$WK_DIR/local.files         # Stem name for files holding lists of local updates directories
LOCAL_FILES3=$WK_DIR/local.files3       # Ditto to $LOCAL_FILES, but represents the file list after an update has occurred
LOCAL_FILES4=$WK_DIR/local.files4       # Ditto to $LOCAL_FILES3, but represents the file list after obsolete files have been removed
LOCAL_FILES3_TMP=$LOCAL_FILES3.tmp      # Temp file for $LOCAL_FILES3
LOCAL_FILES4_TMP=$LOCAL_FILES4.tmp      # Temp file for $LOCAL_FILES4
FTP_FIL=$WK_DIR/ftp.fil                                 # Stem name for holding lists of files to be downloaded from the ftp site
LOCAL_REMOVE=$WK_DIR/local.remove       # Stem name for holding lists of obsolete files to be removed from the local updates directories

IDX_DELTAS=deltas                                                       # Name of Index file from wget for the 'deltas' sub-directory
IDX_PATCHES=patches                                             # Name of Index file from wget for the 'patches' sub-directory
IDX_PATCHES_OBS=patches.obs                     # Name of Index file from wget for the 'patches.obsolete' sub-directory
IDX_i586=i586                                                           # Name of Index file from wget for the 'i586' sub-directory
IDX_NOARCH=noarch                                                       # Name of Index file from wget for the 'noarch' sub-directory
IDX_SCRIPTS=scripts                                             # Name of Index file from wget for the 'scripts' sub-directory

# Declare an array to hold wget Index sub-directory names
declare -a IdxDir
# Load the directory names into the array
IdxDir[0]=$IDX_DELTAS
IdxDir[1]=$IDX_PATCHES
IdxDir[2]=$IDX_PATCHES_OBS
IdxDir[3]=$IDX_i586
IdxDir[4]=$IDX_NOARCH
IdxDir[5]=$IDX_SCRIPTS

# Get the stage specified on the command line
Stage=$1                                        # Stage 1 or 2

cd $WK_DIR                              # Change directory to the working directory. SyncDirs10.0 will expect to find these files there.

# Scan through all $LOCAL_FILES.<dir> files and create the required $LOCAL_FILES3.<dir> and $LOCAL_FILES4.<dir> files
for Dir in "${IdxDir[@]}"; do                                                   # Create the $LOCAL_FILES3.<dir> files
       case "$Stage" in                                                                                # Check which stage was called
       1)                                                              # Stage 1 actions
               # Transfer the files listed in $LOCAL_FILES.<dir> to the temp file
               cat $LOCAL_FILES.$Dir $FTP_FIL.$Dir > $LOCAL_FILES3_TMP.$Dir

               # Sort the result and transfer it to $LOCAL_FILES3.<dir> - a sorted list is needed for SyncDirs10.0
               cat $LOCAL_FILES3_TMP.$Dir | sort > $LOCAL_FILES3.$Dir
               ;;
       2)                                                              # Stage 2 actions
               if [ -e "$LOCAL_FILES3.$Dir" ]; then            # Make sure that $LOCAL_FILES3.<dir> exists
                       # Remove the files in $LOCAL_REMOVE.<dir> from the $LOCAL_FILES3.<dir> listing, & store the result in the temp file
                       comm -23 $LOCAL_FILES3.$Dir $LOCAL_REMOVE.$Dir > $LOCAL_FILES4_TMP.$Dir
                       # Sort the result and transfer it to $LOCAL_FILES4.<dir>
                       cat $LOCAL_FILES4_TMP.$Dir | sort > $LOCAL_FILES4.$Dir
               else                                                                                                    # If $LOCAL_FILES3.<dir> is missing, announce it
                       echo "Error in CreateLocalFilesUpdated: $LOCAL_FILES3.$Dir is missing in Stage 2"
               fi
               ;;
       *)                                                                                                      # Must have had bad argument passed
               echo "Error in CreateLocalFilesUpdated: Bad Argument passed."
               ;;
       esac
done