#!/bin/bash 
#
# This simple script provides Slacky.it 
# latest posts tracking and lets you see
# them on your desktop using torsmo system
# monitor.
# Please add following string into your /tmp/.torsmorc
# in order to recall the script (1800 means 18 
# seconds delay):
# 
# ${execi 1800 /usr/bin/slackypst
#
# Author: Andrioli Lorenzo aka AsPh4lTo
#         albatros_la@hotmail.com
#
# REQUIREMENTS: Lynx, (Torsmo)
# Thanks to those guys who have developed Torsmo & Lynx!

#help
if [[ -z $1 ]]
then
	echo "
	Usage: slackypst <last posts number> <last packages number>
	Example: slackypst 10 5 
       	 	prints out the last 10 posts and the last 5 packages
	"
	exit
fi

#define variables
file=/tmp/slacky.tmp
file2=/tmp/slacky2.tmp
location="http://www.slacky.eu"

#download home page
lynx -accept_all_cookies -dump $location > $file
cp $file $file2

####Last posts section begins####
begin=`cat -n $file | grep "Ultimi Topics" | sed 's/^[ \t]*//' | cut -c-3`
end=`cat -n $file | grep "References" | sed 's/^[ \t]*//' | cut -c-3`
end=`expr $end - 1`
begin=`expr $end - $begin - 2`


#Catch the right part of the file
head -n $end $file > /tmp/tmp.1
tail -n $begin /tmp/tmp.1 > $file

#Manipulates text files
sed 's/^[ \t]*//g' $file > /tmp/tmp.1 #left justify
cut -d ']' -f2- /tmp/tmp.1 > $file #cut stupid characters!
sed '/^$/d' $file > /tmp/tmp.1 #delete blank lines

#Print output
echo LAST POSTS:
cat /tmp/tmp.1 | head -n$1 

####Last posts section ends####

####Last downloads section begins####
begin=`cat -n $file2 | grep "Packages" | sed 's/^[ \t]*//' | cut -c-3`
end=`cat -n $file2 | grep "FREE Joomla" | sed 's/^[ \t]*//' | cut -c-3`
end=`expr $end - 1`
begin=`expr $end - $begin - 1`

#Catch the right part of the file
head -n $end $file2 > /tmp/tmp.2
tail -n $begin /tmp/tmp.2 > $file2

#Manipulates text files & Print output
sed 's/^[ \t]*//g' $file2 > /tmp/tmp.2 #left justify
cut -d ']' -f2- /tmp/tmp.2 > $file2 #cut stupid characters!
sed '/^$/d' $file2 > /tmp/tmp.2 #delete blank lines

echo "
LAST PACKAGES:"
cat /tmp/tmp.2 | head -n$2

####Last downloads section ends####

rm -rf /tmp/tmp.1 /tmp/tmp.2 $file $file2 #remove temporary files
