/*
	Compile:  $azpc -p sjis2utf8.pl /no /e sjis2utf8
	For Easy: $sudo cp sjis2utf8 /usr/local/bin 
	Usage:    $sjis2utf8 *.pl
*/

top_level:-
	fileerrors(_,fail),
	get_parameter(X),
	conv_each(X).

conv_each([]):-!.
conv_each([A|L]):- each_file(A),conv_each(L).

each_file(See):-
	atom_append('nkf -w -w80 --overwrite ',See,Cmd),
	  system(Cmd),
	atom_add('cp ',[See,' ',See,'.bk'],Cpy),
	  system(Cpy),	
	s_pid(PID),
	atom_add(my_conv_tmp_,[PID],TELL),
	  see(See),tell(TELL),get11(_,_),
	  repeat,
		( get11(Before,Next) -> check_cd(Before,Next),fail ;true),
	  !,
	  seen,told,
	rename(TELL,See).

get11(X,Y):- get1(Y),e_register(0,X,Y).

check_cd(0'D,0'A):- nl,!,get11(_,_).
check_cd(Before,_):- put(Before),!.

atom_add(O,[],O):-!.
atom_add(B,[A|L],O):- term_atom(A,AA),atom_append(B,AA,C),atom_add(C,L,O).
