#!/bin/bash
# Copyright 2008  Patrick Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

# This is a script to track packages of your choice from SBo (slackbuilds.org).
# It works by comparing the timestamps of the online packages to the time the
# package was last installed on your machine.  This is not a 100% accurate
# approach, if say, someone builds/installs using an old build script when a
# better one is already on slackbuilds.org.  It will catch up quickly in most
# cases, though.  :-)

# Track version:
TRACK_VERSION=12.0

# Enter the packages that you want to track here.  They'll be placed in a
# secured directory in /tmp.  You should be able to do a fairly long
# list if you like without running into the command-line limit.
SECURE_DIRECTORY=$(mktemp -d)
lftp -e "ls -l ; exit" http://slackbuilds.org:/ports/${TRACK_VERSION}/ 1> $SECURE_DIRECTORY/list 2> /dev/null
( cd $SECURE_DIRECTORY
  for package in xmms.tar.gz xfce4-xmms-plugin.tar.gz ; do
    FILETIME=$(grep ${package} $SECURE_DIRECTORY/list | grep -v "asc$" | cut -b 26-41)
    touch -d "$FILETIME" $SECURE_DIRECTORY/test-remote-file
    LOCAL_FILE=$(ls /var/log/packages/$(basename ${package} .tar.gz)-*-*-* 2> /dev/null | tail -n 1)
    if [ ! -z "$LOCAL_FILE" ]; then
      touch -r $LOCAL_FILE $SECURE_DIRECTORY/test-local-install
      if [ $SECURE_DIRECTORY/test-remote-file -nt $SECURE_DIRECTORY/test-local-install ]; then
        SOMETHING_WAS_DOWNLOADED="true"
        echo "${package} is newer than installation time.  Fetching..."
        lftpget http://slackbuilds.org:/ports/${TRACK_VERSION}/${package}
        lftpget http://slackbuilds.org:/ports/${TRACK_VERSION}/${package}.asc
      fi
    fi
  done
  rm -f list test-remote-file test-local-install
  if [ "$SOMETHING_WAS_DOWNLOADED" = "true" ]; then
    echo "New script archives may be found in $SECURE_DIRECTORY"
  else
    rm -r $SECURE_DIRECTORY
  fi
)
