/* ntp.h for Syowa operation
 * =========================
 *
 * ver 1.00
 *
 * This is a set of routines
 * related to NonTxPeriod(NTP) feature
 * which need to be implemented in RCP's
 * for Syowa operation
 * You have to include next line in RCP's
 * to use NTP family
 *	#include "ntp.h"
 * 
 *				by Sessai@NIPR
 */

#define MAX_NTP_NUM	100

/* global variables for NTP routines... */

short int ntp_start[MAX_NTP_NUM];	/* hhmm */
short int ntp_end[MAX_NTP_NUM];		/* hhmm */
short int ntp_num;

extern char errlog_name[];		// declared in support.c

/* -------------------------
 * NTP routines... by Sessai
 * -------------------------
 */

/* NTP(NonTxPeriod) routines for Syowa operation
 *					added by Sessai in Dec.,1996
 *					modified by Sessai in Oct.,1997
 *					modified by Sessai in Nov.,1997
 *
 *  int ntp_read(*char errlog);
 *	a routine to read an NTP(NonTxPeriod) table file for Syowa operation
 *		and to set the NTP entries and the entry number
 *		to global variables 
 *
 *  //long int ntp_check(int hr,int mn,int sc,int ms);
 *  int ntp_check(int hr,int mn,int sc,int ms);
 *	a routine to check if it's now in NTP(NonTxPeriod) period or not
 *		and to calculate the rest time (in second)
 *		up to the end of an NTP period which we're now in
 *		and sleep for a required period!!
 *				for Syowa operation
 *
 */

int ntp_read(char *errlog) {

/* ntp_read(char *errlog);
 * ======================
 *	a routine to read an NTP(NonTxPeriod) table file for Syowa operation
 *		and to set the NTP entries and the entry number
 *		to global variables 
 * ---------------------------------------------------------------------------
 *	environmental variable
 *			which should be exported in /radops/scripts/rad_export
 *		SD_NTP_TBL
 *			: path name of your NTP table file
 *				e.g.) export SD_NTP_TBL=/radops/tables/NTP.tbl
 *	NTP table file which shuold be prepared for :
 *			a text file which has NTP entries as much as you need,
 *			each of which has following format
 *				HHMM hhmm
 *				 where	HHMM : start time (HH=hour,MM=minute)
 *					hhmm :  end  time (hh=hour,mm=minute)
 *			It's OK if NTP entry number is zero,
 *			but the number should be less than MAX_NTP_NUM below.
 *			(if not, entries which exceed the number, just ignored)
 *	global defined variable
 *		MAX_NTP_NUM ( defined in /radops/usr/include/ntp(_def).h )
 *			: maximun entry number in NTP table file (now it's 100)
 *		errlog_name[];
 *			: extern string declared in support.c
 *	global variables which will be set by this routine
 *		short int ntp_start[MAX_NTP_NUM]
 *			: start time ("hhmm") array of NTP
 *		short int ntp_end[MAX_NTP_NUM]
 *			:  end  time ("hhmm") array of NTP
 *		short int ntp_num
 *			: entry number in NTP table file ( < MAX_NTP_NUM )
 *	input  value	: char *errlog : errlog task name
 *				if NULL then errlog_name will be "ERRLOG"
 *	return value	: 0 if this routine can read an NTP file successfully 
 *			: -1 if some error occurrs
 *
 *						on Jan. 8, 1997 by Sessai@NIPR
 *						on Oct.26, 1997 by Sessai@NIPR
 */

	char *ntp_file;
	FILE *ntp_fp;
	short int scan_status;
	short int i;
	char log_message[80]="";

	char *routine_name="ntp_read";

// #ifdef REPORT
	fprintf(stderr,"NTP(NonTxPeriod) table being read in...\n");
// #endif

	if (errlog !=NULL) strcpy(errlog_name,errlog);

	ntp_num = 0;

	if ((ntp_file =getenv("SD_NTP_TBL"))==NULL) {
		log_error(errlog_name,routine_name,
				"NTP: SD_NTP_TBL not defined");
		return -1;
	}

	if ( (ntp_fp = fopen(ntp_file,"r"))==NULL) {
		log_error(errlog_name,routine_name,
				"NTP: NTP table file not found");
		return -1;
	}

	scan_status = fscanf(ntp_fp,"%d %d",
		&ntp_start[ntp_num],&ntp_end[ntp_num]);
   
	while(scan_status != EOF) {
		ntp_num++;
		if(ntp_num == MAX_NTP_NUM){
			log_error(errlog_name,routine_name,
				"NTP: NTP entry num reaches MAX!");
			// break;
		}
		if(ntp_num >= MAX_NTP_NUM){
			log_error(errlog_name,routine_name,
				"NTP: NTP entries too much!! ignored...");
			// return -1;
			ntp_num--;
			break;
		}
		scan_status = fscanf(ntp_fp,"%d %d",
			&ntp_start[ntp_num],&ntp_end[ntp_num]);
	}

	fclose(ntp_fp);

	if (ntp_num==0) {
			sprintf(log_message,
				"NTP: No NTP entry in %s",ntp_file);
			log_error(errlog_name,routine_name,log_message);
	} else {
  		for (i=0;i<ntp_num;i++) {
			sprintf(log_message,"NTP:  %3d : %04d - %04d",
					i+1,ntp_start[i],ntp_end[i]);
			log_error(errlog_name,routine_name,log_message);
		}
	}
			sprintf(log_message,"NTP:   ...ntp_num=%d",ntp_num);
			log_error(errlog_name,routine_name,log_message);

	return 0;
}


