/*	make.c
 *  (c) Copyright 1991 by Ben Eng, All Rights Reserved
 *
 */

#include <string.h>
#include <ctype.h>
#include <fcntl.h>

#include <clib/exec_protos.h>
#include <clib/dos_protos.h>

#include "make.h"
#include "depend.h"

static int make_implicit( char *goalname, int *remake_flag );

/*
 *		Get the modification time of a file.  If the first
 *		doesn't exist, it's modtime is set to 0.
 */

time_t
modtime( char *filename )
{
	struct FileInfoBlock *fib;
	BPTR myLock;
	long ioErr;
	time_t mtime = 0L;

	fib = (struct FileInfoBlock *)malloc( sizeof(struct FileInfoBlock));
	if( fib ) {
		if( !(myLock = Lock( filename, ACCESS_READ ))) {
			if( (ioErr = IoErr()) != ERROR_OBJECT_NOT_FOUND)
				logprintf( "Can't Lock '%s'; error %ld\n", filename, ioErr );
		}
		else if( !Examine( myLock, fib )) {
			UnLock(myLock);
			logprintf( "Can't Examine '%s'; error %ld", filename, IoErr() );
		}
		else {
			mtime = fib->fib_Date.ds_Tick / TICKS_PER_SECOND +
				 60*fib->fib_Date.ds_Minute + 86400 * fib->fib_Date.ds_Days;
			UnLock( myLock );
		}
		free( fib );
	}
	return( mtime );
}

/* return true if targ1 is newer than targ2 */
int
isnewer( char *targ1, char *targ2 )
{
	time_t t1, t2;
	long diff;
	int retval;

	t1 = modtime( targ1 );
	t2 = modtime( targ2 );
	diff = (long)(t1 - t2);
	retval = (diff > 0L) ? 1 : 0;

	debugprintf( 5, ("isnewer %s=%ld,%s=%ld diff=%ld: %s\n", targ1, t1,
		targ2, t2, diff, (retval) ? "yes" : "no" ));

	return( retval );
}

/* recursively make a target filename */
int
make_filename( const char *goalname, int *made )
{
	int remake_flag = (Param.all_mode) ? 1 : 0;	
	struct depend *dep;
	struct List *cmdlist;
	struct target *goal = find_target( goalname );
	char *depname = NULL;

	Global.recursion_level++;
	*made = 0;

	if( Global.recursion_level == 1 ) {
		logprintf( "\tmake( %s )\n", goalname );
	}
	else  {
		debugprintf( 1, ( "\n\tmake %d ( %s )\n", Global.recursion_level,
		goalname ));
	}
	if( goal ) {
		if( goal->dependlist.lh_Head->ln_Succ ) {
			for( dep = (struct depend *)goal->dependlist.lh_Head; 
				dep->node.ln_Succ; dep = dep->node.ln_Succ ) {
				int made_flag;

				debugprintf( 1, ("%s depends on %s\n", goalname, dep->name ));
				if( make_filename( dep->name, &made_flag )) return( 1 );
				if( made_flag || isnewer( dep->name, goalname )) {
					depname = dep->name;
					remake_flag = 1;
				}
			} /* for */
		} /* if has dependencies */
		else remake_flag = 1;
	} /* if */

	/* if no explicit rule for goal OR goal has no commands */
	if( !goal || !goal->commandlist.lh_Head->ln_Succ ) {
		int retval = make_implicit( goalname, &remake_flag );
		--Global.recursion_level;
		*made = remake_flag;
		return( retval ); /* illegal reuse of remake_flag :-) */
	}

	--Global.recursion_level;

	if( remake_flag ) {
		struct List *cmdlist;
		int retval;

		cmdlist = (goal->flags & TF_OWNER ) ? &goal->commandlist :
			goal->alternate;
		if( cmdlist->lh_Head->ln_Succ ) {
			set_target_macros( goalname, depname );
			retval = recipe( goalname, cmdlist );
			set_target_macros( NULL, NULL );
			if( retval ) return( 1 );
			*made = 1;
		}
	}
	else {
		debugprintf( 2,("%s is up to date\n", goal->name ));
	}
	return( 0 );
}

/*	use this inference engine as a last resort
 *	use suffix rules to determine dependencies and commands for goalname
 */
static int
make_implicit( char *goalname, int *remake_flag )
{
	char *depfile = (char *)malloc( MAXPATHNAME );
	char *depname = NULL;
	struct suffixrule *sr;
	char *dot;
	char suf[ MAXSUFFIX ];

	debugprintf( 1, ("\tmake_implicit( %s )\n", goalname ));
				
	if( !depfile ) goto death; /* no mem */

	dot = strrchr( goalname, '.' );
	if( !dot ) goto out;

	strcpy( suf, dot + 1 );
	for( sr = &Global.suffixlist.lh_Head; sr->node.ln_Succ;
		sr = sr->node.ln_Succ ) {
		if( !strcmp( suf, sr->tar_suf ) ) {
			debugprintf( 2, ("Matched Suffix rule .%s.%s to %s\n",
				sr->dep_suf, sr->tar_suf, goalname ));
			if( *sr->dep_suf ) {
				strcpy( depfile, goalname );
				dot = strrchr( depfile, '.' );
				strcpy( dot + 1, sr->dep_suf );
				if( !access( depfile, 0 )) { /* found it */
					int made_flag;
					depname = depfile;
					debugprintf( 2, ("double suffix rule matches %s\n",
						depname ));
					if( make_filename( depname, &made_flag )) {
						goto death;
					}
					if( made_flag || isnewer( depname, goalname ))
						*remake_flag = 1;
					break;
				} /* else not the right rule, continue to find another */
			}
			else {
				debugprintf( 2, ("single suffix rule applies\n" ));
				*remake_flag = 1;
				break;
			}
		} /* if */
	} /* for each suffix rule */

	if( !sr->node.ln_Succ ) {
		logprintf( "don't know how to make %s\n", goalname );
		goto death;
	}
	if( *remake_flag ) {
		struct List *cmdlist;
		struct target *goal = sr->targ;

		set_target_macros( goalname, depname );

		cmdlist = (goal->flags & TF_OWNER ) ? &goal->commandlist :
			goal->alternate;
		if( cmdlist->lh_Head->ln_Succ ) {
			if( recipe( goalname, cmdlist )) goto death;
		}
		else *remake_flag = 0;
		set_target_macros( NULL, NULL );
	}
	else {
		debugprintf( 2,("%s is up to date\n", goalname ));
	}
out:
	free( depfile );
	return( 0 );
death:
	if( depfile ) free( depfile );
	return( 1 );
}
