#!/bin/bash

# concept:
#
# simple, single-purpose script that checks the specified path (local
# filesystem or rsync) and notifies the user which packages (if any)
# may need to be upgraded.
#
# designed with 'slackware-$VERSION/patches/packages/' in mind.
#
# minimal sanity checking is performed. (attempts to outsmart this
# script will succeed - use your head)
#
# currently, only 'different' packages are shown, not 'newer'

# copyright (c) 2004 Erik Jan Tromp
#
# This program 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.
#
# This program 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 this program; if not, write to:
#
#	Free Software Foundation, Inc.
#	59 Temple Place - Suite 330
#	Boston, MA  02111-1307, USA

if [ $# -eq 0 ] ; then
  echo "Usage: $(basename $0) {source}"
  echo "       where {source} is the full path to the desired packages."
  echo "       (can be either rsync or local filesystem)"
  exit 0
fi

# assign name/version/architecture/build from full name
assign_nvab() {
  if [ $# -lt 4 ] ; then
    # less than 4 segments = old style or out of spec
    # deliberately avoiding '$*' & '$@' forms for now
    NAME=$1
    while [ $# -gt 1 ] ; do
      shift
      NAME=$NAME"-"$1
    done
    VERS=""
    ARCH=""
    BUILD=""
  else
    # 4 or more segments = new style
    NAME=$1
    while [ $# -gt 4 ] ; do
      shift
      NAME=$NAME"-"$1
    done
    VERS=$2
    ARCH=$3
    BUILD=$4
  fi
}

# parse full name into name/version/architecture/build
parse_nvab() {
  NVAB=$(basename $1 .tgz)
  NVAB=${NVAB//-/ }
  assign_nvab $NVAB
}

# force trailing slash (necessary for rsync, harmless for ls)
SOURCE=${1/%\//}/

# mirror access method ('rsync' for rsync, 'ls -l' for local filesystem)
if [ -d "$SOURCE" ] ; then
  COMMAND="ls -l"
else
  COMMAND="rsync"
fi

$COMMAND $1 | grep ^-.*tgz$ | \
while { read F1 F2 F3 F4 F5 F6 F7 F8 F9 ; } ; do
  if [ -z "$F6" ] ; then

# -rw-r--r--     3632918 2004/06/26 15:51:23 gaim-0.79-i486-1.tgz

    # 'rsync'
    REMOTE_SIZE=$F2 # unused (for now)
    REMOTE_PACKAGE=$F5
  else

# -rw-r--r--  1 erik users  3632918 Jun 26 15:51 gaim-0.79-i486-1.tgz
# -rw-r--r--  1 erik users  3632918 2004-06-26 15:51 gaim-0.79-i486-1.tgz

    # 'ls -l'
    REMOTE_SIZE=$F5 # unused (for now)
    if [ -z "$F9" ] ; then
      REMOTE_PACKAGE=$F8
    else
      REMOTE_PACKAGE=$F9
    fi
  fi

  parse_nvab $REMOTE_PACKAGE
  REMOTE_NAME=$NAME
  REMOTE_VERS=$VERS
  REMOTE_ARCH=$ARCH
  REMOTE_BUILD=$BUILD

  VARLOGPACKAGES=$(ls /var/log/packages/ | grep ^$REMOTE_NAME)
  if [ ! -z "$VARLOGPACKAGES" ] ; then
    for LOCAL_PACKAGE in $VARLOGPACKAGES ; do
      parse_nvab $LOCAL_PACKAGE
      LOCAL_NAME=$NAME
      LOCAL_VERS=$VERS
      LOCAL_ARCH=$ARCH
      LOCAL_BUILD=$BUILD

      # TODO: parse *_VERS & *_BUILD to test 'newer', not just 'different'

      if [ $LOCAL_NAME == $REMOTE_NAME ] \
      && ( [ $LOCAL_VERS != $REMOTE_VERS ] \
      || [ $LOCAL_ARCH != $REMOTE_ARCH ] \
      || [ $LOCAL_BUILD != $REMOTE_BUILD ] ) ; then
        echo -e "$LOCAL_PACKAGE\t>\t$REMOTE_PACKAGE"
      fi
    done
  fi
done
