RRCI18N	; Internationalization Converter
	;RRCI18N V0.0001;;MTZ/RCR/27MAY2004
	;Donated by Chris Richardson
	;Initial database design by Marcus Werner
	;Copyright 2008 WorldVistA.  Licensed under the terms of the GNU      
	;General Public License See attached copy of the License.             
	;                                                                     
	;This program is free software; you can redistribute it and/or modify 
	;it under the terms of the GNU General Public License as published by 
	;the Free Software Foundation; either version 2 of the License, or    
	;(at your option) any later version.                                  
	;                                                                     
	;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.  See the        
	;GNU General Public License for more details.                         
	;                                                                     
	;You should have received a copy of the GNU General Public License along
	;with this program; if not, write to the Free Software Foundation, Inc.,
	;51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.  
V	; Note: Snapshot file .84 before this run so that
	;        it might be restored completely, if needed.
	N CODE,EX,I,J,K,LL,RRCODE,RRCFILE,RRCIEN,RRCNODE,MSK
	S (CODE,RRCODE(1,0))="$$EZBLD^DIALOG("
	S MSK=$$NAMEMSK(),RRCIEN=2150000000
	Q:MSK=""
	;
	S EX=0
	S J=RRCIEN-1
	F  S J=$O(^DI(.84,J)) Q:('J)!(J>2159999999)  D
	. S I=$G(^DI(.84,J,0)),S42=$P(I,"^",3)
	. K ^DI(.84,J),^DI(.84,"B",J)
	. ; Dump all generated xrefs
	. K:$L(S42) ^DI(.84,"D",S42,J)
	.QUIT
	D CLEAN
	S TRGDIR="/tmp/I18N/r/"
	;  Clear the decks
	ZSY "mkdir -p "_TRGDIR
	ZSY "rm -f "_TRGDIR_"[A-G]*.m"
	ZSY "rm -f "_TRGDIR_"[G-K]*.m"
	ZSY "rm -f "_TRGDIR_"[K-M]*.m"
	ZSY "rm -f "_TRGDIR_"[M-P]*.m"
	ZSY "rm -f "_TRGDIR_"[P-W]*.m"
	ZSY "rm -f "_TRGDIR_"[W-Z]*.m"
	ZSY "rm -f "_TRGDIR_"*.m"
	; D REINDX
	F  D   Q:EX
	. S RRCFILE=$ZSEARCH(MSK)
	. I RRCFILE=""  S EX=1  Q
	. ;
	. N CC,LC,X
	. S (CC,LC)=0
	. S RTN=$P(RRCFILE,"/",$L(RRCFILE,"/"))
	. O RRCFILE:READ
	. S RRCNUFL=TRGDIR_RTN
	. S RTN=$P(RTN,".")
	. O RRCNUFL:NEW
	. U RRCFILE
	. F LL=1:1 R X Q:'$L(X)  D
	. . N NL
	. . S NL=""
	. . S CC=CC+$L(X),LC=LC+1
	. . S X=$$PULLSTR(X)
	. . U RRCNUFL
	. . W X,!
	. . U RRCFILE
	. .QUIT
	. C RRCNUFL
	. C RRCFILE
	. U 0
	. W RTN_":"_LC_":"_CC_" "
	. W:$X>65 !
	.QUIT
	QUIT
	;  ============
NAMEMSK()	; Request the File Search Seed, ie. r/*.m
	N M
	R !,"Enter the file mask to include: ",M,!
	QUIT M
	;  ============
	; Note:
	;  I L?3.A  <== Picked up a lot of the DIC(0)=Setups
	;  
PULLSTR(LN)	; Look for strings
	N J,K,L,LX,L42,RL,NL
	S NL=""
	F J=2:2:$L(LN,"""") D
	. S NL=NL_$P(LN,"""",J-1)_""""
	. S (L,LX)=$P(LN,"""",J)
	. S L42=$E(L,1,42)
	. D:$L(L42)
	. . I '$D(^VWDI(250.84,"B",L42))  D   Q  ; Check the Exclusions
	. . . I L'?1"["1U.(1" ",1.UNP).1"]",L?.E2.A1." "2.A.E D MOD Q
	. . . ; I L?3.A                       D MOD   Q
	. . . I L?3.(1" "1AP)                 D MOD   Q
	. . . I L?2.(1.2AN1":"1.AP1";")       D MOD   Q
	. . . I L?1U1.A.1(1" ",1":",1";").E   D MOD   Q
	. . .QUIT
	. . ;
	. . ; Now if no condition found, add string into NL
	. . S NL=NL_L_""""
	. .QUIT
	.QUIT
	S K=$P(LN,"""",J+1)
	S:K'="" NL=NL_K
	S:NL="" NL=LN
	QUIT NL
	;  ============
MOD	; Modify the Line of Code
	; D
	; . I $L(L)>60 S L=$E(L,1,60),^RRCSTR(L,RRCFILE,LL,J,"STR")=LX Q
	; . ;
	; . S ^RRCSTR(L,RRCFILE,LL,J)=""
	; .QUIT
	; Get or build a new IEN for this string and return in RL
	S RL=$$RESOLVE(LX)
	S:$E(NL,$L(NL))="""" NL=$E(NL,1,$L(NL)-1)
	S NL=NL_CODE_RL_")"
	QUIT
	;  ============
RESOLVE(ST)	; Resolve the String as being in DIALOG or add it.
	N J,K,L,S42,S60
	S S60=ST
	S:$L(ST)>59 S60=$E(ST,1,60)
	S S42=$E(S60,1,42)
	S J=$O(^DI(.84,"D",S42,""))
	D:J=""
	. S (RRCIEN,J)=RRCIEN+1
	. S ^DI(.84,J,0)=J_"^1^"_S42
	. S ^DI(.84,J,2,0)="^.844^1^1"
	. S ^DI(.84,J,2,1,0)=ST
	. S ^DI(.84,"B",J,J)=""
	. S ^DI(.84,"D",S42,J)=RTN
	.QUIT
	S D1=$O(^DI(.84,J,5,"B",RTN,""))
	D:'D1
	. S D1=$P($G(^DI(.84,J,5,0)),"^",3)+1
	. S ^DI(.84,J,5,0)="^.841^"_D1_"^"_D1
	.QUIT
	S ^DI(.84,J,5,D1,0)=RTN_"^"_LL
	S ^DI(.84,J,5,"B",RTN,D1)=""
	QUIT J
	;  ============
CLEAN	; Check the "D" Cross Reference and Clear Inconsistancies
	N I,J,K,L,M
	S I=""
	F  S I=$O(^DI(.84,"D",I))     Q:I=""  D
	. S J=""
	. F  S J=$O(^DI(.84,"D",I,J)) Q:J=""  D
	. . S K=$P($G(^DI(.84,J,0)),"^",3)
	. . I I'=K   K ^DI(.84,"D",I,J)
	. .QUIT
	.QUIT
	QUIT
	;  ===========


