#!/bin/sh
#######################################
#	aStats #VERSIONNUMBER# by BigBob	      #
#				      #
# (original idea by Aviv Rosental)    #
#				      #
# a statistics analizer for aMule.    #
#				      #
# Have fun.			      #
#######################################
# DON'T TOUCH ANYTHING BETWEEN HERE : #
#######################################

BIN_PREFIX=#BIN_PREFIX#
SHARE_PREFIX=#SHARE_PREFIX#
DOC_PREFIX=#DOC_PREFIX#
VERSIONNUMBER=#VERSIONNUMBER#

#########################################
# AND HERE.				#
#########################################

INSTALLED=`echo $BIN_PREFIX | cut -d "#" -f 2`

if test "$INSTALLED"  == "BIN_PREFIX"; then
	echo
	echo "aStats must be installed to be used !!! (dynamic generation)."
	echo
	exit
fi

VERSIONL1="aStats Script $VERSIONNUMBER By BigBob, Get the latest @ http://bigbob.chez.tiscali.fr"
VERSIONL2="                  (original idea (v0.8) by Aviv Rosental)"
VERSIONSHORT="aStats Script $VERSIONNUMBER"
VERSIONNUMBERHTML=$VERSIONNUMBER
LASTSTATS=`date`
ASKUSER=$2
P2PPROCESSNAME=amule

###########
HPTESTING=N
###########

if test "$EUID" != "0" && test -n "$ASKUSER"; then
	echo "You must be root to specify a user !!!"
	exit
fi

if test "$EUID" == "0" && test -n "$ASKUSER"; then
	if test "$ASKUSER" != "root"; then
		HOMEUSER="/home/$ASKUSER"
	else
		HOMEUSER="/root"
	fi
fi

if test "$EUID" == "0" && test -z "$ASKUSER"; then
	ASKUSER=root
	HOMEUSER=/root
fi

if test "$EUID" != "0"; then
	HOMEUSER=`echo "$HOME"|cut -d "=" -f -`
	ASKUSER=`echo $HOME|cut -d "/" -f 3`
fi

function usage {
clear
echo 
echo "$VERSIONL1"
echo
echo "$VERSIONL2"
echo 
echo "Usage: aStats [--help|--quiet|--web|--console] [user]"
echo
echo " --help		Prints this"
echo " --quiet	Prints info to console (default)"
echo " --console	Same as --quiet with some debug output"
echo " --web		Make web page"
echo
echo
echo "IMPORTANT !!! : if you want to check stats of different user than"
echo "                the current, you must be root and use one of options :"
echo
echo "                [--quiet|--web|--console] user"
echo
echo "                user == user under aMule running !!!"
echo
echo "Example : -> aStats --web user <- WORKING !!!"
echo
echo "          -> aStats user       <- EXIT without any output !!!"
echo
}

function check_signature {
if test "$DEBUG" == "Yes"; then
	echo "$VERSIONL1"
	echo
	echo "$VERSIONL2"
	echo
	echo -n "Searching for signature file (amulesig.dat) in $HOMEUSER/.aMule/ ... "
	if [ -e "$HOMEUSER/.aMule/amulesig.dat" ]; then
		echo "Signature found"
		echo
	else 
		echo "Signature not Found."
		echo
		echo "Please check that 'enable online signature' is enable in the aMule preferences window"
		echo "and that you are running aMule with this user ($ASKUSER)."
		echo
		exit
	fi
else
	if [ -e "$HOMEUSER/.aMule/amulesig.dat" ]; then
		echo -n	
	else
	        echo "Signature not Found."
		echo
	        echo "Please check that 'enable online signature' is enable in the aMule preferences window"
		echo "and that you are running aMule with this user ($ASKUSER)."
		echo
	        exit
	fi
fi
}