int ntp_check(int hr,int mn,int sc,int ms) {

/* int ntp_check(int hr,int mn,int sc,int ms);
 * ===========================================
 *	a routine to check if it's now in NTP(NonTxPeriod) period or not
 *		and to calculate the rest time (in second)
 *		up to the end of an NTP period which we're now in
 *		and sleep for a required period!!
 *				for Syowa operation
 *
 *	global defined variable
 *		MAX_NTP_NUM ( defined in /radops/usr/include/syowa_def.h )
 *			: maximun entry number in NTP table file (now it's 100)
 *	global variables which are set by ntp_read routine from NTP table file
 *			and which are used in this routine
 *		short int ntp_start[MAX_NTP_NUM]
 *			: start time ("hhmm") array of NTP
 *		short int ntp_end[MAX_NTP_NUM]
 *			:  end  time ("hhmm") array of NTP
 *		short int ntp_num
 *			: entry number in NTP table file ( < MAX_NTP_NUM )
 *	input  value	: int hr : hour now
 *			: int mn :  min now
 *			: int sc :  sec now
 *			: int ms : msec now
 *	//OLD! return value	: 0L if it's now not in NTP period
 *	//OLD! 		: >0L time to end an NTP where we're now in seconds
 *	New!!! return value	: 0 if it's now NOT in NTP period
 *	New!!!! 		: 1 if it's now in NTP period!!!!
 *
 *						on Jan. 8, 1997 by Sessai@NIPR
 *						on Nov. 8, 1997 by Sessai@NIPR
 */

	char *routine_name="ntp_check";
	long int now,nows,rsec;
	short int i;

	if (ntp_num==0) return 0;

	now = (long)hr * 10000L + (long)mn * 100L + (long)sc;
	nows= (long)hr *  3600L + (long)mn *  60L + (long)sc;

	for (i=0;i<ntp_num;i++) {
	    if ( ( (long)ntp_start[i]*100L <= now) &&
	         ( now < (long)ntp_end[i]*100L   )    ){

		rsec=(long)((ntp_end[i]/100)*60 + (ntp_end[i] % 100))*60L
			- nows;
//#ifdef SHOW
		fprintf(stderr,"NTP_check : ");
		fprintf(stderr,"S%02d:%02d-",ntp_start[i]/100,ntp_start[i]%100);
		fprintf(stderr,"E%02d:%02d" ,ntp_end[i]  /100,ntp_end[i]  %100);
		fprintf(stderr,"-N%02d:%02d:%02d",hr,mn,sc);
		fprintf(stderr,"=>Wait%ldsec\n",rsec);
//#endif

		if(rsec <= 2) {	if(rsec>0L) sleep((unsigned int)rsec-1);
				delay( ((1000-ms-20)>0) ? (1000-ms-20) : 0); }
		else if(rsec <= 10) sleep(2);
		else if(rsec % 10 != 0) sleep((unsigned int)rsec % 10);
		else sleep(10);

		return 1;	//return rsec;
	    }
	}

	if(sc==59) {
		delay( ((1000-ms)<20) ? (1000-ms) : 20);
		return 1;
	}

	return 0;
}