#!/bin/bash
#########################################################################################
#
# Add new jobs.
#
#
# © Copyright 2012 Arto Jääskeläinen <temp001(at)pp.inet.fi>
# Part of Auto DL software package. All rights reserved.
# The program is distributed under the terms of the GNU General Public License
#
#    Auto DL 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 3 of the License, or
#    (at your option) any later version.
#
#    Auto DL 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.
#
#    You should have received a copy of the GNU General Public License
#    along with Auto DL.  If not, see <http://www.gnu.org/licenses/>.
#
#
# V1.0	2012-07-27
# V1.01	2012-09-14	Loadable conversion
# V1.1  2012-09-27  Configuration file support, improved logging
# V1.11 2012-09-28  Fix system conf variable
# V1.12 2012-09-28  Minor logging change
# V1.13 2012-10-09  Validate_link & close_all fix, init removes temp files
# V1.14 2012-10-16  Improved titles handling |:
#
#########################################################################################
inst_dir=~/.auto_dl

get_conf()
{
path_file="$1"
conf_par="$2"
if [ -f "$path_file" ] && [ -n "$conf_par" ]; then
	line=$(cat "$path_file" | sed 's/^#.*//g' | grep -wi "$conf_par")
	if [ -n "$line" ]; then
	response=$(echo ${line##*=})
	else
	response=""
	fi
else
	return 1 
fi
}

get_user_conf()
{
user_conf_path="/home/$username/.auto_dl/autodl.conf"	
if [ "$1" = "" ]; then
return 1
fi
get_conf "$user_conf_path" "$1"
if [ $? = "0" ] && [ -n "$response" ]; then 
conf=$response
else
conf=""
return 1
fi
}

get_system_conf()
{
system_conf_path="/etc/auto_dl/autodl.conf"
if [ "$1" = "" ]; then
return 1
fi
get_conf "$system_conf_path" "$1"
if [ $? = "0" ] && [ -n "$response" ]; then 
conf=$response
else
conf=""
return 1
fi
}

define_setting()
{
var_name="$1"	
default="$2"	
get_user_conf "$var_name"
if [ $? = "0" ]; then
setting=$conf
echo "$(date "+%F %T %z") User defined $var_name=$setting " | tee -a $logs_dir/$log_file
else
        get_system_conf "$var_name"
        if [ $? = "0" ]; then
        setting=$conf 
        echo "$(date "+%F %T %z") System defined $var_name=$setting " | tee -a $logs_dir/$log_file
        else
        setting="$default"
        echo "$(date "+%F %T %z") Default $var_name=$setting " | tee -a $logs_dir/$log_file
        fi
fi
}

check_match()
{
matches=0	
st_1="$1"
shift
st_array=($*)
st_elems=${#st_array[*]}
for (( i=0; i<$st_elems; i++ ))
do
	if [ "${st_array[$i]}" != "${st_array[$i]/$st_1/}" ] ; then 
	(( matches++ ))
	fi
done
}

check_maintenance()
{
maint_words=(huolto katko huolletaan häiriö)
matching_words=0
info=$(cat "$1" | sed '/\<STYLE/,/\/STYLE\>/d; s/<[^>]*>//g' | tr -s '\n')
for word in ${maint_words[*]}
do
	text=${info,,}
	check_match "$word" "$text"
	if [ "$matches" != "0" ]; then
	((matching_words=$matching_words+$matches))
	fi
	
done
if [ "$matching_words" != "0" ]; then
echo "$(date "+%F %T %z") Maintenance break detected: $f_link" | tee -a $data_root/logs/$log_file 	
echo "$(date "+%F %T %z") ${info:0:80}" | tee -a $data_root/logs/$log_file
echo -e "Message:\n"$info
else
echo "$(date "+%F %T %z") Invalid feed file: $f_link" | tee -a $data_root/logs/$log_file
close_all 68
fi
}

get_category()
{
category=$(cat "$1" | grep -i "category" | cut -d $'\n' -f1 | sed 's/ *<[^>]*>//g')
}

get_guid()
{
guid=$(cat "$1.tsv" | awk "/$2/" | cut -f3 | cut -d '=' -f2)
}

get_medium_title()
{
local par="$1"
local pipe_tail="$(echo ${par##*|})"	
medium_title="$(echo $(echo ${pipe_tail%%:*}) | sed 's/(.*)//g;s/[ \t]*$//')"
}

get_latest_feed_titles()
{
rm $work_dir/titles/* 2>/dev/null
saved_ifs="$IFS"
IFS=$'\n'
for line in $(cat $work_dir/"$1.tsv")
do
get_medium_title "$(echo "$line" | cut -f1 | cut -d '=' -f2)"
echo "$medium_title" | xargs -r -n 1 -I{} touch $work_dir/titles/{}
IFS="$saved_ifs"
done
}

. $inst_dir/xml2tsv-*

validate_link()
{
local f_link="$@"
local short_link	
if [ -z "$f_link" ] ; then
	echo
	echo "$(date "+%F %T %z") RSS job maker error: Parameter missing." | tee -a $data_root/logs/$log_file
	close_all 69 
else
	short_link=$(echo "$f_link" | cut -d'&' -f1)  
	if [ $(echo ${short_link:0:7}) == "http://" ] ; then
		domain=$(echo $short_link | cut -d'/' -f3)
	else
	echo
	echo "$(date "+%F %T %z") Not http link: $f_link" | tee -a $data_root/logs/$log_file
	close_all 70 
	fi
fi
host_resp=$(host "$(echo "$domain")")
ip_feed=$(echo "$host_resp" | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
if [ -z "$ip_feed" ] ; then
	echo
	echo "$(date "+%F %T %z") Domain" $domain":" $host_resp | tee -a $data_root/logs/$log_file
	close_all 71 
fi
}

validate_rss()
{
	
rss_tags=$(cat "$1" | grep -o -e "<rss" -e "/rss>" | tr -d '\r\n')
if [ -z "$rss_tags" ] ; then
echo
echo "$(date "+%F %T %z") Not RSS destination: $f_link" | tee -a $data_root/logs/$log_file
return 72
fi
}

close_all()
{
pass_error="$*"
echo
echo "Job maker closing..."
cd $saved_dir
if [ "${#work_dir}" -gt 15 ] && [ -d "$work_dir" ] ; then
rm -rf $work_dir/* 2>/dev/null  
rm -rf $data_root/.live_data >/dev/null
fi
echo -e "Bye.\n"
exit $pass_error
}

init()
{
mkdir -p $data_root
if [ ! -d $data_root ]; then
	echo "Failed to find data root." | tee -a $data_root/logs/$log_file 
	close_all 64  
fi 	
mkdir -p $logs_dir
touch $logs_dir/$log_file
if [ ! -f $logs_dir/$log_file ] ; then
	echo
	echo "$(date "+%F %T %z") Failed to find failure log file." | tee -a $logs_dir/$log_file
	close_all 65
fi
rm -rf $data_root/.live_data >/dev/null
mkdir -p $data_root/.live_data/$BASHPID
if [ ! -d $data_root/.live_data/$BASHPID ] ; then
	echo
	echo "$(date "+%F %T %z") Failed to find work directory." | tee -a $data_root/logs/$log_file
	close_all 66
fi
work_dir=$data_root/.live_data/$BASHPID
if [ "${#data_root}" -lt 16 ] || [ "${#work_dir}" -lt 29 ]; then
	echo "$(date "+%F %T %z") Creating working directory into" ~ "failed." | tee -a $data_root/logs/$log_file
	close_all 67
fi
mkdir -p $data_root/adl_jobs
if [ ! -d $data_root/adl_jobs ]; then
echo "$(date "+%F %T %z") Failed to find $data_root/adl_jobs" | tee -a $data_root/logs/$log_file
close_all 68
fi
mkdir -p $work_dir/titles
if [ ! -d $work_dir/titles ]; then
echo "$(date "+%F %T %z") Failed to find $work_dir/titles" | tee -a $data_root/logs/$log_file
close_all 69
fi
saved_dir=$PWD
cd $work_dir
trap close_all HUP INT USR1 USR2 TERM
}

get_feed_file()
{
echo "Receiving RSS feed..."
wget_resp=$(wget -nv -O$feed_file_name "$f_link" 2>&1)	
}

error_activity()    
{
filt_resp=$(echo $(echo $wget_resp | grep -o '\s[0-9]\{3\}[^0-9]'))	
case $filt_resp in   
		404: )	
			echo; echo "$(date "+%F %T %z") Http error, not found 404: $f_link" | tee -a $data_root/logs/$log_file; close_all 1 ;;
		500: )
			echo; echo "$(date "+%F %T %z") Http error, feed server internal error 500: $f_link" | tee -a $data_root/logs/$log_file; close_all 1 ;;		
		503: )
			echo; echo "$(date "+%F %T %z") Service not available 503: $f_link" | tee -a $data_root/logs/$log_file; close_all 1 ;;
		* )		
			echo; echo "$(date "+%F %T %z") Http error: $resp" | tee -a $data_root/logs/$log_file; close_all 1 ;;
esac
}

show_list()
{
box_title="Auto DL"
box_toptext="Nauhoitettavaksi asetettavien ohjelmien lisäys"	
list_to_show=("$@")	
if [ -n "$list_to_show" ]; then 
box_column="    Lajittele klikkamalla tähän"
choice_list=$(zenity --list --checklist --width=$box_width --height=$box_height --title "$box_title" --text "$box_toptext" --column "" --column "$box_column" "${list_to_show[@]}")	
else
zenity --info --text="Ei nauhoitettavaksi asetettuja ohjelmia" --title "Auto DL"
fi
}
	
data_root=~/.auto_dl
jobs_dir=$data_root/adl_jobs
logs_dir=$data_root/logs
log_file="new_job.log"
init
echo "$(date "+%F %T %z") Start complete." | tee -a $data_root/logs/$log_file
f_link="$@"
validate_link "$f_link"
echo "$(date "+%F %T %z") Feed link accepted." | tee -a $data_root/logs/$log_file
ttl=10
guid=0
category=''
feed_file_name="$(echo "$f_link" | sed 's/.*\///')" 
get_feed_file
es="$?"
if [ "$es" != "0" ] ; then 
error_activity  
fi
echo "$(date "+%F %T %z") Feed received." | tee -a $data_root/logs/$log_file
validate_rss "$feed_file_name"

if [ "$?" = "0" ]; then
		echo "$(date "+%F %T %z") RSS feed contents validation passed." | tee -a $data_root/logs/$log_file
		echo -n "Filtering file..."  
		cat "$feed_file_name" | tr -d '\r' > "$feed_file_name.fixed" 
		rm "$feed_file_name"
		mv "$feed_file_name.fixed" "$feed_file_name"
		echo " done."
		get_category "$feed_file_name" 
		echo "$(date "+%F %T %z") Category= $category." | tee -a $data_root/logs/$log_file
		echo -n "Converting to tsv..."
		xml2tsv $feed_file_name
		echo "$(date "+%F %T %z") Conversion complete." | tee -a $data_root/logs/$log_file
		echo -n "Getting feed titles..."
		get_latest_feed_titles $feed_file_name
		echo "$(date "+%F %T %z") Building user menu." | tee -a $data_root/logs/$log_file
		mkdir -p $work_dir/is_already
		i=0
		check_on="False"
		for pathfile in $work_dir/titles/* 
		do
			long_title="$(echo $pathfile | sed 's/.*\///')"
			get_medium_title "$long_title" 
			if [ ! -f "$work_dir/is_already/$medium_title" ]; then 
			my_list[i]=$check_on
			my_list[i+1]="$medium_title"
			((i=i+2))
			touch "$work_dir/is_already/$medium_title"
			fi
		done
		rm -rf $work_dir/is_already 2>/dev/null 
		username=$USER
		define_setting "WindowHeight" 600
		box_height=$setting
		define_setting "WindowWidth" 400
		box_width=$setting
		show_list "${my_list[@]}" 
		if [ -n "$choice_list" ]; then   
			jobname=$(echo $f_link | sed 's/\//|/g')  
			mkdir -p $jobs_dir/$jobname 
			echo $choice_list | sed 's/|/\n/g'| xargs -r -n 1 -I{} touch "$jobs_dir/$jobname/{}"
			echo "$(date "+%F %T %z") $(echo New: $choice_list | sed 's/|/, /g' )" | tee -a $data_root/logs/$log_file
		else
			echo "$(date "+%F %T %z") No new jobs added." | tee -a $data_root/logs/$log_file
		fi
else   
check_maintenance "$feed_file_name" 
fi
close_all