function getVal {
HOSTUPTIME=`uptime | cut -d " " -f 4-`
CONNECTED=`cat $HOMEUSER/.aMule/amulesig.dat |head -1`
SERVERNAME=`cat $HOMEUSER/.aMule/amulesig.dat |head -2|tail -n 1`
SERVERIP=`cat $HOMEUSER/.aMule/amulesig.dat |head -3|tail -n 1`
SERVERPORT=`cat $HOMEUSER/.aMule/amulesig.dat |head -4|tail -n 1`
XMULEID=`cat $HOMEUSER/.aMule/amulesig.dat |head -5|tail -n 1`
DLSPEED=`cat $HOMEUSER/.aMule/amulesig.dat |head -6|tail -n 1`
ULSPEED=`cat $HOMEUSER/.aMule/amulesig.dat |head -7|tail -n 1`
WAITINGQUEUE=`cat $HOMEUSER/.aMule/amulesig.dat |head -8|tail -n 1`
SHAREDFILES=`cat $HOMEUSER/.aMule/amulesig.dat |head -9|tail -n 1`
NICK=`cat $HOMEUSER/.aMule/amulesig.dat |head -10|tail -n 1`
TOTALDL=`cat $HOMEUSER/.aMule/amulesig.dat |head -11|tail -n 1`
TOTALUL=`cat $HOMEUSER/.aMule/amulesig.dat |head -12|tail -n 1`
XMULEVERSION=`cat $HOMEUSER/.aMule/amulesig.dat |head -13|tail -n 1`

if [ `ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|wc -l` == 1 ]; then
	if [ "`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 1-2`" == "  " ] && [ "`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 4-5`" == "  " ] && [ "`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 7-8`" == "  " ]; then
		PROCESSUPTIME="has been running for 0 Day(s) and 00h00m`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 10-11`s"
	fi
	if [ "`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 1-2`" == "  " ] && [ "`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 4-5`" == "  " ] && [ "`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 7-8`" != "  " ]; then
		PROCESSUPTIME="has been running for 0 Day(s) and 00h`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 7-8`m`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 10-11`s"
	fi
	if [ "`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 1-2`" == "  " ] && [ "`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 4-5`" != "  " ] && [ "`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 7-8`" != "  " ]; then
		PROCESSUPTIME="has been running for 0 Day(s) and `ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 4-5`h`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 7-8`m`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 10-11`s"
	fi
	if [ "`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 1-2`" != "  " ] && [ "`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 4-5`" != "  " ] && [ "`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 7-8`" != "  " ]; then
		PROCESSUPTIME="has been running for `ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 1-2` Day(s) and `ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 4-5`h`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 7-8`m`ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|cut -b 10-11`s"
	fi
else
	PROCESSUPTIME="is not running"
fi

if test "$CONNECTED" == "0"; then
	if [ `ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|wc -l` == 1 ]; then
		STATUS="Offline (aMule running)"
	else
		STATUS="Offline (aMule not running)"
	fi
else
		STATUS="Online"
fi

if test "$HPTESTING" == "Y"; then
	HTML_PATH="$HOMEUSER/.aMule/$ASKUSER-amulestats-testing.html"
	GRAPHSIGN=aMule-online-sign-testing.png
else
	HTML_PATH="$HOMEUSER/.aMule/$ASKUSER-amulestats.html"
	GRAPHSIGN=aMule-online-sign.png
fi

if test "$CONNECTED" == "0"; then
	ID=""
else
	if test "$XMULEID" == "L"; then
		ID="LowID"
	else
		ID="HighID"
	fi
fi
}

function checkOff {
if [ `ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|wc -l` == 1 ] && test "$CONNECTED" == 0; then
	echo "aMule $XMULEVERSION $PROCESSUPTIME"
	echo "$NICK is $STATUS"
	echo "Total Download: $TOTALDL GB, Upload: $TOTALUL GB"
	echo "Sharing: $SHAREDFILES File(s) / Client(s) in queue: $WAITINGQUEUE"
	echo "Download: $DLSPEED kB/s, Upload: $ULSPEED kB/s"
	echo "Host Uptime: $HOSTUPTIME"
	exit
fi
}

