#!/bin/rc

# svcrepair
# checks list of mia svcin, attempts to remount, discards if impossible

rfork ns
echo `{date} svcrepair >>/tmp/grid/log/log

# for each service in miasvc, try to reconnect
# if it succeeds move it back to svcin, otherwise move it to dedsvc

for (i in /tmp/grid/miasvc/*){
	if (! ~ $i '/tmp/grid/miasvc/*'){
		RESCUE1=0
		RESCUE2=0
		SVCNAME=`{echo $i |awk -F/ '{print $5}'}
		DIALSTRING=`{cat /tmp/grid/miasvc/$SVCNAME}
		g/local unmount /g/$SVCNAME/s
		sleep 5
		rm /srv/$SVCNAME
		if (recover $DIALSTRING $SVCNAME) RESCUE1=success
		if (~ $RESCUE1 success){
			sleep 2
			g/local mount /srv/$SVCNAME /g/$SVCNAME/s		
			sleep 5
			touch /tmp/grid/work/testsvc$SVCNAME
			rc -c 'ls /g/$SVCNAME/s >>/tmp/grid/work/testsvc$SVCNAME' &
			sleep 4
			if (test -e /proc/$apid) echo kill>/proc/$apid/note
			sleep 2
			if (test -e /proc/$apid) echo kill>/proc/$apid/ctl
			sleep 5
			RESCUE2=`{cat /tmp/grid/work/testsvc$SVCNAME}
			if (~ $RESCUE2 ??*){
				echo `{date} RESCUE of $SVCNAME >>/tmp/grid/log/log
				mv /tmp/grid/miasvc/$SVCNAME /tmp/grid/svcin/$SVCNAME
				echo service $SVCNAME rescued
			}
		}	
		if (! ~ $RESCUE2 ??*){
			echo `{date} DEAD SVC $SVCNAME removed >>/tmp/grid/log/log
			echo rescue of service $SVCNAME failed marking as dead
			if (test -e /srv/$SVCNAME) rm /srv/$SVCNAME
			mv /tmp/grid/miasvc/$SVCNAME /tmp/grid/dedsvc/$SVCNAME
		}
	}
}

if (test -e /tmp/grid/info/miasvc/*) rm /tmp/grid/miasvc/*
if (test -e /tmp/grid/work/testsvc*) rm /tmp/grid/work/testsvc*

echo completed attempted fixups for broken service mounts
