1cocurrent 'jpacman'
2coinsert 'j'
3
4BASELIB=: 'base library'
5DATAMASK=: 0
6HWNDP=: ''
7ISGUI=: 0
8ONLINE=: 0
9PKGDATA=: 0 7$a:
10SECTION=: ,<'All'
11SYSNAME=: 'Package Manager'
12TIMEOUT=: 60
13WWWREV=: REV=: _1
14
15IgnoreIOS=: 0 : 0
16api/jni
17data/dbman
18data/ddmysql
19data/odbc
20demos/isigraph
21demos/wd
22demos/wdplot
23games/minesweeper
24games/nurikabe
25games/pousse
26games/solitaire
27general/pcall
28general/sfl
29graphics/d3
30graphics/fvj3
31graphics/gl2
32graphics/gnuplot
33graphics/graph
34graphics/graphviz
35graphics/jturtle
36graphics/print
37graphics/tgsj
38graphics/treemap
39graphics/viewmat
40gui/monthview
41gui/util
42ide/qt
43math/tabula
44media/animate
45media/gdiplus
46media/image3
47media/ming
48media/paint
49media/wav
50)
51
52Ignore=: 3 : 0''
53if. IFIOS do.
54  <;._2 IgnoreIOS
55else.
56  <'ide/ios'
57end.
58)
593 : 0''
60nc=. '--no-cache'
61if. IFUNIX do.
62  if. UNAME-:'Darwin' do.
63    HTTPCMD=: 'curl -o %O --stderr %L -f -s -S %U'
64  elseif. do.
65    if. 'Android'-:UNAME do. nc=. ''
66    else. try. nc=. nc #~ 1 e. nc E. shell 'wget --help' catch. nc=. '' end. end.
67    HTTPCMD=: 'wget ',nc,' -O %O -o %L -t %t %U'
68  end.
69else.
70  if. fexist exe=. jpath '~tools/ftp/wget.exe' do. exe=. '"',exe,'"' else. exe=. 'wget.exe' end.
71  try. nc=. nc #~ 1 e. nc E. shell exe,' --help' catch. nc=. '' end.
72  HTTPCMD=: exe,' ',nc,' -O %O -o %L -t %t -T %T %U'
73  if. fexist UNZIP=: jpath '~tools/zip/unzip.exe' do. UNZIP=: '"',UNZIP,'" -o -C ' else. UNZIP=: 'unzip.exe -o -C ' end.
74end.
75)
76setfiles=: 3 : 0
77ADDCFG=: jpath '~addons/config/'
78makedir ADDCFG
79ADDCFGIJS=: ADDCFG,'config.ijs'
80JRELEASE=: ({.~i.&'/') 9!:14''
81JRELEASE=: 'j802'
82LIBTREE=: readtree''
83if. IFIOS do.
84  WWW=: '/jal/',JRELEASE,'/'
85else.
86  WWW=: 'http://www.jsoftware.com/jal/',JRELEASE,'/'
87end.
88LIBVER=: jpath '~system/config/version.txt'
89)
90destroy=: codestroy
91CFGFILES=: <;._2 (0 : 0)
92addons.txt
93library.txt
94release.txt
95revision.txt
96zips.txt
97)
98LIBDESC=: 0 : 0
99This is the base library of scripts and labs included in the J system.
100
101Reinstalling or upgrading this library will overwrite files in the system subdirectory. Restart J afterwards.
102
103Files outside the system subdirectory, such as profile.ijs, are not changed.
104)
105cutjal=: ([: (* 4 > +/\) ' ' = ]) <;._1 ]
106cutjsp=: ([: (* 5 > +/\) ' ' = ]) <;._1 ]
107dquote=: '"'&, @ (,&'"')
108fname=: #~ ([: *./\. ~:&'/')
109hostcmd=: [: 2!:0 '(' , ] , ' || true)'"_
110ischar=: 2 = 3!:0
111rnd=: [ * [: <. 0.5 + %~
112sep2under=: '/' & (I.@('_' = ])})
113termLF=: , (0 < #) # LF -. {:
114todel=: ; @: (DEL&, @ (,&(DEL,' ')) each)
115tolist=: }. @ ; @: (LF&,@,@":each)
116isjpkgout=: ((4 = {:) *. 2 = #)@$ *. 1 = L.
117getintro=: ('...' ,~ -&3@[ {. ])^:(<#)
118info=: smoutput
119getnames=: 3 : 0
120select. L.y
121case. 0 do.
122  if. +/ BASELIB E. y do.
123    y=. (<BASELIB), cutnames y rplc BASELIB;''
124  else.
125    y=. cutnames y
126  end.
127case. 1 do.
128  if. 2 = #$y do.
129    y=. {."1 y
130  else.
131    y=. ,y
132  end.
133case. do.
134  '' return.
135end.
136y
137)
138curtailcaption=: 3 : 0
139idx=. <_1;~I. 45<#&>{:"1 y
140y=. (45&getintro &.> idx{y) idx}y
141)
142deltree=: 3 : 0
143try.
144  res=. 0< ferase {."1 dirtree y
145  *./ res,0<ferase |.dirpath y
146catch. 0 end.
147)
148fixjal=: 3 : 0
149if. 2 > #y do. i.0 5 return. end.
150m=. _2 |. (LF,')',LF) E. y
151r=. _2 }. each m <;._2 y
152x=. r i.&> LF
153d=. (x+1) }.each r
154r=. x {.each r
155r=. 3 {."1 cutjal &> ' ' ,each r
156x=. d i.&> LF
157c=. x {.each d
158d=. (x+1) }.each d
159r,.c,.d
160)
161fixjal2=: 3 : 0
162if. 2 > #y do. i.0 2 return. end.
163cutjal &> ' ' ,each <;._2 y
164)
165fixjsp=: 3 : 0
166if. 2 > #y do. i.0 5 return. end.
167m=. _2 |. (LF,')',LF) E. y
168r=. _2 }. each m <;._2 y
169x=. r i.&> LF
170d=. (x+1) }.each r
171r=. x {.each r
172r=. ' ' ,each r
173(cutjsp &> r),.d
174)
175fixlib=: 3 : 0
176msk=. (<LIBTREE) = 1 {"1 y
177if. -. 1 e. msk do. ($0);'';0 return. end.
178'ver fln siz'=. 2 4 5 { (msk i.1) { y
179ver=. fixver ver
180ver;fln;siz
181)
182fixlibs=: 3 : 0
183if. 2 > #y do.
184  i.0 6 return.
185end.
186fls=. <;._2 y
187ndx=. fls i.&> ' '
188siz=. <&> 0 ". (ndx+1) }.&> fls
189fls=. ndx {.each fls
190zps=. <;._2 &> fls ,each '_'
191pfm=. 3 {"1 zps
192uname=. tolower UNAME
193msk=. (uname -: ({.~ i.&'.')) &> pfm
194if. 1 ~: +/msk do. msk=. 1,~ }:0*.msk end.
195msk # zps,.fls,.siz
196)
197fixrev=: 3 : 0
198{. _1 ". :: _1: y -. CRLF
199)
200fixupd=: 3 : 0
201_1 ". :: _1: y -. CRLF
202)
203fixver=: 3 : 0
204if. ischar y do.
205  y=. y -. CRLF
206  y=. 0 ". ' ' (I. y='.') } y
207end.
2083 {. y
209)
210fixvers=: 3 : 0
211s=. $y
212y=. ,y
2133 {."1 [ 0 ". s $ ' ' (I. y e. './') } y
214)
215fmtjal=: 3 : 0
216if. 0 = #y do. '' return. end.
217r=. (4 {."1 y) ,each "1 '  ',LF2
218r=. <@; "1 r
219; r ,each ({:"1 y) ,each <')',LF
220)
221fmtjal2=: 3 : 0
222if. 0 = #y do. '' return. end.
223; (2 {."1 y) ,each "1 ' ',LF
224)
225fmtdep=: 3 : 0
226}. ; ',' ,each a: -.~ <;._2 y
227)
228fmtjsp=: 3 : 0
229if. 0 = #y do. '' return. end.
230r=. (4 {."1 y) ,each "1 '   ',LF
231r=. <@; "1 r
232; r ,each ({:"1 y) ,each <')',LF
233)
234fmtlib=: 3 : 0
235, 'q<.>,q<.>r<0>3.0,r<0>3.0' 8!:2 y
236)
237fmtver=: 3 : 0
238if. 0=#y do. '' return. end.
239if. ischar y do. y return. end.
240}. ; '.' ,each ": each y
241)
242fmtverlib=: 3 : 0
243fmtver y
244)
245fixzips=: 3 : 0
246if. 2 > #y do. i.0 5 return. end.
247fls=. <;._2 y
248ndx=. fls i.&> ' '
249siz=. 0 ". (ndx+1) }.&> fls
250fls=. ndx {.each fls
251zps=. <;._2 &> fls ,each '_'
252zps=. zps,.fls,.<&>siz
253pfm=. 3 {"1 zps
254and=. (1 e. 'android'&E.) &> pfm
255lnx=. (1 e. 'linux'&E.) &> pfm
256mac=. (1 e. 'darwin'&E.) &> pfm
257win=. mac < (1 e. 'win'&E.) &> pfm
258
259select. UNAME
260case. 'Win' do.
261  zps=. win # zps
262case. 'Linux' do.
263  zps=. lnx # zps
264case. 'Android' do.
265  zps=. and # zps
266case. 'Darwin' do.
267  zps=. mac # zps
268  zps=. zps /: 3 {"1 zps
269  zps=. (~: 3 {."1 zps) # zps
270end.
271
272bit=. IF64 pick '64';'32'
273pfm=. 3 {"1 zps
274exc=. (1 e. bit&E.) &> pfm
275zps=. zps \: exc
276zps=. (~: 3 {."1 zps) # zps
277fnm=. 0 {"1 zps
278lnm=. 1 {"1 zps
279ver=. 2 {"1 zps
280pfm=. 3 {"1 zps
281fls=. 4 {"1 zps
282siz=. 5 {"1 zps
283nms=. fnm ,each '/' ,each lnm
284pfm=. (pfm i.&> '.') {.each pfm
285ndx=. \: # &> pfm
286sort ndx { nms,.pfm,.ver,.fls,.siz
287)
288fwritenew=: 4 : 0
289if. x -: fread y do.
290  0
291else.
292  x fwrite y
293end.
294)
295platformparent=: 3 : 0
296((< _2 {. y) e. '32';'64') # _2 }. y
297)
298makedir=: 1!:5 :: 0: @ <
299plural=: 4 : 0
300y,(1=x)#'s'
301)
302sizefmt=: 3 : 0
303select. +/ y >: 1e3 1e4 1e6 1e7 1e9
304case. 0 do.
305  (": y), ' byte',(y~:1)#'s'
306case. 1 do.
307  (": 0.1 rnd y%1e3),' KB'
308case. 2 do.
309  (": 1 rnd y%1e3),' KB'
310case. 3 do.
311  (": 0.1 rnd y%1e6),' MB'
312case. 4 do.
313  (": 1 rnd y%1e6),' MB'
314case. do.
315  (": 0.1 rnd y%1e9),' GB'
316end.
317)
318shellcmd=: 3 : 0
319if. IFUNIX do.
320  hostcmd y
321else.
322  spawn_jtask_ y
323end.
324)
325subdir=: 3 : 0
326if. 0=#y do. '' return. end.
327a=. 1!:0 y,'*'
328if. 0=#a do. '' return. end.
329a=. a #~ '-d' -:"1 [ 1 4 {"1 > 4 {"1 a
330(<y) ,each ({."1 a) ,each '/'
331)
332testaccess=: 3 : 0
333f=. <jpath'~install/testaccess.txt'
334try.
335  '' 1!:2 f
336  1!:55 f
337  1
338catch.
339  0
340end.
341)
342toupper1=: 3 : 0
343if. 0=#y do. '' return. end.
344(toupper {. y),tolower }. y
345)
346unzip=: 3 : 0
347'file dir'=. dquote each y
348e=. 'Unexpected unzip error'
349if. IFUNIX do.
350  notarcmd=. 0
351  if. IFIOS +. UNAME-:'Android' do.
352    notarcmd=. _1-: 2!:0 ::_1: 'which tar'
353    if. (UNAME-:'Android') > '/mnt/sdcard'-:2!:5'EXTERNAL_STORAGE' do. notarcmd=. 1 end.
354  end.
355  if. notarcmd do.
356    require 'tar'
357    'file dir'=. y
358    if. (i.0 0) -: tar 'x';file;dir do. e=. '' end.
359  else.
360    e=. shellcmd 'tar ',((IFIOS+:UNAME-:'Android')#(('Darwin'-:UNAME){::'--no-same-owner --no-same-permissions';'-o -p')),' -xzf ',file,' -C ',dir
361  end.
362  if. (0~:FHS) *. ('root'-:2!:5'USER') +. (<2!:5'HOME') e. 0;'/var/root';'/root';'';,'/' do.
363    shellcmd ::0: 'find ',dir,' -type d -exec chmod a+rx {} \+'
364    shellcmd ::0: 'find ',dir,' -type f -exec chmod a+r {} \+'
365  end.
366else.
367  dir=. (_2&}. , '/' -.~ _2&{.) dir
368  e=. shellcmd UNZIP,' ',file,' -d ',dir
369end.
370e
371)
372zipext=: 3 : 0
373y, IFUNIX pick '.zip';'.tar.gz'
374)
375CHECKADDONSDIR=: 0 : 0
376The addons directory does not exist and cannot be created.
377
378It is set to: XX.
379
380You can either create the directory manually, or set a new addons directory in your profile script.
381)
382CHECKASK=: 0 : 0
383Read catalog from the server using Internet connection now?
384
385Otherwise the local catalog is used offline.
386)
387CHECKONLINE=: 0 : 0
388An active Internet connection is needed to install packages.
389
390Continue only if you have an active Internet connection.
391
392OK to continue?
393)
394CHECKREADSVR=: 0 : 0
395An active Internet connection is needed to read the server repository catalog.
396
397Continue only if you have an active Internet connection.
398
399OK to continue?
400)
401CHECKSTARTUP=: 0 : 0
402Setup repository using Internet connection now?
403
404Select No if not connected, to complete setup later. After Setup is done, repository can be used offline with more options in Tools menu and Preferences dialog.
405)
406checkaccess=: 3 : 0
407if. testaccess'' do. 1 return. end.
408msg=. 'Unable to run Package Manager, as you do not have access to the installation folder.'
409if. IFWIN do.
410  msg=. msg,LF2,'To run as Administrator, right-click the J icon, select Run as... and '
411  msg=. msg,'then select Adminstrator.'
412end.
413info msg
4140
415)
416checkaddonsdir=: 3 : 0
417d=. jpath '~addons'
418if. # 1!:0 d do. 1 return. end.
419if. 1!:5 :: 0: <d do.
420  log 'Created addons directory: ',d
421  1 return.
422end.
423info CHECKADDONSDIR rplc 'XX';d
4240
425)
426getonline=: 3 : 0
427ONLINE=: 2=3 2 wdquery y
428)
429getserver=: 3 : 0
430'rc p'=. httpgetr (WWW,'revision.txt');2
431if. rc do. 0 return. end.
432write_lastupdate''
433WWWREV=: fixrev p
434if. WWWREV = REV do. 1 return. end.
435refreshweb''
436)
437checkonline=: 3 : 0
438select. ReadCatalog_j_
439case. 0 do.
440  if. REV >: 0 do.
441    ONLINE=: 0
442    log 'Using local copy of catalog. See Preferences to change the setting.'
443    1 return.
444  end.
445  if. 0 = getonline 'Read Catalog from Server';CHECKREADSVR do. 0 return. end.
446case. 1 do.
447  ONLINE=: 1
448case. 2 do.
449  if. REV >: 0 do.
450    if. 0 = getonline 'Read Catalog from Server';CHECKASK do.
451      log 'Using local copy of catalog. See Preferences to change the setting.'
452      1 return.
453    end.
454  else.
455    if. 0 = getonline 'Setup Repository';CHECKSTARTUP do. 0 return. end.
456  end.
457end.
458log 'Updating server catalog...'
459if. 0 = getserver'' do.
460  ONLINE=: 0
461  log 'Working offline using local copy of catalog.'
462else.
463  log 'Done.'
464end.
4651
466)
467checkstatus=: 3 : 0
468if. 0 e. #LIBS do. '' return. end.
469msk=. masklib PKGDATA
470ups=. pkgups''
471libupm=. 1 e. msk *. ups
472msk=. -. msk
473addnim=. +/msk *. pkgnew''
474addupm=. +/msk *. pkgups''
475tot=. +/addnim,addupm,libupm
476if. 0 = tot do.
477  'All available packages are installed and up to date.' return.
478end.
479select. 0 < addnim,addupm
480case. 0 0 do.
481  msg=. 'Addons are up to date.'
482case. 0 1 do.
483  msg=. 'All addons are installed, ',(":addupm), ' can be upgraded.'
484case. 1 0 do.
485  if. addnim = <:#PKGDATA do.
486    msg=. 'No addons are installed.'
487  else.
488    j=. ' addon',('s'#~1<addnim),' are not yet installed.'
489    msg=. 'Installed addons are up to date, ',(":addnim),j
490  end.
491case. 1 1 do.
492  j=. (":addupm),' addon',('s'#~1<addupm),' can be upgraded, '
493  msg=. j,(":addnim), ' addon',('s'#~1<addnim),' are not yet installed.'
494end.
495if. 0 = libupm do.
496  msg,LF,'The base library is up to date.'
497else.
498  msg,LF,'There is a newer version of the base library.'
499end.
500)
501write_lastupdate=: 3 : 0
502txt=. ": 6!:0 ''
503txt fwrites ADDCFG,'lastupdate.txt'
504)
505checklastupdate=: 3 : 0
506if. _1 -: LASTUPD do.
507  res=. 'has never been updated.'
508else.
509  res=. 'was last updated: ',timestamp LASTUPD
510end.
511'Local JAL information ',res
512)
513getdepend=: 3 : 0
514if. 0 = #y do. y return. end.
515dep=. getdepend_console 1{"1 y
516PKGDATA #~ (1{"1 PKGDATA) e. dep
517)
518getdepend_console=: 3 : 0
519if. 0 = #y do. y return. end.
520old=. ''
521ids=. 1{"1 PKGDATA
522dep=. 6{"1 PKGDATA
523res=. ~. <;._1 ; ',' ,each (ids e. y) # dep
524whilst. -. res-:old do.
525  old=. res
526  res=. ~. res, <;._1 ; ',' ,each (ids e. res) # dep
527end.
528~. y, res -. a:, {."1 ADDINS
529)
530httpget=: 3 : 0
531'f t'=. 2 {. (boxxopen y),a:
532n=. f #~ -. +./\. f e. '=/'
533p=. jpath '~temp/',n
534q=. jpath '~temp/httpget.log'
535t=. ":{.t,3
536ferase p;q
537fail=. 0
538cmd=. HTTPCMD rplc '%O';(dquote p);'%L';(dquote q);'%t';t;'%T';(":TIMEOUT);'%U';f
539if. IFIOS +. UNAME-:'Android' do.
540  require 'socket'
541  1!:55 ::0: <p
542  rc=. 0 [ e=. pp=. ''
543  whilst. 0 do.
544    'rc sk'=. sdsocket_jsocket_''
545    if. 0~:rc do. break. end.
546    rc=. sdconnect_jsocket_ sk;PF_INET_jsocket_;'23.21.67.48';80
547    if. 0~:rc do. break. end.
548    'rc sent'=. ('GET ',f,' HTTP/1.0',LF2) sdsend_jsocket_ sk;0
549    if. 0~:rc do. break. end.
550    while. ((0=rc)*.(*#m)) [[ 'rc m'=. sdrecv_jsocket_ sk,4096 do.
551      pp=. pp,m
552    end.
553  end.
554  sdclose_jsocket_ sk
555  if. 0~:rc do. fail=. 1
556  elseif. 1 -.@e. '200 OK' E. (20{.pp) do. fail=. 1 [ e=. ({.~ i.&LF) pp
557  elseif. #p1=. I. (CRLF,CRLF) E. 500{.pp do. p2=. 4
558  elseif. #p1=. I. LF2 E. 500{.pp do. p2=. 2
559  elseif. do. fail=. 1
560  end.
561  if. 0=fail do.
562    ((p2+{.p1)}.pp) 1!:2 <p
563  else.
564    if. 0~:rc do. e=. sderror_jsocket_ rc end.
565  end.
566elseif. do.
567  try.
568    e=. shellcmd cmd
569  catch. fail=. 1 end.
570end.
571if. fail +. 0 >: fsize p do.
572  if. _1-:msg=. freads q do.
573    if. 0=#msg=. e do. msg=. 'Unexpected error' end. end.
574  log 'Connection failed: ',msg
575  info 'Connection failed:',LF2,msg
576  r=. 1;msg
577  ferase p;q
578else.
579  r=. 0;p
580  ferase q
581end.
582r
583)
584httpgetr=: 3 : 0
585res=. httpget y
586if. 0 = 0 pick res do.
587  f=. 1 pick res
588  txt=. freads f
589  ferase f
590  0;txt
591end.
592)
593install=: 3 : 0
594dat=. getdepend y
595'num siz'=. pmview_applycounts dat
596many=. 1 < num
597msg=. 'Installing ',(":num),' package',many#'s'
598msg=. msg,' of ',(many#'total '),'size ',sizefmt siz
599log msg
600installdo 1 {"1 dat
601log 'Done.'
602readlocal''
603pacman_init 0
604)
605install_console=: 3 : 0
606  if. -. init_console 'server' do. '' return. end.
607  pkgs=. getnames y
608  if. pkgs -: ,<'all' do. pkgs=. 1 {"1 PKGDATA end.
609  pkgs=. pkgs (e. # [) ~. (<'base library'), ((pkgnew +. pkgups) # 1&{"1@]) PKGDATA
610  pkgs=. pkgs -. Ignore
611  pkgs=. getdepend_console pkgs
612  if. 0 = num=. #pkgs do. '' return. end.
613  many=. 1 < num
614  msg=. 'Installing ',(":num),' package',many#'s'
615  log msg
616  installdo pkgs
617  log 'Done.'
618  readlocal''
619  pacman_init ''
620  checkstatus''
621)
622upgrade_console=: 3 : 0
623  if. -. init_console 'read' do. '' return. end.
624  pkgs=. getnames y
625  if. (0=#pkgs) +. pkgs -: ,<'all' do. pkgs=. 1{"1 PKGDATA end.
626  pkgs=. pkgs (e. # [) (pkgups # 1&{"1@])PKGDATA
627  install_console pkgs
628)
629installdo=: 3 : 0
630msk=. -. y e. <BASELIB
631if. 0 e. msk do.
632  install_library''
633end.
634install_addon each msk # y
635)
636install_addon=: 3 : 0
637ndx=. ({."1 ZIPS) i. <y
638if. ndx = #ZIPS do. EMPTY return. end.
639log 'Downloading ',y,'...'
640f=. 3 pick ndx { ZIPS
641'rc p'=. httpget WWW,'addons/',f
642if. rc do. return. end.
643log 'Installing ',y,'...'
644msg=. unzip p;jpath'~addons'
645ferase p
646if. 0>:fsize jpath'~addons/',y,'/manifest.ijs' do.
647  log 'Extraction failed: ',msg
648  info 'Extraction failed:',LF2,msg
649  return.
650end.
651install_addins y
652install_config y
653)
654install_addins=: 3 :0
655fl=. ADDCFG,'addins.txt'
656ins=. fixjal2 freads fl
657ins=. ins #~ (<y) ~: {."1 ins
658ndx=. ({."1 ADDONS) i. <y
659ins=. sort ins, 2 {. ndx { ADDONS
660(fmtjal2 ins) fwrites fl
661)
662install_config=: 3 : 0
663ADDLABS=: ''
6640!:0 :: ] < ADDCFGIJS
665install_labs y
666write_config''
667)
668install_labs=: 3 : 0
669labs=. dirtree jpath '~addons/',y,'/*.ijt'
670if. 0=#labs do. return. end.
671pfx=. jpath '~addons/'
672labs=. (#pfx) }.each {."1 labs
673LABCATEGORY=: ''
6740!:0 ::] <jpath '~addons/',y,'/manifest.ijs'
675cat=. LABCATEGORY
676if. 0 = #cat do.
677  cat=. toupper1 (y i. '/') {. y
678end.
679new=. labs ,each <' ',cat
680txt=. sort ~. new,<;._2 ADDLABS
681ndx=. 4 + (1 i.~ '.ijt'&E.) &> txt
682msk=. fexist &> (<pfx) ,each ndx {.each txt
683txt=. msk # txt
684ADDLABS=: ; txt ,each LF
685)
686install_library=: 3 : 0
687log 'Downloading base library...'
688f=. 1 pick LIB
689'rc p'=. httpget WWW,'library/',f
690if. rc do. return. end.
691log 'Installing base library...'
692unzip p;jpath'~system'
693ferase p
694readlin''
695)
696write_config=: 3 : 0
697txt=. 'NB. Addon configuration',LF2
698txt=. txt,'ADDLABS=: 0 : 0',LF,ADDLABS,')',LF
699txt fwrites ADDCFGIJS
700)
701show_console=: 4 : 0
702  if. -. init_console 'read' do. '' return. end.
703  select. x
704  case. 'search' do.
705    pkgs=. getnames y
706    res=. (pkgsearch pkgs) # 1 2 3 4 {"1 PKGDATA
707    res=. curtailcaption res
708  case. 'show' do.
709    pkgs=. getnames y
710    if. pkgs -: ,<'all' do. pkgs=. 1 {"1 PKGDATA end.
711    res=. (msk=. pkgshow pkgs) # 5 {"1 PKGDATA
712    if. #res do.
713      res=. ,((<'== '), &.> msk # 1 {"1 PKGDATA) ,. res
714      res=. (2#LF) joinstring (70&foldtext)&.> res
715    end.
716  case. 'showinstalled' do.
717    dat=. (isjpkgout y) {:: (1 2 3 4 {"1 PKGDATA);<y
718    res=. (-.@pkgnew # ])dat
719    res=. curtailcaption res
720  case. 'shownotinstalled' do.
721    dat=. (isjpkgout y) {:: (1 2 3 4 {"1 PKGDATA);<y
722    res=. (pkgnew # 0 2 3&{"1@])dat
723    res=. curtailcaption res
724  case. 'showupgrade' do.
725    dat=. (isjpkgout y) {:: (1 2 3 4 {"1 PKGDATA);<y
726    res=. (pkgups # ])dat
727    res=. curtailcaption res
728  case. 'status' do.
729    res=. checklastupdate''
730    res=. res,LF,checkstatus''
731  end.
732  res
733)
734showfiles_console=: 4 : 0
735  if. -. init_console 'read' do. '' return. end.
736  pkgs=. getnames y
737  pkgs=. pkgs (e. # [) (-.@pkgnew # 1&{"1@]) PKGDATA
738  pkgs=. pkgs -. <BASELIB
739  if. 0=#pkgs do. '' return. end.
740  fn=. (<'~addons/') ,&.> (pkgs) ,&.> <'/',x,(x-:'history'){::'.ijs';'.txt'
741  res=. res #~ msk=. (<_1) ~: res=. fread@jpath &.> fn
742  if. #res do.
743    res=. ,((<'== '), &.> msk#pkgs) ,. res
744    res=. (2#LF) joinstring res
745  end.
746)
747remove_console=: 3 : 0
748  if. -. init_console 'edit' do. '' return. end.
749  pkgs=. getnames y
750  if. pkgs -: ,<'all' do. pkgs=. 1 {"1 PKGDATA end.
751  pkgs=. pkgs (e. # [) (-.@pkgnew # 1&{"1@]) PKGDATA
752  pkgs=. pkgs -. <BASELIB
753  if. 0 = num=. #pkgs do. '' return. end.
754  many=. 1 < num
755  msg=. 'Removing ',(":num),' package',many#'s'
756  log msg
757  remove_addon each pkgs
758  log 'Done.'
759  readlocal''
760  pacman_init ''
761  checkstatus''
762)
763
764remove_addon=: 3 : 0
765  log 'Removing ',y,'...'
766  treepath=. jpath '~addons/',y
767  if. ((0 < #@dirtree) *. -.@deltree) treepath do.
768    nf=. #dirtree treepath
769    nd=. <: # dirpath treepath
770    nd=. nd + (tolower treepath) e. dirpath jpath '~addons/', '/' taketo y
771    msg=. (":nd),' directories and ',(":nf),' files not removed.'
772    log 'Remove failed: ',msg
773    info 'Remove failed:',LF2,msg
774    return.
775  end.
776  remove_addins y
777  remove_config y
778)
779remove_addins=: 3 :0
780  fl=. ADDCFG,'addins.txt'
781  ins=. fixjal2 freads fl
782  ins=. ins #~ (<y) ~: {."1 ins
783  (fmtjal2 ins) fwrites fl
784)
785remove_config=: 3 : 0
786  ADDLABS=: ''
787  0!:0 :: ] < ADDCFGIJS
788  remove_labs y
789  write_config''
790)
791remove_labs=: 3 : 0
792  txt=. <;._2 ADDLABS
793  txt=. txt #~ (<jpathsep y) ~: (#y)&{. each txt
794  ADDLABS=: ; txt ,each LF
795)
796LOG=: 1
797log=: 3 : 0
798if. LOG do. smoutput y end.
799)
800logstatus=: 3 : 0
801if. ONLINE do.
802  log checkstatus''
803end.
804)
805readlin=: 3 : 0
806LIN=: 6 1 1 >. fixver freads LIBVER
807)
808readlocal=: 3 : 0
809readlin''
810ADDONS=: fixjal freads ADDCFG,'addons.txt'
811ADDINS=: fixjal2 freads ADDCFG,'addins.txt'
812REV=: fixrev freads ADDCFG,'revision.txt'
813LASTUPD=: fixupd freads ADDCFG,'lastupdate.txt'
814LIBS=: fixlibs freads ADDCFG,'library.txt'
815LIB=: fixlib LIBS
816ZIPS=: fixzips freads ADDCFG,'zips.txt'
817EMPTY
818)
819readtree=: 3 : 0
820f=. ADDCFG,'tree.txt'
821tree=. LF -.~ freads f
822if. -. (<tree) e. 'current';'stable' do.
823  tree=. 'current'
824  writetree tree
825end.
826tree
827)
828writetree=: 3 : 0
829y fwritenew ADDCFG,'tree.txt'
830)
831refreshweb=: 3 : 0
832if. 0 = refreshjal'' do. 0 return. end.
833readlocal''
8341
835)
836refreshaddins=: 3 : 0
837ADDLABS=: ''
838f=. ADDCFG,'addins.txt'
839p=. jpath '~addons/'
840sd=. ;subdir each subdir p
841if. 0=#sd do.
842  '' fwrite f
843  write_config'' return.
844end.
845r=. s=. ''
846for_d. sd do.
847  mft=. freads (>d),'manifest.ijs'
848  if. mft -: _1 do. continue. end.
849  VERSION=: ''
850  0!:100 mft
851  ver=. fmtver fixver VERSION
852  n=. }: (#p) }. >d
853  n=. '/' (I.n='\') } n
854  r=. r,n,' ',ver,LF
855  s=. s,d
856end.
857r fwritenew f
858s=. (#p) }.each }: each s
859install_labs each s
860write_config''
861)
862refreshjal=: 3 : 0
863'rc p'=. httpget WWW,zipext 'jal'
864if. rc do. 0 return. end.
865unzip p;ADDCFG
866ferase p
867if. *./ CFGFILES e. {."1 [ 1!:0 ADDCFG,'*' do. 1 return. end.
868msg=. 'Could not install the local repository catalog.'
869log msg
870info msg
8710
872)
873updatejal=: 3 : 0
874  log 'Updating server catalog...'
875  if. -. init_console 'server' do. '' return. end.
876  refreshaddins''
877  readlocal''
878  pacman_init''
879  res=. checklastupdate''
880  res,LF,checkstatus''
881)
882RELIBMSG=: 0 : 0
883You are now using the XX base library, and can switch to the YY base library.
884
885This will download the YY version of the base library and overwrite existing files. Addons are not affected.
886
887OK to switch to the YY library?
888)
889prelib=: 3 : 0
890old=. LIBTREE
891new=. (('stable';'current') i. <old) pick 'current';'beta'
892msg=. RELIBMSG rplc ('XX';'YY'),.old;new
893if. 0 = query SYSNAME;msg do.
894  info 'Not done.' return.
895end.
896switchlibrary 1 pick new
897)
898switchlibrary=: 3 : 0
899'' fwrite LIBVER
900writetree LIBTREE=: y
901refreshjal''
902readlocal''
903pmview_setpn''
904)
905masklib=: 3 : 0
906(1 {"1 y) = <BASELIB
907)
908pkglater=: 3 : 0
909dat=. (s=.isjpkgout y){:: PKGDATA;<y
910if. 0=#dat do. $0 return. end.
911loc=. fixvers > (2-s) {"1 dat
912srv=. fixvers > (3-s) {"1 dat
913{."1 /:"2 srv ,:"1 loc
914)
915pkgnew=: 3 : 0
916dat=. (s=.isjpkgout y){:: PKGDATA;<y
917if. 0=#dat do. $0 return. end.
9180 = # &> (2-s) {"1 dat
919)
920pkgups=: pkgnew < pkglater
921pkgsearch=: 3 : 0
922  +./"1 +./ y E."1&>"(0 _) 1{"1 PKGDATA
923)
924pkgshow=: 3 : 0
925  y e.~ 1{"1 PKGDATA
926)
927setshowall=: 3 : 0
928PKGDATA=: (<y) (<(I.DATAMASK);0) } PKGDATA
929)
930setshownew=: 3 : 0
931ndx=. I. DATAMASK *. pkgnew''
932PKGDATA=: (<y) (<ndx;0) } PKGDATA
933)
934setshowups=: 3 : 0
935ndx=. I. DATAMASK *. pkgups''
936PKGDATA=: (<y) (<ndx;0) } PKGDATA
937)
938splitlib=: 3 : 0
939if. 0=#y do.
940  2 $ <y return.
941end.
942msk=. masklib y
943(msk#y) ; <(-.msk)#y
944)
945pacman_init=: 3 : 0
946dat=. ADDONS #~ ({."1 ADDONS) e. {."1 ZIPS
947if. 0=#dat do.
948  dat=. i.0 6
949else.
950  ndx=. ({."1 ADDINS) i. {."1 dat
951  ins=. ndx { (1 {"1 ADDINS),<''
952  dat=. dat,.<''
953  dat=. 0 5 1 3 4 2 {"1 dat
954  dat=. ins 1 }"0 1 dat
955end.
956lib=. 'base library';(fmtver LIN);(fmtver 0 pick LIB);'base library scripts';LIBDESC;''
957dat=. dat,lib
958dat=. (<0),.dat
959PKGDATA=: sort dat
960nms=. 1 {"1 PKGDATA
961nms=. ~. (nms i.&> '/') {.each nms
962SECTION=: 'All';nms
963DATAMASK=: (#PKGDATA) $ 1
964EMPTY
965)
966init_console=: 3 : 0
967  if. 0=#y do. y=. 'read' end.
968  select. y
969  fcase. 'edit';'server' do.
970    if. -. checkaccess'' do. 0 return. end.
971  case. 'read' do.
972    if. -. checkaddonsdir'' do. 0 return. end.
973    setfiles''
974    readlocal''
975    pacman_init ''
976    res=. 1
977  case. do. res=. 0
978  end.
979  if. y -: 'server' do. res=. getserver''  end.
980  res
981)
982jpkg=: 4 : 0
983  select. x
984  case. 'history';'manifest' do.
985    x showfiles_console y
986  case. 'install' do.
987    install_console y
988  case. 'reinstall' do.
989    remove_console y
990    install_console y
991  case. 'remove' do.
992    remove_console y
993  case. ;:'show search showinstalled shownotinstalled showupgrade status' do.
994    x show_console y
995  case. 'update'  do.
996    updatejal ''
997  case. 'upgrade' do.
998    upgrade_console y
999  case. do.
1000    msg=. 'Valid options are:',LF
1001    msg=. msg,'  history, install, manifest, remove, reinstall, show, search,',LF
1002    msg=. msg,'  showinstalled, shownotinstalled, showupgrade, status,',LF
1003    msg,'  update, upgrade'
1004  end.
1005)
1006do_install=: 3 : 0
1007if. -. checkaccess_jpacman_ '' do. return. end.
1008'update' jpkg ''
1009select. y
1010case. 'qtide';'angle' do.
1011  'install' jpkg 'base library ide/qt'
1012  getqtbin (y-:'angle'){::0;'angle'
1013  msg=. (+/ 2 1 * IFWIN,'Darwin'-:UNAME) pick 'jqt.sh';'the jqt icon';'jqt.cmd'
1014  smoutput 'exit and restart J using ',msg
1015case. 'all' do.
1016  'install' jpkg 'all'
1017  getqtbin 0
1018end.
1019)
1020do_getqtbin=: 3 : 0
1021smoutput 'Installing JQt binaries...'
1022if. 'Linux'-:UNAME do.
1023  if. IFRASPI do.
1024    z=. 'jqt-raspi-32.tar.gz'
1025  else.
1026    z=. 'jqt-',((y-:'slim') pick 'linux';'slim'),'-',(IF64 pick 'x86';'x64'),'.tar.gz'
1027  end.
1028  z1=. 'libjqt.so'
1029elseif. IFWIN do.
1030  z=. 'jqt-win',((y-:'slim')#'slim'),'-',(IF64 pick 'x86';'x64'),'.zip'
1031  z1=. 'jqt.dll'
1032elseif. do.
1033  z=. 'jqt-mac',((y-:'slim')#'slim'),'-',(IF64 pick 'x86';'x64'),'.zip'
1034  z1=. 'libjqt.dylib'
1035end.
1036'rc p'=. httpget_jpacman_ 'http://www.jsoftware.com/download/j802/qtide/',z
1037if. rc do.
1038  smoutput 'unable to download: ',z return.
1039end.
1040d=. jpath '~bin'
1041if. IFWIN do.
1042  unzip_jpacman_ p;d
1043else.
1044  if. 'Linux'-:UNAME do.
1045    if. (0~:FHS) do.
1046      if. IFRASPI do.
1047        d1=. '/usr/lib/arm-linux-gnueabihf/.'
1048      elseif. IF64 do.
1049        d1=. '/usr/lib/x86_64-linux-gnu/.'
1050      elseif. do.
1051        d1=. '/usr/lib/i386-linux-gnu/.'
1052      end.
1053      hostcmd_jpacman_ 'cd /usr/bin && tar --no-same-owner --no-same-permissions -xzf ',(dquote p), ' && chmod 755 jqt && chmod 644 libjqt.so && mv libjqt.so ',d1
1054    else.
1055      hostcmd_jpacman_ 'cd ',(dquote d),' && tar xzf ',(dquote p)
1056    end.
1057  else.
1058    hostcmd_jpacman_ 'unzip -o ',(dquote p),' -d ',dquote d
1059  end.
1060end.
1061ferase p
1062if. #1!:0 ((0~:FHS)*.'Linux'-:UNAME){::(jpath '~bin/',z1);'/usr/bin/jqt' do.
1063  m=. 'Finished install of JQt binaries.'
1064else.
1065  m=. 'Unable to install JQt binaries.',LF
1066  m=. m,'check that you have write permission for: ',LF,((0~:FHS)*.'Linux'-:UNAME){::(jpath '~bin');'/usr/bin'
1067end.
1068smoutput m
1069if. 'Linux'-:UNAME do. return. end.
1070
1071tgt=. jpath IFWIN{::'~install/Qt';'~bin/Qt5Core.dll'
1072y=. (*#y){::0;y
1073smoutput 'Installing Qt library...'
1074if. IFWIN do.
1075  z=. 'qt53-',((y-:'angle') pick 'win';'angle'),'-',((y-:'slim')#'slim-'),(IF64 pick 'x86';'x64'),'.zip'
1076else.
1077  z=. 'qt53-mac-',((y-:'slim')#'slim-'),(IF64 pick 'x86';'x64'),'.zip'
1078end.
1079'rc p'=. httpget_jpacman_ 'http://www.jsoftware.com/download/j802/qtlib/',z
1080if. rc do.
1081  smoutput 'unable to download: ',z return.
1082end.
1083d=. jpath IFWIN{::'~install';'~bin'
1084if. IFWIN do.
1085  unzip_jpacman_ p;d
1086else.
1087  hostcmd_jpacman_ 'unzip -o ',(dquote p),' -d ',dquote d
1088end.
1089ferase p
1090if. #1!:0 tgt do.
1091  m=. 'Finished install of Qt binaries.'
1092else.
1093  m=. 'Unable to install Qt binaries.',LF
1094  m=. m,'check that you have write permission for: ',LF,IFWIN{::tgt;jpath'~bin'
1095end.
1096smoutput m
1097
1098)
1099jpkg_z_=: 3 : 0
1100  'help' jpkg y
1101  :
1102  a=. conew 'jpacman'
1103  res=. x jpkg__a y
1104  destroy__a''
1105  res
1106)
1107jpkgv_z_=: (<@:>"1@|:^:(0 ~: #))@jpkg