#!/bin/bash
#########################################################################################
#
# Check setup.
#
#
# © 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-10-01
# V1.1  2012-10-04  RecordExitTask testing included
# V1.11 2013-01-04  MaxDownloadCount
#
#########################################################################################

bash_or_sh()
{
local pathfile="$@"
[ -z "$pathfile" ] && return 100 
[ -f "$pathfile" ] || return 101 
[ -x "$pathfile" ] || return 102 
header=$(dd if=$pathfile bs=12 count=1 2>&- | grep -E '#\!/bin/(bash|sh)$')
if [ -z "$header" ]; then
return 103
else
return 0
fi
}

fgred()
{
echo -en "\x1b[31;01m"
}

fgblue()
{
echo -en "\x1b[34;01m"
}

fgbold()
{
echo -en "\x1b[1m"
}

fgdefault()
{
tput sgr0
}

get_version_list()
{
version_list=($(grep -E -o '# *[Vv][0-9]{1,2}.[0-9]{1,2} ' "$1" | sed 's/# *[Vv]//g'))
}

compare()
{
local longer
local a=$1
local b=$2
if [ -z $a ] || [ -z $b ]; then return 1; fi
int_a=$(echo ${a%%.*})
fract_a=$(echo ${a##*.})
int_b=$(echo ${b%%.*})
fract_b=$(echo ${b##*.})
if [ $int_a -gt $int_b ]; then return 101; fi
if [ $int_a -lt $int_b ]; then return 102; fi
longer=${#fract_a}
if [ ${#fract_b} -gt ${#fract_a} ]; then longer=${#fract_b}; fi
while [ ${#fract_a} -lt $longer ]; do fract_a=$fract_a"0"; done
while [ ${#fract_b} -lt $longer ]; do fract_b=$fract_b"0"; done
if [ $fract_a -gt $fract_b ]; then return 101; fi
if [ $fract_a -lt $fract_b ]; then return 102; fi
return 100	
}

get_biggest()
{
local i
list=( "$@" )
i_biggest=""
n_list=${#list[@]}
if [ -z $list ] || [ $n_list -lt 2 ]; then biggest=""; return 1; fi
biggest=${list[0]}
i_biggest=0
for (( i=1; i<$n_list; i++ )) do
compare $biggest ${list[i]}
result="$?"
case $result in
102)	biggest=${list[i]}; i_biggest=$i ;;
100)	i_biggest="" ;;		
esac
done
}

get_smallest()
{
local i
list=( "$@" )
i_smallest=""
n_list=${#list[@]}
if [ -z $list ] || [ $n_list -lt 2 ]; then smallest=""; return 1; fi
smallest=${list[0]}
i_smallest=0
for (( i=1; i<$n_list; i++ )) do
compare $smallest ${list[i]}
result="$?"
case $result in
101)	smallest=${list[i]}; i_smallest=$i ;;
100)	i_smallest="" ;;		
esac
done
}

show_version()
{
pathfile="$1"	
file=$(echo ${pathfile##*/})	
if [ ! -f "$pathfile" ]; then 
fgred
eval echo -e "'\t' $2 '\t' Not found" $pipe_to_log 
fgdefault
return 1
fi
get_version_list "$pathfile"
if [ -z $version_list ]; then
fgred
eval echo -e "'\t' $2 '\t' Version not found." $pipe_to_log
fgdefault
return 1
fi 
if [ ${#version_list[@]} -gt 1 ]; then
get_biggest ${version_list[@]}
if [ -z $i_biggest ]; then
fgred
eval echo -e "'\t' Multiple matches error." $pipe_to_log
fgdefault
fi
else
biggest=${version_list[0]}
fi
eval echo -e "'\t' $2 '\t' v$biggest" $pipe_to_log
}

set_default_download_path()
{
if [ -d "$user_path/Videos" ]; then 
default_download_path="$user_path/Videos"
else
default_download_path="$user_path/Videot"
fi
}

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
eval echo -e "'\t' User defined $var_name=$setting" $pipe_to_log
else
        get_system_conf "$var_name"
        if [ $? = "0" ]; then
        setting=$conf 
        eval echo -e "'\t' System defined $var_name=$setting" $pipe_to_log
        else
        setting="$default"
        if [ -z "$setting" ]; then show_setting="no"; else show_setting=$setting; fi
        eval echo -e "'\t' Default $var_name=$show_setting" $pipe_to_log 
        fi
fi
}

logging()
{
sw=$1
if [ $(echo ${sw,,}) = "on" ]; then
pipe_to_log=" | tee -a $logs_dir/$log_file"
else
if [ $(echo ${sw,,}) = "off" ]; then
pipe_to_log=""
fi
fi	
}
logs_dir=.
log_file=adl_chk.log
pipe_to_log=""
username="$USER"
user_path=~
default_window_height=600
default_window_width=400
logging off
if [ "x$@" = "x-l" ] || [ "x$@" = "x-L" ]; then
rm $logs_dir/$log_file 2>/dev/null
logging on
echo -e "\nWriting a logfile \"$log_file\", too." 
fi
eval echo $pipe_to_log
eval echo "Checking Auto DL setup:" $pipe_to_log
eval echo "Background:" $pipe_to_log
show_version /etc/init.d/autodld "System-V Init"
show_version /opt/auto_dl/auto_dld "Job Manager"
show_version /opt/auto_dl/get_feed "Feed Reader"
show_version /opt/auto_dl/xml2tsv-yle-fi "TSV Converter"
eval echo "Foreground:" $pipe_to_log
show_version /home/$USER/.auto_dl/new_jobs "Job Maker"
show_version /home/$USER/.auto_dl/edit_jobs "Job Editor"
show_version /home/$USER/.auto_dl/xml2tsv-yle-fi "TSV Converter"
show_version /home/$USER/.auto_dl/adl_chk "Setup Check"
eval echo $pipe_to_log
eval echo "Configuration:" $pipe_to_log
set_default_download_path
define_setting "DownloadPath" "$default_download_path"
download_path=$setting
if [ ! -d "$download_path" ]; then
eval echo "Directory not found: $download_path" $pipe_to_log
fi
define_setting "WindowHeight" "$default_window_height"
define_setting "WindowWidth" "$default_window_width"
define_setting "UseWinFilenames" "no"
define_setting "MaxDownloadCount" "5"
define_setting "RecordExitTask" ""
file_to_run=$setting
if [ -n "$file_to_run" ]; then
bash_or_sh "$file_to_run"
result="$?"
fgred
case $result in
0)		fgdefault
		eval echo -e "'\t' Will run RecordExitTask=$file_to_run as user '\"'$username'\"'." $pipe_to_log
		record_exit_task="$file_to_run" ;;
101)	eval echo -e "'\t' File not found: RecordExitTask=$file_to_run" $pipe_to_log ;;
102)	eval echo -e "'\t' Execute permission not set for RecordExitTask=$file_to_run" $pipe_to_log ;;
103)	eval echo -e "'\t' Not bash/sh, will not run RecordExitTask=$file_to_run" $pipe_to_log ;;
esac
fgdefault
fi 
if [ -f /etc/auto_dl/autodl.conf ]; then
eval echo -e "'\t' System autodl.conf found."  $pipe_to_log
fi 
if [ -f ~/.auto_dl/autodl.conf ]; then
eval echo -e "'\t' User autodl.conf found."  $pipe_to_log
fi 
eval echo $pipe_to_log
