#!/bin/bash
# ExportRpms - get a listing of installed rpms and export the file to a remote host.
# 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 is called by SyncDirs10.0 on the computer retrieving Suse updates to trigger other
# computers on the network to report their installed rpms, for use in compiling lists of what needs to be downloaded.
# In the context of this script, 'remote host' refers to the main computer retrieving the Suse updates;
# 'local host' refers to any of the other computers on the network on which this script is run, to report their
# installed rpms to the main computer.

# This script should be stored as /usr/bin/ExportRpms. This name is used by the SyncDirs10.0 script.

# The nominated directory $REMOTE_DIR should be a local SAMBA or NFS import. It should access the directory ($STORE_DIR) on
# the main computer that SyncDirs10.0 uses to collate the lists of installed rpms of the various network computers.
# Eg, /mnt/fred_common ($REMOTE_DIR) is a local SAMBA share of a folder /home/fred/Common ($STORE_DIR) on the remote host 'local'.
#**************************************************************************************************************************************

# Build list of installed rpms, put in $INSTALLED_RPMS file.
# Method:
# 1. Build a list of installed files from the rpm database, with 'rpm -qa'.
# 2. Filter with 'sed' to produce names in the format 'java-1_4_2-sun-'.
#               The version, release, architecture and extension (eg, '1.4.2.10-2.1.i586.rpm') are omitted deliberately
# 3. Sort the list, then place these filenames in $LOCAL_INSTALLED_RPMS, one per line.
# 4. Copy the list to $REMOTE_INSTALLED_RPMS, where it can be accessed by the main computer.

# The first two declarations need to be set to suit each local host's environment
LOCAL_HOST=remhost1                             # The name of the local host
REMOTE_DIR=/mnt/fred_common     # The local mount point for the directory on the remote host where SyncDirs10.0 will access this host's
#                                                                                       installed.rpms file

LOCAL_DIR=/tmp                                          # This could be any directory for which the nominated user has read-write permissions
# The next declaration must agree with the corresponding one set in SyncDirs10.0 - best not to change it
INSTALLED_RPMS=installed.rpms
LOCAL_INSTALLED_RPMS=$LOCAL_DIR/$INSTALLED_RPMS
LOCAL_INSTALLED_RPMS_TMP=$LOCAL_INSTALLED_RPMS.tmp
REMOTE_INSTALLED_RPMS=$REMOTE_DIR/$LOCAL_HOST.$INSTALLED_RPMS

# Get a listing of installed rpms, minus version numbers, from the local rpm database
rpm -qa | sed -e 's/-[a-zA-Z0-9\._]*-[a-zA-Z0-9\._]*$/-/' > $LOCAL_INSTALLED_RPMS_TMP

# Sort the list and save in $LOCAL_INSTALLED_RPMS (use local files to keep a local copy)
cat $LOCAL_INSTALLED_RPMS_TMP | sort > $LOCAL_INSTALLED_RPMS
# Then copy the result to the remote host
cp -p $LOCAL_INSTALLED_RPMS $REMOTE_INSTALLED_RPMS