#!/bin/sh

# sync_slackbuilds.sh
# 2013-11-02
# by Gernot Walzl

# Synchronizes SlackBuilds via HTTP.
# It downloads locally existent slackbuilds only and checks for modifications.


SLACKBUILDS_DIR="/root/slackbuilds"
REPO_URL="http://gernot-walzl.at/Slackware/SlackBuilds/"


# change to slackbuilds dir
cd "$SLACKBUILDS_DIR" || exit 1

# download slackbuilds
FILES=$(find . -type f -name '*.SlackBuild' | sort)
for FILE in $FILES; do
  wget -nv -O "${FILE}.new" "${REPO_URL}${FILE}"
done

# check for updates
echo ""
echo "The following SlackBuilds have been updated:"
for FILE in $FILES; do
  if [ -s "${FILE}.new" ]; then
    if [ $(diff "${FILE}" "${FILE}.new" | wc -l) -gt 0 ]; then
      cp "${FILE}" "${FILE}.old"
      cat "${FILE}.new" > "${FILE}"
      echo "${FILE}"
    fi
  fi
  rm "${FILE}.new"
done
echo ""