function checkOn {
if [ `ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|wc -l` == 1 ]; then
	if [ "$SERVERNAME" = "$SERVERIP" ]; then
		echo "aMule $XMULEVERSION $PROCESSUPTIME"
		echo "$NICK is $STATUS $SERVERIP:$SERVERPORT with $ID"
		echo "Total Download: $TOTALDL GB, Upload: $TOTALUL GB"
		echo "Sharing: $SHAREDFILES File(s) / Client(s) in queue: $WAITINGQUEUE"
		echo "Download: $DLSPEED kB/s, Upload: $ULSPEED kB/s"
		echo "Host Uptime: $HOSTUPTIME"
	else
		echo "aMule $XMULEVERSION $PROCESSUPTIME"
		echo "$NICK is $STATUS $SERVERNAME ($SERVERIP:$SERVERPORT) with $ID"
		echo "Total Download: $TOTALDL GB, Upload: $TOTALUL GB"
		echo "Sharing: $SHAREDFILES File(s) / Client(s) in queue: $WAITINGQUEUE"
		echo "Download: $DLSPEED kB/s, Upload: $ULSPEED kB/s"
		echo "Host Uptime: $HOSTUPTIME"
	fi
else
	echo "aMule $XMULEVERSION $PROCESSUPTIME"
	echo "$NICK is $STATUS"
	echo "Total Download: $TOTALDL GB, Upload: $TOTALUL GB"
	echo "Sharing: $SHAREDFILES File(s) / Client(s) in queue: $WAITINGQUEUE"
	echo "Download: $DLSPEED kB/s, Upload: $ULSPEED kB/s"
	echo "Host Uptime: $HOSTUPTIME"
fi
}

