#!/bin/bash
# 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.
#
# Jordan Bradley (phrag) phrags at gmail dot com 
# 1.0 - 25/09/04
# 1.1 - 19/04/07
# slackmon - a bash script to rsync slackware-current from a remote mirror to a local machine
VERSION="1.1"

if [ -e /etc/slackmon.conf ]; then
	. /etc/slackmon.conf
else	
	echo 'MIRROR="rsync.osuosl.org" LPATH="/slackware" \
	RPATH="slackware/slackware-current" EXCLUDE=""' | tr ' ' '\n' > /etc/slackmon.conf
        . /etc/slackmon.conf
	echo 'This is your /etc/slackmon.conf'
	cat /etc/slackmon.conf
	echo 'Ensure this is correct and edit as necessary before proceeding'
fi

echo 'slackmon-'$VERSION' by Jordan Bradley [phrags@gmail.com]'
echo 'Please Select An Option Number:'

# Menu
OPTIONS="check view rsync help quit"
select opt in $OPTIONS; do

# Check slackware-current changelog for changes
	if [ "$opt" = "check" ]; then
		
		echo 'Checks for changes to the slackware-current changelog.'
		echo 'Downloading ChangeLog.txt from '$MIRROR'...'
					
		if [ -e /tmp/ChangeLog.txt ]; then
	
			cp /tmp/ChangeLog.txt /tmp/ChangeLog.txt.old
			rsync -aPvz --delete $MIRROR::$RPATH/ChangeLog.txt /tmp/ChangeLog.txt \
			&& echo Download Complete || echo Download from "$MIRROR" Failed
			diff /tmp/ChangeLog.txt /tmp/ChangeLog.txt.old
		else
			rsync -aPvz --delete $MIRROR::$RPATH/ChangeLog.txt /tmp/ChangeLog.txt \
			&& echo Download Complete && less /tmp/ChangeLog.txt \
			|| echo Download from "$MIRROR" Failed	
		fi

# View slackware-current changelog
	elif [ "$opt" = "view" ]; then
		less /tmp/ChangeLog.txt

# Rsync slackware-current
	elif [ "$opt" = "rsync" ]; then 
		
		echo This will create a local slackware-current mirror in $LPATH

		if [ $UID == 0 ]; then

			echo $EXCLUDE | tr ' ' '\n' > /tmp/current-rsync.exclude
			mkdir -p "$LPATH/slackware-current"
			cd "$LPATH"/slackware-current
			rsync -aPvz --delete --exclude-from=/tmp/current-rsync.exclude "$MIRROR"::"$RPATH" "$LPATH" \
			&& echo Got slackware-current OK || echo Download from "$MIRROR" Failed
		else
			echo Only available to root!
		fi
# Help
	elif [ "$opt" = "help" ]; then
		echo
		echo Configuration file  			:: /etc/slackmon.conf ::
		echo Slackware-current mirror 	 		:: $MIRROR ::
		echo Slackware-current local rsync path  	:: $LPATH ::
		echo Slackware-current remote rsync path  	:: $RPATH ::
		echo Exclude from rsync  			:: $EXCLUDE ::
		echo
		echo check - Checks for changes to the slackware-current changelog
		echo view  - Views slackware-current changelog
		echo rsync - Creates a local slackware-current mirror in $LPATH
		echo help  - Displays this dialog
		echo
# Quit
	elif	[ "$opt" = "quit" ]; then
		echo
		echo 'WARNING: Ensure you read and follow UPGRADE.TXT and CHANGES_AND_HINTS.TXT *before* upgrading!'
		echo 'slackmon-'$VERSION' by Jordan Bradley [phrags@gmail.com]'
		exit

	else
		echo Invalid Option. Please select an option number from the menu. Hit enter to display the menu again.

	fi
done


