#!/bin/sh

#
#	xnews_uptime -
#	
#	Determine how many days the current copy of X11/NeWS server has
#	been running and output to the screen.
#

/bin/ls -l /tmp/.X11-unix | /bin/awk '
BEGIN	{	
	daysinmonth[1] = 31
	daysinmonth[2] = 28
	daysinmonth[3] = 31
	daysinmonth[4] = 30
	daysinmonth[5] = 31
	daysinmonth[6] = 30
	daysinmonth[7] = 31
	daysinmonth[8] = 31
	daysinmonth[9] = 30
	daysinmonth[10] = 31
	daysinmonth[11] = 30
	daysinmonth[12] = 31

	monthno["Jan"] = 1
	monthno["Feb"] = 2
	monthno["Mar"] = 3
	monthno["Apr"] = 4
	monthno["May"] = 5
	monthno["Jun"] = 6
	monthno["Jul"] = 7
	monthno["Aug"] = 8
	monthno["Sep"] = 9
	monthno["Oct"] = 10
	monthno["Nov"] = 11
	monthno["Dec"] = 12
}

{

#	Determine current display number.
	if(index(display, ":1"))
		file="X1"
	else
		file="X0"

#	Extract month and day server was started.
#	Support both /bin/ls and /5bin/ls.
	if ($8 == file) {
		month=monthno[$5]
		dayofyear=$6
	} else if ($9 == file) {
		month=monthno[$6]
		dayofyear=$7
	}
}

END	{
	if (month == 0)
		print "no record of X11/NeWS server running."
	else {
		# determine what day of the year the server was started.
		# today is already set to the day of the year for today.
		month--
		while (month > 0) {
			dayofyear += daysinmonth[month]
			month--
		}
		if (today >= dayofyear)
			running=today - dayofyear
		else
			running=365 - dayofyear + today

		if (running == 1)
			printf("X11/NeWS server has been running for 1 day.\n")
		else
			printf("X11/NeWS server has been running for %d days.\n",running)
	}
}
' display=$DISPLAY today=`date +%j` -
