/**********************************************************************
 
	Copyright (C) 2003 Hirohisa MORI <joshua@nichibun.ac.jp>
 
	This program is free software; you can redistribute it 
	and/or modify it under the terms of the GLOBALBASE 
	Library General Public License (G-LGPL) as published by 

	http://www.globalbase.org/
 
	This program is distributed in the hope that it will be 
	useful, but WITHOUT ANY WARRANTY; without even the 
	implied warranty of MERCHANTABILITY or FITNESS FOR A 
	PARTICULAR PURPOSE.

**********************************************************************/


#include	"memory_debug.h"
#include	"gbacrp.h"
#include	"gbmp.h"

void
_trigger_around(int ses,L_CHAR * url)
{
URL u;
int p;
int local_ip;
L_CHAR * path;
	p = get_my_port();
	local_ip = get_localhostip();
	get_url2(&u,url,1236);
	if ( u.server == 0 && u.port == 0 ) {
		u.port = p;
	}
	path = get_url_filepath(&u);
	if ( cmp_site(u.server,0,u.port,
			0,local_ip,p) == 0 ) {
		insert_tick_que(0,path,TQT_CRD,TQL_ACRP);
	}
	else {

		gc_push(0,0,"remote_fetch");

		remote_session(
			gblisp_top_env0,
			ses,
			&u,
			l_string(std_cm,"gbstd"),
			l_string(std_cm,"user"),
			l_string(std_cm,"Get"),
			List(List(n_get_symbol("MPTrigger"),
				get_string(path),
				-1),
				-1),
			0,0,1);

		gc_pop(0,0);
	}
	free_url(&u);
	d_f_ree(path);
}

void
_purge(int ses,L_CHAR * url,L_CHAR * purge_path)
{
URL u;
int p;
int local_ip;
L_CHAR * path;
URL uu;
char * buf;
	get_url2(&uu,purge_path,1237);
	if ( uu.proto == 0 ) {
		uu.proto = nl_copy_str(std_cm,"xlp");
	}
	if ( uu.server == 0 ) {
		buf = d_alloc(1000,1);
		get_localhostname(buf);
		uu.server = nl_copy_str(std_cm,buf);
		d_f_ree(buf);
	}
	if ( uu.port == 0 )
		uu.port = get_my_port();
	purge_path = get_url_str2(&uu);
	free_url(&uu);

	p = get_my_port();
	local_ip = get_localhostip();
	get_url2(&u,url,1238);
	if ( u.server == 0 && u.port == 0 ) {
		u.port = p;
	}
	path = get_url_filepath(&u);
	if ( cmp_site(u.server,0,u.port,
			0,local_ip,p) ) {

		gc_push(0,0,"remote_fetch");

		remote_session(
			gblisp_top_env0,
			ses,
			&u,
			l_string(std_cm,"gbstd"),
			l_string(std_cm,"user"),
			l_string(std_cm,"Get"),
			List(List(n_get_symbol("MPPurge"),
				get_string(purge_path),
				-1),
				-1),
			0,0,1);

		gc_pop(0,0);
	}
	free_url(&u);
	d_f_ree(path);
}

void
trigger_around(int ses,L_CHAR * my_path,AROUND_LIST * al)
{
	for ( ; al ; al = al->next ) {
		_purge(ses,al->crd,my_path);
		_trigger_around(ses,al->crd);
	}
}

