#!/bin/bash

clear

echo " ######################################################"
echo " ##                                                  ##"
echo " ##  XDCCMANAGE                                      ##"
echo " ##                                                  ##"
echo " ##  Lancia  tanti  iroffer  quanti  sono  i         ##"
echo " ##  config  nella  directory  corrente.             ##"
echo " ##  Sito: http://xdccmanage.altervista.org/         ##"
echo " ##  Irc: //server -m irc.chlame.net -j #xdccManage  ##"
echo " ##                                                  ##"
echo " ##  21 Novembre 2011                                ##"
echo " ##  Autore: puccio                                  ##"
echo " ##                                                  ##"
echo " ######################################################"

echo ""

arch=`uname -m`
script="script.bash"

if [ $arch == "x86_64" ] ; then
	iroffer="iroffer-it64"
else
	iroffer="iroffer-it32"
fi

if [ -f $iroffer ] ; then
	if [ ! -x $iroffer ] ; then
		chmod +x $iroffer
	fi
else
	echo -n "Errore:" $iroffer "non esiste, VUOI CARICARLO? [si/no]: "
	read risp
	while [[ ! -n $risp ]] ; do
		echo -n "Errore:" $iroffer "non esiste, VUOI CARICARLO? [si/no]: "
		read risp
	done
	if [ $risp == "si" ] ; then
		if [ $arch == "x86_64" ] ; then
			if [ ! -f iroffer-it64 ] ; then
				echo -n " > Sto scaricando iroffer-it64 ... "
				wget http://xdccmanage.altervista.org/tool/iroffer-it64 > /dev/null 2>&1 && chmod 700 iroffer-it64 
				echo "OK"
			fi
		else
			if [ ! -f iroffer-it32 ] ; then
				echo -n " > Sto scaricando iroffer-it32 ... "
				wget http://xdccmanage.altervista.org/tool/iroffer-it32 > /dev/null 2>&1 && chmod 700 iroffer-it32
				echo "OK"
			fi
		fi
	else
		echo ""
		exit 0
	fi
fi

echo ""

if [ -f $script ] ; then
	if [ ! -x $script ] ; then
		chmod +x $script
	fi
else
	echo -n "Errore:" $script "non esiste, VUOI CARICARLO? [si/no]: "
	read risp
	while [[ ! -n $risp ]] ; do
		echo -n "Errore:" $script "non esiste, VUOI CARICARLO? [si/no]: "
		read risp
	done
	if [ $risp == "si" ] ; then
		if [ ! -f $script ] ; then
			echo -n " > Sto scaricando script.bash ... "
			wget http://xdccmanage.altervista.org/tool/$script > /dev/null 2>&1 && chmod 700 $script
			echo "OK"
		fi
	else
		echo ""
		exit 0
	fi
fi

echo ""
echo "  STO INIZIANDO  "
for file in *.config; do
	./$iroffer -b $file > /dev/null 2>&1
	echo "  >>" $file "... OK"
done

echo ""
