#!/bin/sh
#chmod +x ./install.sh
#set -x
 
# Global Variable
export ftpserver='-nv ftp://ab.prvt-room.com:8003/_deploy'
export pluscolor=$(tput setab 4)
export nocolor=$(tput sgr 0)
export target=''

print_error () {
   echo $(tput setab 1)
   printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
   echo " "$1" "
   printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
   echo $(tput sgr 0) $'\n'
   su -c "killall -9 install.sh" root
   read $ERROR
}
print_color () {
   echo $(tput setab 4)
   printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
   echo " "$1" "
   printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
   echo $(tput sgr 0) $'\n'
}
print_color2 () {
	echo $'\n' $(tput setab 4) $1 $(tput sgr 0) $'\n'
}
export -f print_error
export -f print_color
export -f print_color2

# Скопировать все аддоны скрипты локально
print_color "Копируем скрипты в tmp-local-scripts"
target_in=/tmp/local-scripts/
rm -f ${target_in}*.sh
wget -P $target_in ${ftpserver}/local/*.sh
for script in ${target_in}*.sh ; do
	chmod +x $script
done

# Скопировать все скрипты локально
print_color "Копируем скрипты в tmp"
target_in=/tmp/
rm -f ${target_in}*.sh
wget -P $target_in $ftpserver/*.sh

# Выполнит все скрипты
if [[ "$#" -ne 1 ]]; then
	for script in ${target_in}*.sh ; do
		chmod +x $script
		print_color $script
		rm -f $script.log
		touch $script.log
		chmod 666 $script.log
		sh $script | tee -a $script.log
	done
	exit 1
fi

# Или выполнить выборочные скрипты
for arg in $*; do
	for script in /tmp/${arg}*.sh ; do
		chmod +x $script
		print_color $script
		rm -f $script.log
		touch $script.log
		chmod 666 $script.log
		sh $script | tee -a $script.log
	done
done
exit 0
