#! /bin/bash
# original and unsupported by SISalp
follow="-f"
def_lines="15"
def_file="/var/log/apache2/error.log"
case "$1" in
--version|-v)
	echo "$0 version 1.1 - 5 november 2017"
	exit 0
	;;
--help|-h)
	$0 --version
	echo "MYLOG syntaxe"
	echo "MYLOG --help|-h				#print this help"
	echo "MYLOG --file|-f	path_to_file	[shortcut]	#set default file or shortcut to print"
	echo "MYLOG --file|-f	. [shortcut]		#clear file to print"
	echo "MYLOG --file|-f	[shortcut]		# print file to print"
	echo "MYLOG --list|-l	[nb_lines]		#print all file, $def_lines is default"
	echo "MYLOG --version|-v			#print version"
	echo "MYLOG --update|-u				#download a new copy of this script"
	echo "MYLOG	[path_to_file|shortcut][nb_lines]	#tail of file, $def_lines is default"
	echo ""
	$0 --file
	exit 0
	;;
--file|-f)
	shift
	if [ ! -z "$1" ] ; then
		case "$1" in
		.)
			if [ -z "$2" ] ; then
				rm -f ~/MYLOG_file.conf
				echo "$LINENO| default file is back to $def_file"
			else
				rm -f ~/MYLOG_file-$2.conf
				echo "$LINENO| shortcut $2 is deleted"
			fi
			;;
		*)
			if [ -z "$2" ] ; then
				if [ -f "$1" ] ; then
					echo "$1" > ~/MYLOG_file.conf
					echo "$LINENO| $1 is the new default file"
				else
					if [ -f ~/MYLOG_file-$1.conf ] ; then
						echo -n "$LINENO| $1:		"
						cat ~/MYLOG_file-$1.conf
					else
						echo "$LINENO| file or shortcut $1 not found"
					fi
				fi
			else
				echo "$1" > ~/MYLOG_file-$2.conf
				echo "$LINENO| $1 file is memorized as shortcut $2"
			fi	
			;;
		esac
	else
		echo "$LINENO| default file and shortcuts"
		if [ -f ~/MYLOG_file.conf ] ; then
			echo -n "$LINENO| default:		"
			cat ~/MYLOG_file.conf
		else
			echo "$LINENO| built-in:		$def_file"
		fi
		shortcuts=`ls ~/MYLOG_file-*.conf 2>/dev/null`
	
		for shortcut in $shortcuts ; do
			name=`echo "$shortcut" | cut -d. -f1 | cut -d- -f2`
			echo -n "$LINENO| $name: 		"
			cat $shortcut
		done
	fi
	echo "shortcuts are located in user directory"
	exit 0
	;;
--list|-l)
	shift
	follow=""
	;;
--update|-u)
	cd /usr/local/bin
	OLDVERSION=`$0 --version`
	rm -f MYLOG
	wget -q http://download.sisalp.net/scripts/MYLOG
	chmod 755 MYLOG
	NEWVERSION=`$0 --version`
	case "$NEWVERSION" in
	$OLDVERSION)
		echo "$0 is uptodate $NEWVERSION"
		;;
	*)
		echo "$0 downloaded new version from $download_source $NEWVERSION"
		;;
	esac
	exit 0
	;;
esac
nblin="$def_lines"
if [ -z "$1" ] ; then
	if [ -f ~/MYLOG_file.conf ] ; then
		file=`cat ~/MYLOG_file.conf`
	else
		file="$def_file"
	fi
else
	if [ -f "$1" ] ; then
		file="$1"
	else
		if [ -f ~/MYLOG_file-$1.conf ] ; then
			file=`cat ~/MYLOG_file-$1.conf`
		else
			case "$1" in
			[0-9]*)
				nblin="$1"
				;;
			*)
				echo "file $1 not found"
				$0 --help
				exit 1
				;;
			esac
		fi
	fi
fi
case "$2" in
[0-9]*)
	nblin="$2"
	;;
esac
nbcol=`tput cols`
case "$follow" in
-f)
	echo "$LINENO| content of $file"
	tail -n $nblin $follow $file | cut -b -$nbcol
	;;
*)
	echo "$LINENO| content of $file"
	tail -n $nblin $file | cut -b -$nbcol
	;;
esac
exit 0
