-
Notifications
You must be signed in to change notification settings - Fork 1
/
makesure
executable file
·672 lines (672 loc) · 22.3 KB
/
makesure
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#!/bin/sh
if command -v gawk >/dev/null;then makesure_awk='gawk -ltime -v Gawk=1';makesure_pre='';else makesure_awk=awk;makesure_pre='function gettimeofday(){}';fi
exec $makesure_awk -v "Version=0.9.22" -v "Prog=$0" "$makesure_pre"'
BEGIN {
Shell="bash"
SupportedShells["bash"]
SupportedShells["sh"]
SupportedOptions["tracing"]
SupportedOptions["silent"]
SupportedOptions["timing"]
GlobCnt=0
GlobGoalName=""
Mode="prelude"
srand()
prepareArgs()
MyDirScript="MYDIR="quoteArg(getMyDir(ARGV[1]))";export MYDIR;cd \"$MYDIR\""
Error=""
makesure()}
function makesure(i){
while(getline>0){
Lines[NR]=Line0=$0
if($1~/^@/&&"@reached_if"!=$1&&!reparseCli())continue
if("@options"==$1)handleOptions()
else if("@define"==$1)handleDefine()
else if("@shell"==$1)handleShell()
else if("@goal"==$1){if("@glob"==$2||"@glob"==$3)handleGoalGlob();else handleGoal()}
else if("@doc"==$1)handleDoc()
else if("@depends_on"==$1)handleDependsOn()
else if("@reached_if"==$1)handleReachedIf()
else if("@lib"==$1)handleLib()
else if("@use_lib"==$1)handleUseLib()
else if($1~/^@/)addError("Unknown directive: "$1)
else handleCodeLine($0)
for(i=1;i<10;i++)$i=""}
doWork()
realExit(0)}
function prepareArgs(i,arg){
for(i=2;i<ARGC;i++){
if(substr(arg=ARGV[i],1,1)=="-"){
if(arg=="-f"||arg=="--file"){
delete ARGV[i]
ARGV[1]=ARGV[++i]
}else if(arg=="-D"||arg=="--define"){
delete ARGV[i]
handleOptionDefineOverride(ARGV[++i])
}else
Args[arg]
}else
arrPush(ArgGoals,arg)
delete ARGV[i]}
if("-h"in Args||"--help"in Args){
print"makesure ver. "Version
print"Usage: makesure [options...] [-f buildfile] [goals...]"
print" -f,--file buildfile"
print" set buildfile to use (default Makesurefile)"
print" -l,--list list all available non-@private goals"
print" -la,--list-all list all available goals"
print" -d,--resolved list resolved dependencies to reach given goals"
print" -D \"var=val\",--define \"var=val\""
print" override @define values"
print" -s,--silent silent mode - only output what goals output"
print" -t,--timing display execution times for goals and total"
print" -x,--tracing enable tracing in bash/sh via `set -x`"
print" -v,--version print version and exit"
print" -h,--help print help and exit"
print" -U,--selfupdate update makesure to latest version"
realExit(0)
}else if("-v"in Args||"--version"in Args){
print Version
realExit(0)
}else if("-U"in Args||"--selfupdate"in Args){
selfUpdate()
realExit(0)}
if(!isFile(ARGV[1])){
if(isFile(ARGV[1]"/Makesurefile"))
ARGV[1]=ARGV[1]"/Makesurefile"
else
die("makesure file not found: "ARGV[1])}
if("-s"in Args||"--silent"in Args)
Options["silent"]
if("-x"in Args||"--tracing"in Args)
Options["tracing"]
if("-t"in Args||"--timing"in Args)
Options["timing"]}
function splitKV(arg,kv,n){
n=index(arg,"=")
kv[0]=trim(substr(arg,1,n-1))
kv[1]=trim(substr(arg,n+1))}
function handleOptionDefineOverride(arg,kv){
splitKV(arg,kv)
Vars[kv[0]]=kv[1]
DefineOverrides[kv[0]]}
function handleOptions(i){
checkPreludeOnly()
if(NF<2)
addError("Provide at least one option")
for(i=2;i<=NF;i++){
if(!($i in SupportedOptions))
addError("Option '\''"$i"'\'' is not supported")
Options[$i]}}
function handleDefine(){
started("define")
if(NF!=3){
addError("Invalid @define syntax, should be @define VAR_NAME '\''value'\''")
return}
if($2 !~ /^[A-Za-z_][A-Za-z0-9_]*$/){
addError("Wrong variable name: '\''"$2"'\''")
return}
if(!($2 in DefineOverrides))
Vars[$2]=$3}
function handleShell(){
checkPreludeOnly()
if(!((Shell=$2)in SupportedShells))
addError("Shell '\''"Shell"'\'' is not supported")}
function timingOn(){
return"timing"in Options&&!("silent"in Options)}
function started(mode){
Mode=mode}
function handleLib(libName){
started("lib")
if((libName=$2)in Lib){
addError("Lib '\''"libName"'\'' is already defined")}
arrPush(LibNames,libName)
Lib[libName]}
function handleUseLib(i){
checkGoalOnly()
if("goal"==Mode)
registerUseLib(currentGoalName())
else
for(i=0;i<GlobCnt;i++)
registerUseLib(globGoal(i))}
function registerUseLib(goalName){
if(goalName in GoalToLib)
addError("You can only use one @lib in a @goal")
GoalToLib[goalName]=$2
UseLibLineNo[goalName]=NR}
function handleGoal(i,goalName){
started("goal")
if(registerGoal(parsePriv(),goalName=$2))
if("@params"==$3){
if(3==NF)addError("missing parameters")
for(i=4;i<=NF;i++)
GoalParams[goalName,GoalParamsCnt[goalName]++]=validateParamName($i)
}else if(NF>2)addError("nothing allowed after goal name")}
function validateParamName(p){
if(p !~ /^[A-Z_][A-Z0-9_]*$/)addError("@param name should match /^[A-Z_][A-Z0-9_]*$/: '\''"p"'\''")
return p}
function registerGoal(priv,goalName){
if(""==goalName||"@params"==goalName)
addError("Goal must have a name")
else if(goalName in GoalsByName)
addError("Goal "quote2(goalName,1)" is already defined")
else {
arrPush(GoalNames,goalName)
GoalsByName[goalName]=priv
return 1}}
function globGoal(i){return (GlobGoalName?GlobGoalName"@":"")GlobFiles[i]}
function calcGlob(goalName,pattern,script,file){
GlobCnt=0
GlobGoalName=goalName
delete GlobFiles
script=MyDirScript";for f in "pattern";do test -e \"$f\"&&echo \"$f\"||:;done"
if("sh"!=Shell)
script=Shell" -c "quoteArg(script)
while((script|getline file)>0){
GlobCnt++
arrPush(GlobFiles,file)}
closeErr(script)
quicksort(GlobFiles,0,arrLen(GlobFiles)-1)}
function parsePriv(){
if("@private"!=$NF)return 0
$NF=""
NF--
return 1}
function handleGoalGlob(goalName,globAllGoal,globSingle,priv,i,pattern,nfMax,gi,j,l,globPgParams){
started("goal_glob")
priv=parsePriv()
if("@glob"==(goalName=$2)){
goalName="";pattern=$(nfMax=3)
}else
pattern=$(nfMax=4)
if(NF>nfMax&&"@params"!=$(nfMax+1))
addError("nothing or @params allowed after glob pattern")
else if(pattern=="")
addError("absent glob pattern")
else {
if("@params"==$(nfMax+1))
for(i=nfMax+2;i<=NF;i++)
arrPush(globPgParams,validateParamName($i))
calcGlob(goalName,pattern)
globAllGoal=goalName?goalName:pattern
globSingle=GlobCnt==1&&globAllGoal==globGoal(0)
for(i=0;i<GlobCnt;i++){
registerGoal(globSingle?priv:1,gi=globGoal(i))
for(j=0;j in globPgParams;j++)
GoalParams[gi,GoalParamsCnt[gi]++]=globPgParams[j]}
if(!globSingle){
registerGoal(priv,globAllGoal)
for(j=0;j in globPgParams;j++)
GoalParams[globAllGoal,GoalParamsCnt[globAllGoal]++]=globPgParams[j]
for(i=0;i<GlobCnt;i++){
registerDependency(globAllGoal,globGoal(i))
if(arrLen(globPgParams)){
l="@depends_on x @params"
for(j=0;j in globPgParams;j++)
l=l" "globPgParams[j]
DependencyArgsL[globAllGoal,i]=l}}}}}
function handleDoc(i){
checkGoalOnly()
if("goal"==Mode)
registerDoc(currentGoalName())
else {
if(!(GlobCnt==1&¤tGoalName()==globGoal(0)))
registerDoc(currentGoalName())
for(i=0;i<GlobCnt;i++)
registerDoc(globGoal(i))}}
function registerDoc(goalName){
if(goalName in Doc)
addError("Multiple @doc not allowed for a goal")
$1=""
Doc[goalName]=trim($0)}
function handleDependsOn(i){
checkGoalOnly()
if(NF<2)
addError("Provide at least one dependency")
if("goal"==Mode)
registerDependsOn(currentGoalName())
else
for(i=0;i<GlobCnt;i++)
registerDependsOn(globGoal(i))}
function registerDependsOn(goalName,i,dep){
for(i=2;i<=NF;i++){
if("@args"==(dep=$i)){
if(i!=3)
addError("@args only allowed at position 3")
DependencyArgsL[goalName,DependenciesCnt[goalName]-1]=Line0
break
}else
registerDependency(goalName,dep)}}
function registerDependency(goalName,depGoalName,x){
Dependencies[x=goalName SUBSEP DependenciesCnt[goalName]++]=depGoalName
DependenciesLineNo[x]=NR}
function handleReachedIf(i){
checkGoalOnly()
if("goal"==Mode)
registerReachedIf(currentGoalName())
else
for(i=0;i<GlobCnt;i++)
registerReachedIf(globGoal(i),makeGlobVarsCode(i))}
function makeGlobVarsCode(i){
return"ITEM="quoteArg(GlobFiles[i])";INDEX="i";TOTAL="GlobCnt";"}
function registerReachedIf(goalName,preScript){
if(goalName in ReachedIf)
addError("Multiple @reached_if not allowed for a goal")
trimDirective()
ReachedIf[goalName]=preScript trim($0)}
function trimDirective(){
sub(/^[ \t]*@[a-z_]+/,"")}
function checkBeforeRun(i,j,dep,depCnt,goalName){
for(i=0;i in GoalNames;i++){
depCnt=DependenciesCnt[goalName=GoalNames[i]]
for(j=0;j<depCnt;j++)
if(!((dep=Dependencies[goalName,j])in GoalsByName))
addError("Goal "quote2(goalName,1)" has unknown dependency '\''"dep"'\''",DependenciesLineNo[goalName,j])
if(goalName in GoalToLib&&!(GoalToLib[goalName]in Lib))
addError("Goal "quote2(goalName,1)" uses unknown lib '\''"GoalToLib[goalName]"'\''",UseLibLineNo[goalName])}}
function getPreludeCode(a,k){
addLine(a,MyDirScript)
for(k in Vars)
addLine(a,k"="quoteArg(Vars[k])";export "k)
return a[0]}
function doWork(\
i,goalName,gnLen,gnMaxLen,reachedGoals,emptyGoals,preludeCode,
body,goalBody,goalBodies,resolvedGoals,exitCode,t0,t1,t2,goalTimed,list){
started("end")
checkBeforeRun()
if(arrLen(GoalNames))topologicalSort(0,GoalNames)
instantiateGoals()
if(Error)
die(Error)
list="-l"in Args||"--list"in Args
if(list||"-la"in Args||"--list-all"in Args){
print"Available goals:"
for(i=0;i in GoalNames;i++){
goalName=GoalNames[i]
if(list&&GoalsByName[goalName])
continue
if((gnLen=length(quote2(goalName)))>gnMaxLen&&gnLen<=30)
gnMaxLen=gnLen}
for(i=0;i in GoalNames;i++){
goalName=GoalNames[i]
if(list&&GoalsByName[goalName])
continue
printf" "
if(goalName in Doc)
printf"%-"gnMaxLen"s : %s\n",quote2(goalName),Doc[goalName]
else
print quote2(goalName)}
}else {
if(timingOn())
t0=currentTimeMillis()
topologicalSort(1,ArgGoals,resolvedGoals,reachedGoals)
preludeCode=getPreludeCode()
for(i=0;i in GoalNames;i++){
body=trim(Code[goalName=GoalNames[i]])
emptyGoals[goalName]=""==body
goalBody[0]=""
addLine(goalBody,preludeCode)
addLine(goalBody,CodePre[goalName])
if(goalName in GoalToLib)
addLine(goalBody,Lib[GoalToLib[goalName]])
addLine(goalBody,body)
goalBodies[goalName]=goalBody[0]}
if("-d"in Args||"--resolved"in Args){
printf"Resolved goals to reach for"
for(i=0;i in ArgGoals;i++)
printf" %s",quote2(ArgGoals[i],1)
print":"
for(i=0;i in resolvedGoals;i++)
if(!reachedGoals[goalName=resolvedGoals[i]]&&!emptyGoals[goalName])
print" "quote2(goalName)
}else {
for(i=0;i in resolvedGoals;i++){
goalName=resolvedGoals[i]
goalTimed=timingOn()&&!reachedGoals[goalName]&&!emptyGoals[goalName]
if(goalTimed)
t1=t2?t2:currentTimeMillis()
if(!("silent"in Options))
print" goal "quote2(goalName,1)" "(reachedGoals[goalName]?"[already satisfied].":emptyGoals[goalName]?"[empty].":"...")
exitCode=(reachedGoals[goalName]||emptyGoals[goalName])?0:shellExec(goalBodies[goalName],goalName)
if(exitCode!=0)
print" goal "quote2(goalName,1)" failed"
if(goalTimed){
t2=currentTimeMillis()
print" goal "quote2(goalName,1)" took "renderDuration(t2-t1)}
if(exitCode!=0)
break}
if(timingOn())
print" total time "renderDuration((t2?t2:currentTimeMillis())-t0)
if(exitCode!=0)
realExit(exitCode)}}}
function topologicalSort(includeReachedIf,requestedGoals,result,reachedGoals,i,j,goalName,loop,depCnt){
topologicalSortReset()
for(i=0;i in GoalNames;i++){
depCnt=DependenciesCnt[goalName=GoalNames[i]]
for(j=0;j<depCnt;j++)
topologicalSortAddConnection(goalName,Dependencies[goalName,j])}
if(arrLen(requestedGoals)==0)
arrPush(requestedGoals,"default")
for(i=0;i in requestedGoals;i++){
if(!((goalName=requestedGoals[i])in GoalsByName))
die("Goal not found: "goalName)
topologicalSortPerform(includeReachedIf,reachedGoals,goalName,result,loop)}
if(loop[0]==1)
die("There is a loop in goal dependencies via "loop[1]" -> "loop[2])}
function isCodeAllowed(){return"goal"==Mode||"goal_glob"==Mode||"lib"==Mode}
function isPrelude(){return"prelude"==Mode}
function checkPreludeOnly(){if(!isPrelude())addError("Only use "$1" in prelude")}
function checkGoalOnly(){if("goal"!=Mode&&"goal_glob"!=Mode)addError("Only use "$1" in @goal")}
function currentGoalName(){return arrLast(GoalNames)}
function currentLibName(){return arrLast(LibNames)}
function realExit(code){
exit code}
function addError(err,n){
if((err,n?n:(n=NR))in AddedErrors)
return
AddedErrors[err,n]
Error=addL(Error,err":\n"ARGV[1]":"n": "Lines[n])}
function die(msg,out){
out="cat 1>&2"
print msg|out
close(out)
realExit(1)}
function checkConditionReached(goalName,conditionStr,script){
script=getPreludeCode()
if(goalName in GoalToLib)
script=script"\n"Lib[GoalToLib[goalName]]
script=script"\n"conditionStr
return shellExec(script,goalName"@reached_if")==0}
function shellExec(script,comment,res){
if("tracing"in Options){
script=": "quoteArg(comment)"\n"script
script=Shell" -x -e -c "quoteArg(script)
}else
script=Shell" -e -c "quoteArg(script)
script="trap '\''exit 7'\'' INT;"script
res=system(script)
return res}
function getMyDir(makesurefilePath){
return executeGetLine("cd \"$(dirname "quoteArg(makesurefilePath)")\";pwd")}
function handleCodeLine(line){
if(!isCodeAllowed()&&line !~ /^[ \t]*#/&&trim(line)!=""){
if(!ShellInPreludeErrorShown++)
addError("Shell code is not allowed outside goals/libs")
}else
addCodeLine(line)}
function addCodeLine(line,goalName,name,i){
if("lib"==Mode){
name=currentLibName()
Lib[name]=addL(Lib[name],line)
}else if("goal_glob"==Mode){
for(i=0;i<GlobCnt;i++){
if(!CodePre[goalName=globGoal(i)])
CodePre[goalName]=makeGlobVarsCode(i)
addCodeLineToGoal(goalName,line)}
}else
addCodeLineToGoal(currentGoalName(),line)}
function addCodeLineToGoal(name,line){
Code[name]=addL(Code[name],line)}
function topologicalSortReset(){
delete Visited
delete Slist
delete Scnt}
function topologicalSortAddConnection(from,to){
Slist[from,++Scnt[from]]=to}
function topologicalSortPerform(includeReachedIf,reachedGoals,node,result,loop,i,s){
if(Visited[node]==2)
return
if(includeReachedIf&&node in ReachedIf&&checkConditionReached(node,ReachedIf[node])){
Visited[node]=2
arrPush(result,node)
reachedGoals[node]=1
return}
Visited[node]=1
for(i=1;i<=Scnt[node];i++){
if(Visited[s=Slist[node,i]]==0)
topologicalSortPerform(includeReachedIf,reachedGoals,s,result,loop)
else if(Visited[s]==1){
loop[0]=1
loop[1]=s
loop[2]=node}}
Visited[node]=2
arrPush(result,node)}
function instantiateGoals(i,l,goalName){
l=arrLen(GoalNames)
for(i=0;i<l;i++)
if(GoalParamsCnt[goalName=GoalNames[i]]==0)
instantiate(goalName)
for(goalName in GoalsByName)
if(GoalParamsCnt[goalName]>0){
arrDel(GoalNames,goalName)
delete GoalsByName[goalName]}}
function instantiate(goal,args,newArgs,i,j,depArg,depArgType,dep,goalNameInstantiated,argsCnt,gi,gii,argsCode,reparsed){
if(goal in Instantiated)return goal
Instantiated[goalNameInstantiated=instantiateGoalName(goal,args)]
if(goalNameInstantiated!=goal){
if(!(goalNameInstantiated in GoalsByName))
arrPush(GoalNames,goalNameInstantiated)
copyKey(goal,goalNameInstantiated,GoalsByName)
copyKey(goal,goalNameInstantiated,DependenciesCnt)
copyKey(goal,goalNameInstantiated,CodePre)
copyKey(goal,goalNameInstantiated,Code)
copyKey(goal,goalNameInstantiated,Doc)
copyKey(goal,goalNameInstantiated,ReachedIf)
copyKey(goal,goalNameInstantiated,GoalToLib)
for(i in args)
argsCode=addL(argsCode,i"="quoteArg(args[i]))
CodePre[goalNameInstantiated]=addL(CodePre[goalNameInstantiated],argsCode)
if(goalNameInstantiated in ReachedIf)
ReachedIf[goalNameInstantiated]=argsCode"\n"ReachedIf[goalNameInstantiated]}
for(i=0;i<DependenciesCnt[goal];i++){
dep=Dependencies[gi=goal SUBSEP i]
argsCnt=0
if(gi in DependencyArgsL){
delete reparsed
parseCli_2(DependencyArgsL[gi],args,Vars,reparsed)
argsCnt=reparsed[-7]-3}
if(dep in GoalsByName&&argsCnt!=GoalParamsCnt[dep])
addError("wrong args count for '\''"dep"'\''",DependenciesLineNo[gi])
for(j=0;j<argsCnt;j++){
depArg=reparsed[j+3]
depArgType="u"==reparsed[j+3,"quote"]?"var":"str"
newArgs[GoalParams[dep,j]]=\
depArgType=="str"?\
depArg : \
depArgType=="var"?\
(depArg in args?args[depArg]:\
depArg in Vars?Vars[depArg]:addError("wrong arg '\''"depArg"'\''",DependenciesLineNo[gi])):\
die("wrong depArgType: "depArgType)}
gii=goalNameInstantiated SUBSEP i
Dependencies[gii]=instantiate(dep,newArgs)
DependenciesLineNo[gii]=DependenciesLineNo[gi]}
return goalNameInstantiated}
function instantiateGoalName(goal,args,res,cnt,i){
if((cnt=GoalParamsCnt[goal])==0)return goal
res=goal
for(i=0;i<cnt;i++)
res=res"@"args[GoalParams[goal,i]]
return res}
function currentTimeMillis(res){
if(Gawk)
return int(gettimeofday()*1000)
res=executeGetLine("date +%s%3N")
sub(/%?3N/,"000",res)
return +res}
function selfUpdate(url,tmp,err,newVer){
url="https://raw.githubusercontent.com/xonixx/makesure/main/makesure?token="rand()
tmp=executeGetLine("mktemp /tmp/makesure_new.XXXXXXXXXX")
err=dl(url,tmp)
if(!err&&!ok("chmod +x "tmp))err="can'\''t chmod +x "tmp
if(!err){
newVer=executeGetLine(tmp" -v")
if(Version!=newVer){
if(!ok("cp "tmp" "quoteArg(Prog)))
err="can'\''t overwrite "Prog
else print"updated "Version" -> "newVer
}else print"you have latest version "Version" installed"}
rm(tmp)
if(err)die(err)}
function renderDuration(deltaMillis,\
deltaSec,deltaMin,deltaHr,deltaDay,dayS,hrS,minS,secS,secSI,res){
deltaSec=deltaMillis/1000
deltaMin=0
deltaHr=0
deltaDay=0
if(deltaSec>=60){
deltaMin=int(deltaSec/60)
deltaSec=deltaSec-deltaMin*60}
if(deltaMin>=60){
deltaHr=int(deltaMin/60)
deltaMin=deltaMin-deltaHr*60}
if(deltaHr>=24){
deltaDay=int(deltaHr/24)
deltaHr=deltaHr-deltaDay*24}
dayS=deltaDay>0?deltaDay" d":""
hrS=deltaHr>0?deltaHr" h":""
minS=deltaMin>0?deltaMin" m":""
secS=deltaSec>0?deltaSec" s":""
secSI=deltaSec>0?int(deltaSec)" s":""
if(dayS!="")
res=dayS" "(hrS==""?"0 h":hrS)
else if(deltaHr>0)
res=hrS" "(minS==""?"0 m":minS)
else if(deltaMin>0)
res=minS" "(secSI==""?"0 s":secSI)
else
res=deltaSec>0?secS:"0 s"
return res}
function executeGetLine(script,res){
script|getline res
closeErr(script)
return res}
function closeErr(script){if(close(script)!=0)die("Error executing: "script)}
function dl(url,dest,verbose){
verbose="VERBOSE"in ENVIRON
if(commandExists("wget")){
if(!ok("wget "(verbose?"":"-q")" "quoteArg(url)" -O"quoteArg(dest)))
return"error with wget"
}else if(commandExists("curl")){
if(!ok("curl "(verbose?"":"-s")" "quoteArg(url)" -o "quoteArg(dest)))
return"error with curl"
}else return"wget/curl not found"}
function natOrder(s1,s2,i1,i2,c1,c2,n1,n2){
if(_digit(c1=substr(s1,i1,1))&&_digit(c2=substr(s2,i2,1))){
n1=+c1;while(_digit(c1=substr(s1,++i1,1))){n1=n1*10+c1}
n2=+c2;while(_digit(c2=substr(s2,++i2,1))){n2=n2*10+c2}
return n1==n2?natOrder(s1,s2,i1,i2):_cmp(n1,n2)}
while((c1=substr(s1,i1,1))==(c2=substr(s2,i2,1))&&c1!=""&&!_digit(c1)){
i1++;i2++}
return _digit(c1)&&_digit(c2)?natOrder(s1,s2,i1,i2):_cmp(c1,c2)}
function _cmp(v1,v2){return v1>v2?1:v1<v2?-1:0}
function _digit(c){return c>="0"&&c<="9"}
function quicksort(data,left,right,i,last){
if(left>=right)
return
swap(data,left,int((left+right)/2))
last=left
for(i=left+1;i<=right;i++)
if(natOrder(data[i],data[left],1,1)<0)
swap(data,++last,i)
swap(data,left,last)
quicksort(data,left,last-1)
quicksort(data,last+1,right)}
function swap(data,i,j,temp){
temp=data[i]
data[i]=data[j]
data[j]=temp}
function parseCli_2(line,vars,vars2,res,pos,c,c1,isDoll,q,var,inDef,defVal,val,w,i){
for(pos=1;;){
while((c=substr(line,pos,1))==" "||c=="\t")pos++
if(c=="#"||c=="")
return
else {
if((isDoll=substr(line,pos,2)=="$'\''")||c=="'\''"||c=="\""){
if(isDoll)
pos++
q=isDoll?"'\''":c
w=""
while((c=substr(line,++pos,1))!=q){
if(c=="")
return"unterminated argument"
else if(c=="\\"&&((c1=substr(line,pos+1,1))=="'\''"&&isDoll||c1==c||q=="\""&&(c1==q||c1=="$"))){
c=c1;pos++
}else if(c=="$"&&q=="\""){
var=""
inDef=0
defVal=""
if((c1=substr(line,pos+1,1))=="{"){
for(pos++;(c=substr(line,++pos,1))!="}";){
if(c=="")
return"unterminated argument"
if(!inDef&&":"==c&&"-"==substr(line,pos+1,1)){
inDef=1
c=substr(line,pos += 2,1)}
if(inDef){
if("}"==c)
break
if("\\"==c&&((c1=substr(line,pos+1,1))=="$"||c1=="\\"||c1=="}"||c1=="\"")){
c=c1;pos++}
defVal=defVal c
}else
var=var c}
}else
for(;(c=substr(line,pos+1,1))~/[_A-Za-z0-9]/;pos++)
var=var c
if(var !~ /^[_A-Za-z][_A-Za-z0-9]*$/)
return"wrong var: '\''"var"'\''"
w=(w)((val=var in vars?vars[var]:var in vars2?vars2[var]:ENVIRON[var])!=""?val:defVal)
continue}
w=w c}
res[i=+res[-7]++,"quote"]=isDoll?"$":q
res[i]=w
if((c=substr(line,++pos,1))!=""&&c!=" "&&c!="\t")
return"joined arguments"
}else {
w=c
while((c=substr(line,++pos,1))!=""&&c!=" "&&c!="\t"){
w=w c}
if(w !~ /^[_A-Za-z0-9@.]+$/)
return"wrong unquoted: '\''"w"'\''"
res[i=+res[-7]++,"quote"]="u"
res[i]=w}}}}
function reparseCli(res,i,err){
err=parseCli_2($0,Vars,EMPTY,res)
if(err){
addError("Syntax error: "err)
return 0}
$0=""
for(i=NF=0;i in res;i++)
$(++NF)=res[i]
for(i=2;i<=NF;i++)
if("\""==res[i-1,"quote"]&&!("@define"==$1&&3==i||"@depends_on"==$1&&"@args"==$3&&i>3)){
addError("Wrong quoting: "$i)
return 0}
return 1}
function quote2(s,force){
if(index(s,"'\''")){
gsub(/\\/,"\\\\",s)
gsub(/'\''/,"\\'\''",s)
return"$'\''"s"'\''"
}else
return force||s~/[^a-zA-Z0-9.,@_\/=+-]/?"'\''"s"'\''":s}
function addLine(target,line){target[0]=addL(target[0],line)}
function addL(s,l){return s?s"\n"l:l}
function arrPush(arr,elt){arr[arr[-7]++]=elt}
function arrLen(arr){return +arr[-7]}
function arrDel(arr,v,l,i,e,resArr){
l=arrLen(arr)
for(i=0;i<l;i++)
if(v!=(e=arr[i]))
arrPush(resArr,e)
delete arr
for(i in resArr)
arr[i]=resArr[i]}
function arrLast(arr,l){return (l=arrLen(arr))>0?arr[l-1]:""}
function commandExists(cmd){return ok("command -v "cmd" >/dev/null")}
function ok(cmd){return system(cmd)==0}
function isFile(path){return ok("test -f "quoteArg(path))}
function rm(f){system("rm "quoteArg(f))}
function quoteArg(a){gsub("'\''","'\''\\'\'''\''",a);return"'\''"a"'\''"}
function trim(s){sub(/^[ \t\r\n]+/,"",s);sub(/[ \t\r\n]+$/,"",s);return s}
function copyKey(keySrc,keyDst,arr){if(keySrc in arr)arr[keyDst]=arr[keySrc]}' Makesurefile "$@"