#!/bin/bash
# $Id: update_scripts.bash 511 2024-04-24 16:34:28Z bertrand $
# Ce script permet de mettre à jour les scripts existants ou ajouter 
# les nouveaux scripts.
# target
target_domain="park.cfi15.fr"
target_path="public/scripts/linux"
target_url="https://$target_domain/$target_path"
target_file_list="file.list"
# internal
script_name="update_scripts.bash"
script_name_temp="$script_name.temp"
this_script=$(basename $0)
this_script_temp="$this_script.temp"
skip_meta="skip"
download_tool=$(which wget)
download_cmdline="$download_tool -nv -q -t 1 -4"
file_to_update=$1
function update_self_meta()
{
	if [[ $file_to_update != $skip_meta ]]; then
		$download_cmdline $target_url/$this_script -O $this_script_temp
		this_script_hash=$(sha1sum $this_script | cut -d' ' -f1)
		this_script_temp_hash=$(sha1sum $this_script_temp | cut -d' ' -f1)
		if [[ $this_script_hash == $this_script_temp_hash ]]; then
			if [[ -f $this_script_temp ]]; then
				rm -f $this_script_temp
			fi
			echo "$this_script looks up to date"
			return 0
		else
			echo "$this_script has a new version available"
			/bin/bash $(pwd)/$this_script_temp $this_script &
			return 1
		fi
	else
		return 2
	fi

}
function update_self_real()
{
	if [[ $this_script == $script_name_temp ]]; then
		cat $script_name_temp > $script_name
		diff -q $script_name_temp $script_name > /dev/null
		if [[ $? == 0 ]]; then
			echo "$script_name updated"
			# une fois le script mis à jour, 
			# on s'arrête car on ne peut pas savoir,
			# dans l'état actuel des choses,
			# si la mise à jour a eu lieu dans une phase 
			# globale ou individuelle
			#$(pwd)/$script_name $skip_meta &
		else
			echo "$script_name not updated"
		fi
	else
		update_self_meta
	fi
}
function update_single()
{
	cf=$1
	cft=$2
	echo -n "$cf"
	$download_cmdline $target_url/$cf -O $cft
	if [[ -f $cft ]]; then
		if [[ -f $cf ]]; then
			#diff -q $cf $cft > /dev/null
			#if [[ $? != 0 ]]; then
			cf_hash=$(sha1sum $cf | cut -d' ' -f1)
			cft_hash=$(sha1sum $cft | cut -d' ' -f1)
			if [[ $cf_hash != $cft_hash ]]; then
				cat $cft > $cf
				echo " updated"
			else
				echo " kept"
			fi
		else
			mv $cft $cf
			chmod +x $cf
			echo " created"
		fi
		rm -f $cft
	else
		"$cft does not exist (download failed?)"
	fi
}
if [[ -n $download_tool ]]; then
	if [[ -z $file_to_update ]]; then
		if [[ -f $this_script_temp ]]; then
			rm -f $this_script_temp
		fi
		update_self_meta
		if [[ $? == 0 ]]; then
			if [[ -f $target_file_list ]]; then
				rm -f $target_file_list
			fi
			$download_cmdline $target_url/$target_file_list
			if [[ -f $target_file_list ]]; then
				while read current_file; do
					current_file_temp="$current_file.temp"
					update_single $current_file $current_file_temp
				done < $target_file_list
				rm -f $target_file_list
			else
				echo "$target_file_list not found"
			fi
		fi
	else
		if [[ $file_to_update != $script_name ]]; then
			file_to_update_temp="$file_to_update.temp"
			update_single $file_to_update $file_to_update_temp
		else
			update_self_real
		fi
	fi
else
	echo "$download_tool not found"
fi
