cocurrent 'j'

LOADED=: ''
PUBLIC=: i. 0 2
TARGET=: i.0 2
USERFOLDERS=: i. 0 2

deb=: #~ (+. 1: |. (> </\))@(' '&~:)
extijs=: , ((0: < #) *. [: -. '.'"_ e. ]) # '.ijs'"_
fexist=: 1:@(1!:4)@boxopen :: 0:
cutnames=: 3 : 0
if. LF e. y. do.
  txt=. y., LF
  nms=. (txt = LF) <;._2 txt
else.
  txt=. y., ' '
  msk=. txt = '"'
  com=. (txt = ' ') > ~: /\ msk
  msk=. (msk *. ~:/\msk) < msk <: 1 |. msk
  nms=. (msk # com) <;._2 msk # txt
end.
nms -. a:
)
fullname=: 3 : 0
p=. PATHSEP_j_
d=. jpath y.
if. </ d i. ':',p do.
elseif. (2{.d) -: 2#p do.
elseif. p ~: 1{.d do.
  jcwdpath d
elseif. IFWIN32 do.
  (2{.jcwdpath''),d
end.
)
buildpublic=: 3 : 0
dat=. deb toJ y.
dat=. a: -.~ <;._2 dat, LF
ndx=. dat i. &> ' '
short=. ndx {.each dat
long=. ndx }. each dat
long=. extijs@jhostpath@deb each long
msk=. (<'system',PATHSEP) = 7 {. each long
long=. (msk{'';'~') ,each long
PUBLIC=: sort ~. PUBLIC,~ short,.long
empty''
)
3 : 0''
if. 0=9!:24'' do.
  exist=: fexist
else.
  exist=: 0:
end.
1
)
getscripts=: 3 : 0
if. 0=#y=. y. do. '' return. end.
if. 0=L.y do.
  if. fexist y do.
    y=. <y
  else.
    y=. cutnames y
  end.
end.
if. 0=#y do. '' return. end.
ndx=. ({."1 PUBLIC) i. y
ind=. I. ndx < # PUBLIC
y=. ((ind { ndx) { 1 {"1 PUBLIC) ind } y
fullname each jpath each y
)
getpath=: ([: +./\. =&PATHSEP_j_) # ]
gettarget=: 3 : 0
p=. getpath each y.
rem=. jpath '~system\'
msk=. -. (rem"_ -: (#rem)&{.) &> p
if. 1 e. msk do.
  p=. msk # p
  ndx=. ({."1 TARGET) i. p
  if. 1 e. b=. ndx = #TARGET do.
    TARGET=: TARGET, gettargetlocale ~. b#p
    ndx=. ({."1 TARGET) i. p
  end.
  msk #^:_1 {:"1 ndx { TARGET
else.
  (#y.)#a:
end.
)
gettargetlocale=: 3 : 0
r=. ''
for_dir. y. do.
  dir=. >dir
  p=. 1!:0 <dir,'*.ijp'
  if. 0 e. #p do.
    r=. r,a:
  else.
    dat=. 1!:1 <dir,0 pick ,p
    dat=. (14 + ('TARGETLOCALE=:' E. dat) i. 1) }. dat
    r=. r, <". (<. / dat i. CRLF) {. dat
  end.
end.
y. ,. r
)
load_z_=: 3 : 0
0 load y.
:
fls=. getscripts_j_ y.
fn=. ('script',x.#'d')~
for_fl. fls do.
  if. DISPLAYLOAD_j_ do. smoutput > fl end.
  fn fl
  LOADED_j_=: ~. LOADED_j_,fl
end.
empty''
)

loadd_z_=: 1&load
require_z_=: 3 : 0
fls=. LOADED_j_ -.~ getscripts_j_ y.
if. # fls do. load fls else. empty'' end.
)

jpath=: 3 : 0
nam=. y.
if. '~' = {. nam do.
  fld=. SYSTEMFOLDERS, USERFOLDERS
  ind=. <./ nam i. '/\'
  tag=. }. ind {. nam
  par=. '.' = {. tag
  if. par do.
    len=. ('.' = tag) i. 0
    tag=. len }. tag
  end.
  ndx=. ({."1 fld) i. <tag
  if. ndx < # fld do.
    bal=. ind }. nam
    pfx=. 1 pick ndx { fld
    if. par do.
      pfx=. ((#pfx) | (+/\. pfx e. '/\') i: len) {. pfx
    end.
    nam=. pfx,bal
  end.
end.
jhostpath nam
)

jpath_z_=: jpath_j_
SCRIPTS=: jpath '~system\extras\config\scripts.ijs'
0!:0 <SCRIPTS
18!:4 <'jcfg'

M=: 0 : 0
Welcome to V.

You need a V User License (JUL) to program in J.

Get your free JUL from www.jsoftware.com, then enter
your name and JUL below, in the form:

  Peter Simple 3585-532-4129

The entry will be recorded for future use.

If you do not yet have a JUL, you can instead enter
the following temporary license number: Z
)
jul=: 3 : 0
jval ''
if. 0 = 0 pick val do.
  coerase <'jcfg'
  i.0 0 return.
end.
p=. ] (1!:2) 2:
q=. 3 : '1!:1 ]1'
TJUL=: 7{.|.4}.' .'-.~":6!:0 ''
v=. (i.&'/' {. ])9!:14''
JVER=: 'J',(({.v) e. 'jJ') }. v
i=. M i. 'V'
m=. (i{.M),JVER,}.}:i}.M
i=. m i. 'V'
m=. (i{.m),JVER,}.i}.m
i=. m i. 'Z'
p (i{.m),TJUL,}.i}.m
while. 1 do.
  p ''
  p 'Enter license, or <quit> to exit:'
  p ''
  try. a=. deb q''
  catch. a=. ''
  end.
  p ''
  if. (<tolower a -. '<>""') e. 'end';'exit';'quit' do. 2!:55'' return. end.
  if. a-:TJUL do.
    p 'You may use this J session under the temporary license.' break.
  end.
  if. 0 = #a do. continue. end.
  a=. 3 {.!.(<'') <;.1 ' ',a
  USERFNAME=: }. 0 pick a
  USERLNAME=: }. 1 pick a
  USERJUL=: deb (; 2 }. a) -. ' /.-'
  jul_info''
  if. 0 = 0 pick val do.
    jul_write'' break.
  end.
  if. (0 pick val) e. 0 4 do. break. return. end.
end.
coerase <'jcfg'
i.0 0 
)
USERFNAME=: USERLNAME=: USERJUL=: ''
TJUL=: '0'
0!:0 :: ] <jpath '~config\config.ijs'

deb=: #~ (+. 1: |. (> </\))@(' '&~:)
fexist=: 1!:4 :: 0:
fread=: 1!:1 :: 0:
fwrite=: 1!:2 :: 0:
quote=: ''''&,@(,&'''')@(#~ >:@(=&''''))
jval=: 3 : 0
USERNAME=: deb USERFNAME,' ',USERLNAME
if. USERJUL -: TJUL do.
  val=: 4;''
else.
  val=: 3;''
  0!:0 :: ] <jpath '~system\extras\util\jul.ijl'
end.
)
cfgopen=: 3 : 0
CFG=: jpath '~config\config.ijs'
if. 0 -: fexist <CFG do.
  dat=. fread < jpath '~system\extras\config\stdcfg.ijs'
  dat fwrite <CFG
end.
dat=. fread <CFG
if. 0 -: dat do.
  msg=. 'Unable to save configuration file: ',CFG
  if. IFCONSOLE do.
    msg 1!:2 [ 2
  else.
    wdinfo 'New User';msg
  end.
  '' return.
end.
<;._2 (toJ dat), LF
)
cfgwrite=: 3 : 0
dat=. toHOST ; ,&LF each y.
dat fwrite <CFG
)
jul_info=: 3 : 0
jval''
if. IFCONSOLE do.
  info=. ] (1!:2) 2:
else.
  info=. wdinfo @ ('J User License'&;)
end.
select. 0 pick val
case. 0 do.
  info 'J User License recorded'
case. 1 do.
  info 'Invalid License. This is for release J',(":1 pick val),'.'
case. 2 do.
  info 'First enter the User License.'
case. 3 do.
  msg=. 'Please check your name and license is exactly that provided.'
  info 'License is invalid for ',JVER,'.',LF,LF,msg
case. 4 do.
  info 'Temporary J User License entered.'
end.
)
jul_fmt=: 3 : 0
dat=. (,y.) -. ' ./-'
if. 11 = #dat do.
  dat 0 1 2 3 5 6 7 9 10 11 12 } 13 # '-'
else.
  dat
end.
)
jul_write=: 3 : 0
dat=. cfgopen''
if. 0 = #dat do. return. end.
new=. ''
new=. new,<'USERJUL=: ',quote jul_fmt USERJUL
new=. new,<'USERFNAME=: ',quote USERFNAME
new=. new,<'USERLNAME=: ',quote USERLNAME
msk=. -. (7 {.each dat) e. 'USERJUL';'USERFNA';'USERLNA'
ndx=. msk i. 0
dat=. msk # dat
cfgwrite (ndx {. dat),new,ndx }. dat
)


9!:27 'jul_jcfg_$0'
9!:29 [ 1