function gen_html_and_graphsign {
###########################
# Generation of html page #
###########################
if test "$PROCESSUPTIME" == "is not running"; then
	STATUS="Offline (aMule not running)"
	ULSPEED="N/A"
	DLSPEED="N/A"
	WAITINGQUEUE="N/A"
	SHAREDFILES="N/A"
	SERVERNAME="N/A"
	SERVERIP="N/A"
	SERVERPORT="N/A"
else
	if test "$SERVERIP" == "0"; then
		SERVERNAME="Not Connected"
		SERVERIP="N/A"
		SERVERPORT="N/A"
	fi
	ULSPEED="$ULSPEED kB/s"
	DLSPEED="$DLSPEED kB/s"
fi
echo "{" > /tmp/aStats-Graphic-Signature-Generation
echo "s#VERSION#$VERSIONSHORTg" >> /tmp/aStats-Graphic-Signature-Generation
echo "s#VERSIONNUMBERHTML#$VERSIONNUMBERHTMLg" >> /tmp/aStats-Graphic-Signature-Generation
echo "s#CLIENT#aMule $XMULEVERSION $PROCESSUPTIMEg" >> /tmp/aStats-Graphic-Signature-Generation
echo "s#STATUS#$STATUSg" >> /tmp/aStats-Graphic-Signature-Generation
echo "s#NICK#$NICKg" >> /tmp/aStats-Graphic-Signature-Generation
echo "s#TIME#$LASTSTATSg" >> /tmp/aStats-Graphic-Signature-Generation
echo "s#UPLOADRATE#$ULSPEEDg" >> /tmp/aStats-Graphic-Signature-Generation
echo "s#DOWNLOADRATE#$DLSPEEDg" >> /tmp/aStats-Graphic-Signature-Generation
echo "s#QUEUE#$WAITINGQUEUEg" >> /tmp/aStats-Graphic-Signature-Generation
echo "s#SERVER#$SERVERNAMEg" >> /tmp/aStats-Graphic-Signature-Generation
echo "s#IP#$SERVERIPg" >> /tmp/aStats-Graphic-Signature-Generation
echo "s#PORT#$SERVERPORTg" >> /tmp/aStats-Graphic-Signature-Generation
echo "s#TOTALUP#$TOTALULg" >> /tmp/aStats-Graphic-Signature-Generation
echo "s#TOTALDOWN#$TOTALDLg" >> /tmp/aStats-Graphic-Signature-Generation
echo "s#NUMSHARE#$SHAREDFILESg" >> /tmp/aStats-Graphic-Signature-Generation
echo "s#KERNEL#`uname -r`g" >> /tmp/aStats-Graphic-Signature-Generation
echo "s#UPTIME#$HOSTUPTIMEg" >> /tmp/aStats-Graphic-Signature-Generation
echo "s#GRAPHSIGN#$GRAPHSIGNg" >> /tmp/aStats-Graphic-Signature-Generation
echo "}" >> /tmp/aStats-Graphic-Signature-Generation

sed -f /tmp/aStats-Graphic-Signature-Generation $SHARE_PREFIX/share/aStats-$VERSIONNUMBER/tmp.html > $HTML_PATH
USERGROUP=`id $ASKUSER|cut -d "(" -f 3|cut -d ")" -f 1`
if  test "$EUID" != "0" && test -n "$ASKUSER"; then
	chown $ASKUSER:$USERGROUP $HTML_PATH
fi
chown $ASKUSER:$USERGROUP $HTML_PATH
rm /tmp/aStats-Graphic-Signature-Generation

##########################################
# Generation of graphic online signature #
##########################################
#
# Where is ImageMagick ?
#
CONVERT=`whereis -b convert | awk '{print $2}'`
COMPOSITE=`whereis -b composite | awk '{print $2}'`
#
# ImageMagick Found !!! :-)
#
if test -n "$CONVERT"; then
	#
	# Default is Arial, if it doesn't work, set to a diffrent font path
	#
	FONT="Arial"
	### imagemagick commands to generate the amule graphic online signature with stats included
	
	# Cleanup
	
	rm -f "$HOMEUSER/.aMule/aMule-online-sign*.png"
	
	# Prepare dynamic mini icons status ...

	if (test "$ULSPEED" == "0.0 kB/s" || test "$ULSPEED" == "0,0 kB/s") && (test "$DLSPEED" == "0.0 kB/s" || test "$DLSPEED" == "0,0 kB/s") || test "$PROCESSUPTIME" == "is not running"; then
		UDLOGO=aMule-Up0-Down0-mini-logo.png
		# debug
		# echo "1 - UL: $ULSPEED - DL: $DLSPEED"
	elif (test "$ULSPEED" == "0.0 kB/s" || test "$ULSPEED" == "0,0 kB/s") && (test "$DLSPEED" != "0.0 kB/s" && test "$DLSPEED" != "0,0 kB/s") && test "$PROCESSUPTIME" != "is not running"; then
		UDLOGO=aMule-Up0-Down1-mini-logo.png
		# debug
		# echo "2 - UL: $ULSPEED - DL: $DLSPEED"
	elif (test "$ULSPEED" != "0.0 kB/s" && test "$ULSPEED" != "0,0 kB/s") && (test "$DLSPEED" == "0.0 kB/s" || test "$DLSPEED" == "0,0 kB/s") && test "$PROCESSUPTIME" != "is not running"; then
		UDLOGO=aMule-Up1-Down0-mini-logo.png
		# debug
		# echo "3 - UL: $ULSPEED - DL: $DLSPEED"
	elif (test "$ULSPEED" != "0.0 kB/s" && test "$ULSPEED" != "0,0 kB/s") && (test "$DLSPEED" != "0.0 kB/s" && test "$DLSPEED" != "0,0 kB/s") && test "$PROCESSUPTIME" != "is not running"; then
		UDLOGO=aMule-Up1-Down1-mini-logo.png
		# debug
		# echo "4 - UL: $ULSPEED - DL: $DLSPEED"
	fi

	if test "$ID" == "HighID"; then
		IDLOGO=aMule-Connected-High-mini-logo.png
	else
		IDLOGO=aMule-Connected-Low-mini-logo.png
	fi

	# Place static mini icons ...

	$COMPOSITE -geometry '+5+5' $SHARE_PREFIX/share/aStats-$VERSIONNUMBER/aMule-Running-mini-logo.png $SHARE_PREFIX/share/aStats-$VERSIONNUMBER/aMule-online-sign-clean-right-logo.png /tmp/aMule-temp1.png
	$COMPOSITE -geometry '+251+27' $SHARE_PREFIX/share/aStats-$VERSIONNUMBER/aMule-Shared-Files-mini-logo.png /tmp/aMule-temp1.png /tmp/aMule-temp2.png
	$COMPOSITE -geometry '+5+49' $SHARE_PREFIX/share/aStats-$VERSIONNUMBER/aMule-Server-Name-mini-logo.png /tmp/aMule-temp2.png /tmp/aMule-temp3.png
	$COMPOSITE -geometry '+250+71' $SHARE_PREFIX/share/aStats-$VERSIONNUMBER/aMule-Waiting-mini-logo.png /tmp/aMule-temp3.png /tmp/aMule-temp4.png
	$COMPOSITE -geometry '+5+93' $SHARE_PREFIX/share/aStats-$VERSIONNUMBER/aMule-Clock-mini-logo.png /tmp/aMule-temp4.png /tmp/aMule-temp5.png

	# aMule version & uptime :
	
	$CONVERT /tmp/aMule-temp5.png -font $FONT -pointsize 13 -fill white -draw "text +26+19 'aMule $XMULEVERSION $PROCESSUPTIME'" /tmp/aMule-temp5.png
	
	# Online status and nick
	
	if test "$CONNECTED" == "0" || test "$PROCESSUPTIME" == "is not running"; then
		$CONVERT /tmp/aMule-temp5.png -font $FONT -pointsize 13 -fill white -draw "text +25+41 '$NICK is $STATUS'" /tmp/aMule-temp5.png
	else
		if [ `ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|wc -l` == 1 ]; then
			$CONVERT /tmp/aMule-temp5.png -font $FONT -pointsize 13 -fill white -draw "text +25+41 '$NICK is $STATUS with $ID'" /tmp/aMule-temp5.png
		else
			$CONVERT /tmp/aMule-temp5.png -font $FONT -pointsize 13 -fill white -draw "text +25+41 '$NICK is $STATUS'" /tmp/aMule-temp5.png
		fi
	fi
	
	# Shared files
	
	if test "$CONNECTED" == "1" && test "$PROCESSUPTIME" != "is not running"; then
		$CONVERT /tmp/aMule-temp5.png -font $FONT -pointsize 13 -fill white -draw "text +271+41 'Shared files: $SHAREDFILES'" /tmp/aMule-temp5.png
	else
		if [ `ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|wc -l` == 1 ]; then
			$CONVERT /tmp/aMule-temp5.png -font $FONT -pointsize 13 -fill white -draw "text +271+41 'Shared files: $SHAREDFILES'" /tmp/aMule-temp5.png
		else
			$CONVERT /tmp/aMule-temp5.png -font $FONT -pointsize 13 -fill white -draw "text +271+41 'Shared files: N/A'" /tmp/aMule-temp5.png
		fi
	fi
	
	# Server name

	if test "$CONNECTED" == "1"; then
		$CONVERT /tmp/aMule-temp5.png -font $FONT -pointsize 13 -fill white -draw "text +25+63 '$SERVERNAME ($SERVERIP:$SERVERPORT)'" /tmp/aMule-temp5.png
	else
		$CONVERT /tmp/aMule-temp5.png -font $FONT -pointsize 13 -fill white -draw "text +25+63 '$SERVERNAME'" /tmp/aMule-temp5.png
	fi
	
	# Up & Down :
	
	if test "$CONNECTED" == "1" && test "$PROCESSUPTIME" != "is not running"; then
		$CONVERT /tmp/aMule-temp5.png -font $FONT -pointsize 13 -fill white -draw "text +25+85 'Up: $ULSPEED | Down: $DLSPEED'" /tmp/aMule-temp5.png
	else
		if [ `ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|wc -l` == 1 ]; then
			$CONVERT /tmp/aMule-temp5.png -font $FONT -pointsize 13 -fill white -draw "text +25+85 'Up: $ULSPEED | Down: $DLSPEED'" /tmp/aMule-temp5.png
		else
			$CONVERT /tmp/aMule-temp5.png -font $FONT -pointsize 13 -fill white -draw "text +25+85 'Up: N/A | Down: N/A'" /tmp/aMule-temp5.png
		fi
	fi
	
	# Waiting Clients :

	if test "$CONNECTED" == "1" && test "$PROCESSUPTIME" != "is not running"; then
		$CONVERT /tmp/aMule-temp5.png -font $FONT -pointsize 13 -fill white -draw "text +270+85 'Waiting Queue: $WAITINGQUEUE'" /tmp/aMule-temp5.png
	else
		if [ `ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|wc -l` == 1 ]; then
			$CONVERT /tmp/aMule-temp5.png -font $FONT -pointsize 13 -fill white -draw "text +270+85 'Waiting Queue: $WAITINGQUEUE'" /tmp/aMule-temp5.png
		else
			$CONVERT /tmp/aMule-temp5.png -font $FONT -pointsize 13 -fill white -draw "text +270+85 'Waiting Queue: N/A'" /tmp/aMule-temp5.png
		fi
	fi

	# Place dynamic mini icons ...

	# Connection status mini icon

	if test "$CONNECTED" == "0" || test "$PROCESSUPTIME" == "is not running"; then
		$COMPOSITE -geometry '+5+27' $SHARE_PREFIX/share/aStats-$VERSIONNUMBER/aMule-Not-Connected-mini-logo.png /tmp/aMule-temp5.png /tmp/aMule-temp6.png
	else
		$COMPOSITE -geometry '+5+27' $SHARE_PREFIX/share/aStats-$VERSIONNUMBER/$IDLOGO /tmp/aMule-temp5.png /tmp/aMule-temp6.png
	fi

	# Up & Down status mini icon

	if test "$CONNECTED" == "0" || test "$PROCESSUPTIME" == "is not running"; then
		$COMPOSITE -geometry '+5+71' $SHARE_PREFIX/share/aStats-$VERSIONNUMBER/$UDLOGO /tmp/aMule-temp6.png /tmp/aMule-temp7.png
	else
		if [ `ps --no-header -C $P2PPROCESSNAME -o etime,user --sort time|grep $ASKUSER|head -n 1|wc -l` == 1 ]; then
			$COMPOSITE -geometry '+5+71' $SHARE_PREFIX/share/aStats-$VERSIONNUMBER/$UDLOGO /tmp/aMule-temp6.png /tmp/aMule-temp7.png
		else
			$COMPOSITE -geometry '+5+71' $SHARE_PREFIX/share/aStats-$VERSIONNUMBER/$UDLOGO /tmp/aMule-temp6.png /tmp/aMule-temp7.png
		fi
	fi

	# Timestamp

	$CONVERT /tmp/aMule-temp7.png -font $FONT -pointsize 13 -fill white -draw "text +25+107 'Last update: $LASTSTATS'" /tmp/aMule-temp8.png

	cp /tmp/aMule-temp8.png $HOMEUSER/.aMule/$GRAPHSIGN

	# change permissions for user in root mode generation
	if  test "$EUID" == "0"; then
		chown $ASKUSER:$USERGROUP $HOMEUSER/.aMule/$GRAPHSIGN
	fi
	chmod +w $HOMEUSER/.aMule/$GRAPHSIGN


	# Clean
	
	rm /tmp/aMule-temp*.png
else
	echo "ImageMagick not found !!! check your system if you want dynamic graphic signature ..."
	echo
	echo "Possible causes :"
	echo
	echo "Verify Imagemagick Tools are installed (1st try 'locate convert')"
	echo
	echo "A generic graphic signature without stats is generated in replacement ..."
	echo
	if  test "$EUID" == "0"; then
		cp $SHARE_PREFIX/share/aStats-$VERSIONNUMBER/aMule-online-sign-clean.png $HOMEUSER/.aMule/aMule-online-sign.png
		chown $ASKUSER:$USERGROUP $HOMEUSER/.aMule/aMule-online-sign.png
	fi
	cp $SHARE_PREFIX/share/aStats-$VERSIONNUMBER/aMule-online-sign-clean.png $HOMEUSER/.aMule/aMule-online-sign.png
fi	
}

function quiet {
check_signature
getVal
checkOff
checkOn
}

function console {
check_signature
getVal
checkOff
checkOn
echo
}

function web {
check_signature
getVal
gen_html_and_graphsign
}

case $1 in
--help)
	usage
	exit 0
;;
--vdirs)
	echo "$VERSIONNUMBER|$BIN_PREFIX|$SHARE_PREFIX|$DOC_PREFIX"
	exit 0
;;
--quiet)
	quiet
	exit 0
;;
--console)
	DEBUG=Yes
	quiet
	exit 0
;;
--web)
	web
	exit 0
;;
esac

if [ x`echo $1 | cut -c 1` == "-" ]; then
	echo "$0: invalid option $1"
	echo "Try \`$0 --help' for more information."
fi

if [ "$#" == 0 ]; then
	quiet
fi
