#!/bin/sh
# written by David Price <mantys@goldweb.com.au>
# freshmeat, segfault and linuxtoday readers
# Michael Lucas-Smith <ich@driftwood.darktech.org>

# this was designed to be used with root-portal 
# this script is under the GPL

# this script requires wget (as well as a few other things.  but wget is the
# thing people are most likely not to have)
# note that this script saves a time stamp.  it will not reload from
# news site unless the time-stamp is at least an hour old

# first parameter should be the news site to fetch
# supported are:
#     slashdot, freshmeat, segfault

# to reset so that current news is output delete
# ~/.root-portal/temp/*.old

TEMP_DIR=${HOME}/.root-portal/temp

# read options
DISPLAY_URLS=N
ALL_NEWS=N
NEWS=""

while [ $# -gt 0 ]; do
    if [ "$1" == "-u" -o "$1" == "--urls" ]; then
	DISPLAY_URLS=Y
    elif [ "$1" == "--help" -o $1 == "-h" ]; then
	echo "Usage: readnews.sh [options...] {slashdot|freshmeat|segfault|linuxtoday}"
	echo "  -h, --help        display this help message"
	echo "  -u, --urls        display urls in addition to headlines"
	echo "  --old             display all news not just new stuff"
	exit
    elif [ "$1" == "--old" ]; then
	ALL_NEWS=Y
    else
	NEWS=$1
    fi
    shift
done

ROOT_PORTAL="${HOME}/.root-portal/temp"

if test ! -d "${ROOT_PORTAL}"; then
	mkdir "${ROOT_PORTAL}"
fi

cd "${TEMP_DIR}"

if [ ! -f "${ROOT_PORTAL}/${NEWS}.headlines.old" ]; then
	ALL_NEWS=Y
fi
if [ "$ALL_NEWS" == "Y" ]; then
	echo -n > "${ROOT_PORTAL}/${NEWS}.headlines.old"
fi

# if this was last locked less than half an hour ago, return false
time_lock() {
	# some sites ask not to be reloaded more often than every half hour
	# others ask for an hour.  so we go with the safe bet of an hour
	if test ! -f "${TEMP_DIR}/${NEWS}_read.timestamp"
	then
		return 0
	fi
	hour_ago="$(date '+%Y%j%H%M' --date '60 minute ago')"
	more_recent=$(echo -e "$(cat ${TEMP_DIR}/${NEWS}_read.timestamp)\n${hour_ago}" | sort | tail -n 1)
	if test ! -f "${TEMP_DIR}/${FILE}"
	then
		more_recent="${hour_ago}"
	fi
	if test "${more_recent}" = "${hour_ago}"
	then
		return 0
	else
		return 1
	fi
}

# set the time lock to now
set_lock() {
	chmod a+rw "${FILE}"
	date '+%Y%j%H%M' > "${TEMP_DIR}/${NEWS}_read.timestamp"
	chmod a+rw "${TEMP_DIR}/${NEWS}_read.timestamp"
}

# download the file
get_file() {
	if time_lock
	then
		mv -f "${FILE}" "${FILE}.old" > /dev/null 2>&1
		if which wget >/dev/null 2>&1; then
			wget -q "${NEWS_URL}"
		else
			lynx -source "${NEWS_URL}" > "${FILE}"
		fi
		if test ! -f "${TEMP_DIR}/${FILE}"
		then
			mv -f "${FILE}.old" "${FILE}" > /dev/null
		fi
		set_lock
		return 0
	fi
	return 1
}

case $NEWS in
	slashdot)
		NEWS_URL="http://slashdot.org/slashdot.rdf"
		;;
	freshmeat)
		NEWS_URL="http://freshmeat.net/backend/recentnews.txt"
		;;
	segfault)
		NEWS_URL="http://segfault.org/stories.txt"
		;;
	linuxtoday)
		NEWS_URL="http://linuxtoday.com/lthead.txt"
		;;
	*)
		echo "unknown news site \"$NEWS\".  run with --help for help"
		exit
		;;
esac

FILE="${NEWS_URL##*/}"
get_file

case $NEWS in
	slashdot)
		sed -e '/<title>/b' -e '/<link>/b' -e d "${FILE}" | sed '1,4d' | tac | sed -n '1~2h;1~2!G;1~2!p' > "${ROOT_PORTAL}/${NEWS}.headlines.new"
		diff "${ROOT_PORTAL}/${NEWS}.headlines.old" "${ROOT_PORTAL}/${NEWS}.headlines.new" | grep '^>' | sed -n -e '/<title>/p' -e '/<link>/p' -e d | sed -e 's/<\/.*>//g' | sed 's/^>.*<.*>//g' > "${ROOT_PORTAL}/${NEWS}.out"
		;;
	freshmeat)
		sed '2~3d' "${FILE}" | tac | sed -n '1~2h;1~2!G;1~2!p' > "${ROOT_PORTAL}/${NEWS}.headlines.new"
		diff "${ROOT_PORTAL}/${NEWS}.headlines.old" "${ROOT_PORTAL}/${NEWS}.headlines.new" | sed -n '/>/p' | sed 's/> //' > "${ROOT_PORTAL}/${NEWS}.out"
		;;
	segfault)
		sed '1,11d' stories.txt | awk 'BEGIN{a=0}{if(a==7){a=0}if(a==0){print}if(a==1){print}a++;}' | tac | sed -n '1~2h;1~2!G;1~2!p' > "${ROOT_PORTAL}/${NEWS}.headlines.new"
		diff "${ROOT_PORTAL}/${NEWS}.headlines.old" "${ROOT_PORTAL}/${NEWS}.headlines.new" | sed -n '/>/p' | sed 's/> //' > "${ROOT_PORTAL}/${NEWS}.out"
		;;
	linuxtoday)
		sed '1,5d' lthead.txt | awk 'BEGIN{a=0}{if(a==4){a=0}if(a==0){print}if(a==1){print}a++;}' | tac | sed -n '1~2h;1~2!G;1~2!p' > "${ROOT_PORTAL}/${NEWS}.headlines.new"
		diff "${ROOT_PORTAL}/${NEWS}.headlines.old" "${ROOT_PORTAL}/${NEWS}.headlines.new" | sed -n '/>/p' | sed 's/> //' > "${ROOT_PORTAL}/${NEWS}.out"
		;;
esac

if [ "${DISPLAY_URLS}" = "Y" ]; then
	cat "${ROOT_PORTAL}/${NEWS}.out"
else
	cat "${ROOT_PORTAL}/${NEWS}.out" | sed -n '1~2p'
fi

cp "${ROOT_PORTAL}/${NEWS}.headlines.new" "${ROOT_PORTAL}/${NEWS}.headlines.old"

