#!/bin/bash
#########################################################################################
#
# Edit 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.1  2012-09-27 Configuration file support, improved logging
# V1.11 2012-09-28 Fix system conf variable
# V1.12 2012-09-28 Minimal return status/logging fix
# V1.13 2012-09-28 Improved structure
# V1.14 2012-09-28 Logging text change
# V1.15 2012-10-09 Init removes temp files
# V1.16 2012-10-16 medium_title update
#
#########################################################################################

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
}

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

close_all()
{
pass_error="$*"
echo
echo "Job editor 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_failure
	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
}

build_list()
{
i=0
check_on="True"
if [ -z "$(ls -A $work_dir/remlist)" ]; then
unset my_list
else
for pathfile in "$*"/* 
	do
	long_title="$(echo $pathfile | sed 's/.*\///')"
	get_medium_title "$long_title" 
	my_list[i]=$check_on
	my_list[i+1]="$medium_title"
	((i=i+2))
	done
fi	
}

show_list()
{

box_title="Auto DL"
box_toptext="Kaikki nauhoitettavaksi asetetut ohjelmat"	
list_to_show=("$@")	
if [ -n "$list_to_show" ]; then 
box_column="    Lajittele klikkamalla tänne"
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="edit_job.log"
init
echo "$(date "+%F %T %z") Start complete." | tee -a $data_root/logs/$log_file
mkdir -p $work_dir/remlist  
find $jobs_dir -type f | sed 's/.*\///' | xargs -r -n 1 -I{} touch $work_dir/remlist/{} 
build_list $work_dir/remlist
username=$USER
define_setting "WindowHeight" 600
box_height=$setting
define_setting "WindowWidth" 400
box_width=$setting
show_list "${my_list[@]}"
if [ "$?" = "0" ]; then 
echo $choice_list | sed 's/|/\n/g'| xargs -r -n 1 -I{} rm "$work_dir/remlist/{}"
find $work_dir/remlist -type f | sed 's/.*\///' | xargs -r -n 1 -I{} find $jobs_dir -name "{}" -delete
removed=$(find $work_dir/remlist -type f | sed 's/.*\///' | tr '\n' ',' | sed 's/.$//g; s/,/, /g')
if [ -n "$removed" ]; then 
echo "$(date "+%F %T %z") Removed: $removed" | tee -a $data_root/logs/$log_file
else
echo "$(date "+%F %T %z") Nothing removed." | tee -a $data_root/logs/$log_file
fi
else
echo "$(date "+%F %T %z") Nothing removed." | tee -a $data_root/logs/$log_file
fi
rmdir $jobs_dir/* 2>/dev/null 
close_all 0
