summaryrefslogtreecommitdiffstats
path: root/base/pkg-get/pkg-get.awk
blob: fd36a6aa2d874cf0dfde371cbe132412edc8423a (plain)
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
#! /usr/bin/awk -f
#             _                        _                        _
#       _ __ | | ____ _      __ _  ___| |_        __ ___      _| | __
#      | '_ \| |/ / _` |___ / _` |/ _ \ __|      / _` \ \ /\ / / |/ /
#      | |_) |   < (_| |___| (_| |  __/ |_   _  | (_| |\ V  V /|   <
#      | .__/|_|\_\__, |    \__, |\___|\__| (_)  \__,_| \_/\_/ |_|\_\
#      |_|        |___/     |___/
#
#                    A binary package management utility


function configs(   a, i, k, n, old_FS)
{
   n=split(ENVIRON["_"],a,"/")
   NPKG       = a[n]
   VERSION    = "0.3.4"
   DATE       = "2011-06-05"

   # defauts
   CONF       = "/etc/pkg-get.conf"
   VERSFILE   = "/var/lib/pkg/nutyx-version"
   DB         = "/var/lib/pkg/db"
   LOCKER     = "/var/lib/pkg/pkg-get.locker"
   PRE        = "n"
   POST       = "n"
   PKGDIR[0]  = "/srv/NuTyX/release/"
   load_NUTYX_VERSION()
   "uname -p " | getline ENV; close("uname -p")
   WEBDIR[0]  = "http://nutyx.meticul.eu/" NUTYX_VERSION "/" ENV "/release/"
   TIMEOUT   = 30

   # arguments
   OPER=(ARGC>1) ? ARGV[1] : ""
   PKG =(ARGC>2) ? ARGV[2] : ""
   for (i=2; i<ARGC; i++) {
      if (ARGV[i]=="-r") {
         tb_OPT[ARGV[i]]=ARGV[i+1]
         i++
      } else if (substr(ARGV[i],1,1)=="-") {
         split(ARGV[i],a,"=")
         tb_OPT[a[1]]=a[2]
      } else 
         tb_PKG[++nPKG]=ARGV[i]
   }

   # divers
   cmd="uname -m"
   cmd | getline ARCH
   close(cmd)

   # fichier pkg-get.conf
   if ("--config" in tb_OPT)
      CONF = tb_OPT["--config"]
   old_FS=FS
   FS="|"
   NPKGDIR=0
   while (getline < CONF) {
      n=split($1,a," ")
      if (a[1]=="pkgdir") {
         PKGDIR[NPKGDIR]=a[2]"/"
         if (NF<2) {
            WEBDIR[NPKGDIR]=""
         } else {
            sub(" ","",$2)
            sub("/uname-m/", "/"ARCH"/", $2) 
            WEBDIR[NPKGDIR]=$2"/"
         }
         NPKGDIR++
      }
      if (a[1]=="runscripts") {
         PRE  = (a[2]=="yes") ? "y" : "n"
         POST = (a[2]=="yes") ? "y" : "n"
      }
   }
   FS=old_FS

   # options
   if ("-r" in tb_OPT)
      AARGS = AARGS " -r " tb_OPT["-r"]
   if ("-do" in tb_OPT)
      {}
   if ("-f" in tb_OPT)
      AARGS = AARGS " -f"
   if ("-im" in tb_OPT)
      AARGS = AARGS " -im"
   if ("--aargs" in tb_OPT)
      AARGS = AARGS " " tb_OPT["--aargs"]
   if (("--install-scripts" in tb_OPT) || ("--pre-install" in tb_OPT))
      PRE = "y"
   if (("--install-scripts" in tb_OPT) || ("--post-install" in tb_OPT))
      POST = "y"
   DEPS = "PKGDEPS"
   READ = "PKGREAD"
   REPO = "PKGREPO"
   GRP  = "PKGGRP"

   # i18n
   TEXTDOMAIN = "pkg-get"
   bindtextdomain("/usr/share/locale")

   # msg
   RED=31
   GREEN=32
   YELLOW=33
#  BLUE=34
#  MAGENTA=35
   CYAN=36
#  WHITE=37

   tbColor["OK"] = GREEN
   tbColor["ERR"] = RED
   tbColor["INFO"] = CYAN

#  COL = system("exit $(stty size | cut -d' ' -f2)") - 6
   COL = 24
}


function current()
{
   load_DB()
   if (PKG in tb_DB) 
      print tb_DB[PKG]
   else
      print _"Paquet" " " PKG " " _"non installé"
}


function dependent(   a, i, n)
{
      load_RDEPS()
      load_DB()
      n=split(tb_RDEPS[tb_PKG[1]],a,",")
      for (i=1; i<=n; i++)
         if ((a[i] in tb_DB) || ("--all" in tb_OPT))
            print a[i]
} 


function depends(   c)
{
      load_DB()
      load_DEPS()
      load_REPO()
      print _"- dépendences ([i] = installé, [u] = mise à jour possible)"
      depends2(PKG,ARGV[3])
      if (PKG in tb_DB)
         c= (tb_REPO[PKG]==tb_DB[PKG]) ? "i" : "u"
      else
         c=" "
      printf("[%c] %s\n", c, PKG)
}


function depends2(Pkg, Fl,    a, i, n, c)
{
   S = (Fl=="--d") ? S "-- " : ""
   n=split(tb_DEPS[Pkg],a,",")
   for (i=1; i<=n; i++) {
      if (tb_DEP_TRT[a[i]]==0) {
         tb_DEP_TRT[a[i]]++
         if (a[i] in tb_DEPS) 
            depends2(a[i],Fl)
			if (a[i] in tb_DB)
            c= (tb_DB[a[i]]==tb_REPO[a[i]]) ? "i" : "u"
			 else
            c=" "
         printf("[%c] %s%s\n", c, S, a[i])
      }
   }
   S = (Fl=="--d") ? substr(S,4) : ""
}


function depinst(   i)
{
   verif_if_root() 
   load_REPO()
   if (!(PKG in tb_REPO)) {
      print _"Paquet" " " _"indisponible" ": "PKG
      exit 1
   }
   load_DB()
   load_DEPS() 
   for (i=1; i<=nPKG; i++) {
      if (tb_PKG[i] in tb_DB)
         print _"Le paquet" " " tb_PKG[i] " " _"est déjà installé" 
      else 
         depinst2(tb_PKG[i])
   }
   return rc
} 


function depinst2(Pkg,    a, i, n)
{
   n=split(tb_DEPS[Pkg],a,",")
   for (i=1; i<=n; i++) {
      if (tb_DEP_TRT[a[i]]==0) {
         tb_DEP_TRT[a[i]]++
         if (a[i] in tb_DEPS)
            depinst2(a[i])
         if (!(a[i] in tb_DB)) {
            install2_update2(a[i], "")
         }
      }
   }
   if (!(Pkg in tb_DB)) {
      install2_update2(Pkg, "")
   }
}


function diff(   a, i, n, pak)
{
   load_DB()
   load_LOCKER()
   load_REPO()
   print _"Différences entre paquets installés et paquets disponibles dans le dépôts:\n"
   printf("%-30s %-20s %-20s\n", _"Paquet", _"Installé", _"Disponible dans les dépôts\n")
   for (pak in tb_DB)
      a[pak]=pak
   n=asort(a)
   for (i=1;i<=n;i++) {
      pak=a[i]
      if ((pak in tb_REPO) && ((!(pak in tb_LOCK)) || ("--all" in tb_OPT)) && (tb_DB[pak]!=tb_REPO[pak])) {
         printf("%-30s %-20s %-20s\n", pak, tb_DB[pak], tb_REPO[pak])
      }
   }
}


function download(pak,    a, WEBREPO, fnPak, cmd, rc, pakw)
{
   split(pak,a,"#")
   WEBREPO = tb_WEBDIR[a[1]]
   fnPak = tb_PKGDIR[a[1]] pak
   if ((getline < fnPak) <=0) {
      pakw = pak 
      sub("#", "%23", pakw) 
      cmd="wget --no-directories --tries=3 --waitretry=3 -nv --connect-timeout=" TIMEOUT " --directory-prefix="tb_PKGDIR[a[1]] " " WEBREPO pakw " >/dev/null 2>&1"
      if ("--dry" in tb_OPT) {
         print " " cmd
         return 8
      } else {
         rc=system(cmd) 
         if (rc>0) {
            return 1
         }
         if (!("-im" in tb_OPT))
            verif_md5sum(tb_PKGDIR[a[1]], pak)
         return 0
      }
   } else
     return 9
}


function dsearch(   old_FS, j, FILE)
{
   old_FS = FS
   FS="[:#]"
   for (j=0; j<NPKGDIR; j++) {
      FILE=PKGDIR[j] REPO
      while ((getline < FILE) >0) {
         if (index($1,ARGV[2]) || index($5,ARGV[2]))
            print $1
      }
   }
   close(FILE)
   FS = old_FS
} 


function help()
{
   version()
   print "\033[1;33m" _"Usage:" "\033[1;0m"
   print "  " NPKG " current <paquet>    - " _"affiche la version installée de <paquet>"
   print "  " NPKG " dependent <paquet>  - " _"affiche la liste des paquets dépendants de <paquet>"
   print "  " NPKG " depends <paquet>    - " _"affiche la liste des dépendances de <paquet>"
   print "  " NPKG " depinst <paquet(s)> - " _"installe <paquet(s)> et ses(leurs) dépendances"
   print "  " NPKG " diff                - " _"affiche la liste des paquets obsolètes"
   print "  " NPKG " dsearch <chaine>    - " _"cherche les paquets dont le nom/la description contient <chaine>"
   print "  " NPKG " help                - " _"affiche cette aide"
   print "  " NPKG " info <paquet>       - " _"affiche les informations concernant <paquet>"
   print "  " NPKG " isinst <paquet>     - " _"affiche si <paquet> est installé"
   print "  " NPKG " install <paquet(s)> - " _"installe <paquet(s)>"
   print "  " NPKG " list                - " _"affiche la liste des paquets disponibles dans les dépôts"
   print "  " NPKG " listinst            - " _"affiche la liste des paquets installés"
   print "  " NPKG " listlocked          - " _"affiche la liste des paquets verrouillés"
   print "  " NPKG " lock <paquet(s)>    - " _"verrouille <paquet(s)> (empêche la mise à jour)"
   print "  " NPKG " path <paquet>       - " _"affiche le répertoire d'installtion de <paquet>"
   print "  " NPKG " quickdep <paquet>   - " _"affiche la liste abrégée des dépendances de <paquet>"
   print "  " NPKG " quickdiff           - " _"affiche la liste abrégée des paquets obsolètes"
   print "  " NPKG " readme <paquet>     - " _"affiche les informations README de <paquet> si existantes"
   print "  " NPKG " search <chaine>     - " _"cherche les paquets dont le nom contient <chaine>"
   print "  " NPKG " sync                - " _"synchronise avec le dépôt"
   print "  " NPKG " sysup               - " _"met à jour les paquets obsolètes, télécharge si nécessaire"
   print "  " NPKG " unlock <paquet(s)>  - " _"déverrouille <paquet(s)> (permet la mise à jour)"
   print "  " NPKG " update <paquet(s)>  - " _"met à jour <paquet(s)>, télécharge si nécessaire"
   print "  " NPKG " version             - " _"affiche la version de ce script"
   print "\033[1;33m" _"Exemple:" "\033[1;0m"
   print "  " NPKG " depends k3b"
}


function info(   a, b, c, cmd, g, gline, n, rline, out)
{
   load_REPO()
   cmd = "grep ^" PKG " " tb_PKGDIR[PKG] REPO
   cmd | getline rline
   if (rline) {
      close(cmd)
      split (rline, a, ":")
      split (a[1], b, "#")
      print _"Nom " "        : " PKG
      cmd = "grep " PKG " " tb_PKGDIR[PKG] GRP " 2>&1"
      cmd | getline gline
      close(cmd)
      split (gline, g, ":")
      if (g[1]=="grep")
         print _"Groupe" "      : " 
      else
         print _"Groupe" "      : " g[1]
      split(b[2], c, "-")
      print  "Version" "     : " c[1]
      sub(".pkg.tar.xz","",c[2])
      print  "Release" "     : " c[2]
      print  "Description" " : " a[4]
      print  "URL" "         : " tb_WEBDIR[PKG] a[1]
      print  "Md5sum" "      : " a[3]
      print _"Taille" "      : " a[2]
      load_DEPS()
      print _"Dépend de " "  : " tb_DEPS[PKG]
      out=""
      if (tb_PRE[PKG]=="yes")
         out = out "pre-install "
      if (tb_POST[PKG]=="yes")
         out = out "post-install "
      if (tb_READ[PKG]=="yes")
         out = out "readme"
      print _"Fichiers" "    : " out
   } else
      print _"Paquet" " '" PKG "' " _"non trouvé"
} 


function install(   i)
{
   verif_if_root() 
   load_REPO()
   if (!(PKG in tb_REPO)) {
      print _"Paquet" " " _"indisponible" ": "PKG
      exit 1
   }
   load_DB()
   for (i=1; i<=nPKG; i++) {
      if (tb_PKG[i] in tb_DB)
         print _"Le paquet" " " tb_PKG[i] " " _"est déjà installé"
      else
         install2_update2(tb_PKG[i], " ")
   }
}


function install2_update2(Pkg, Oper,   cmd, i, pak, rc, st)
{
   printPkg(Pkg"#"tb_REPO[Pkg])

   if (!(Pkg in tb_REPO)) {
      print "\033[1;31m" _"Paquet" " " Pkg " " _"indisponible" "\033[1;0m"
      return
   }
   if (tb_REPO[Pkg] != "" ) {
      pak=Pkg"#"tb_REPO[Pkg] ".pkg.tar.xz"
      printOper("Téléchargement")
      rc = download(pak)
      if (!("--dry" in tb_OPT)) {
         if (rc==0)      st="OK"
         else if (rc==1) st="ERR"
         else if (rc==9) st="OLD"
         else            st=""
         printStatus(st)
      }
   }
   if ("-do" in tb_OPT)
      return

   if (PRE=="y" && tb_PRE[Pkg]=="yes") {
      printOper("Pré-installation")
      cmd = "/bin/bash " tb_PKGDIR[Pkg] "PKGINST " Pkg "_pre_install"
      if ("--dry" in tb_OPT) {
         print " " cmd
      } else {
         rc = system(cmd)
#        while ((cmd | getline) >0)
#           print
#        close(cmd)
         st = (rc==0) ? "OK" : "ERR"
         printStatus(st)
      }
   }

   printOper("Installation")
   cmd = "pkgadd " AARGS " " Oper " " tb_PKGDIR[Pkg] pak   # Oper==" " : insert ; Oper=="-u" : update
   if ("--dry" in tb_OPT) {
      print " " cmd
   } else {
      rc = system(cmd)
#     while ((cmd | getline) >0)
#        print
#     close(cmd)
      st = (rc==0) ? "OK" : "ERR"
      printStatus(st)
   } 

   tb_DB[Pkg]=tb_REPO[Pkg]

   if (POST=="y" && tb_POST[Pkg]=="yes") { 
      printOper("Post-installation")
      cmd = "/bin/bash " tb_PKGDIR[Pkg] "PKGINST " Pkg "_post_install"
      if ("--dry" in tb_OPT) {
         print " " cmd
      } else {
         rc = system(cmd)
#        while ((cmd | getline) >0)
#           print
#        close(cmd)
         st = (rc==0) ? "OK" : "ERR"
         printStatus(st)
      } 
   }
}


function isinst()
{
   load_DB()	      
   if (PKG in tb_DB)
      print _"Le paquet" " " PKG " " _"est installé"
   else
      print _"Le paquet" " " PKG " " _"n'est pas installé"
}


function list(   old_FS, FILE, j, prev)
{
   old_FS = FS
   FS="#"
   for (j=0; j<NPKGDIR; j++) {
      FILE = PKGDIR[j] REPO
      while ((getline < FILE) >0) {
         if ($1 != prev) {
            print $1
            prev = $1
         }
      }
      close(FILE)
   }
   FS = old_FS
}


function listinst(   old_FS, old_RS, fl)
{
   old_FS = FS; FS = "\t"
   old_RS = RS; RS = ""
   while (getline < DB)
      print $1
   close(DB)
   FS = old_FS
   RS = old_RS
}


function listlocked()
{
   while (getline < LOCKER)
      print $1
   close(LOCKER)
} 


function load_DB(   old_FS, old_RS, fl, pak)
{
   old_FS=FS; FS="\n"
   old_RS=RS; RS=""
   while (getline < DB)
      tb_DB[$1]=$2
   close(DB)
   FS=old_FS
   RS=old_RS
}


function load_DEPS(   j, FILE)
{
   for (j=0; j<NPKGDIR; j++) {
      FILE=PKGDIR[j] DEPS
      while ((getline < FILE) >0) {
         if (!($1 in tb_DEPS)) {
            tb_DEPS[$1] = $3
            tb_DEP_TRT[$1] = 0
            tb_DEP_DIR[$1] = PKGDIR[j]
         }
      }
      close(FILE)
   }
}


function load_LOCKER()
{
   while (getline < LOCKER)
      tb_LOCK[$1] = 1
   close(LOCKER)
}


function load_NUTYX_VERSION(   old_FS)
{
   old_FS = FS
   FS = "-"
   getline < VERSFILE
   if      ($1=="2010") NUTYX_VERSION = "attapu"
   else if ($1=="2011") NUTYX_VERSION = "pakxe"
   close(VERSFILE)    
   FS = old_FS
}


function load_RDEPS(   a, FILE, i, j, n)
{
   for (j=0; j<NPKGDIR; j++) {
      FILE=PKGDIR[j] DEPS
      while ((getline < FILE) >0) {
         n=split($3, a, ",")
         for (i=1; i<=n; i++) {
            if (a[i] in tb_RDEPS) {
               if (!index(tb_RDEPS[a[i]],$1))
                  tb_RDEPS[a[i]] = tb_RDEPS[a[i]] "," $1
            } else {
               tb_RDEPS[a[i]] = $1
            }
         }
      }
      close(FILE)
   }
}


function load_REPO(   FILE, j, old_FS)
{
   old_FS = FS
   FS="[:#]"
   for (j=0; j<NPKGDIR; j++) {
      FILE = PKGDIR[j] REPO
      while (getline < FILE) {
         if (!($1 in tb_REPO)) {
            sub(".pkg.tar.xz","",$2)
            tb_REPO[$1] = $2
            tb_MD5[$1] = $4
            tb_PRE[$1] = $6
            tb_POST[$1] = $7
            tb_READ[$1] = $8
            tb_PKGDIR[$1] = PKGDIR[j]
            tb_WEBDIR[$1] = WEBDIR[j]
         }
      }
      close(FILE)
   }
   FS = old_FS
}


function lock(   i)
{
   verif_if_root() 
   for (i=1; i<=nPKG; i++) {
      print tb_PKG[i] >>LOCKER
   }
   close(LOCKER)
} 


function path(   a, cmd, j)
{
   load_REPO()
   if (!PKG)
      print NPKG ": 'path' " _"demande un argument" 
   else
      print tb_PKGDIR[PKG]
} 


function printff()
{
   print _"Désolé, fonction" " " OPER " " _"indisponible" "..."
   exit 1
}


function printOper(texte) {
   printf "   %-" COL-4 "s%s", texte, "..."
}


function printPkg(pkg) {
   printf "\033[1;%dm%s\033[0m\n", YELLOW, pkg
}


function printStatus(status) {
   printf "\033[%dG\033[1;%dm%-4s\033[0m\n", COL, tbColor[status], status
}


function quickdep()
{
   load_DB()
   load_DEPS() 
   load_REPO()
   quickdep2(PKG)
   printf ("%s\n",PKG)
} 


function quickdep2(Pkg,    a, i, n)
{
   n=split(tb_DEPS[Pkg],a,",")
   for (i=1; i<=n; i++) {
      if (tb_DEP_TRT[a[i]]==0) {
         tb_DEP_TRT[a[i]]++
         if (a[i] in tb_DEPS)
            quickdep2(a[i])
         printf("%s ", a[i])
      }
   }
}


function quickdiff(Dest,   a, i, n, pak)
{
   load_DB()
   load_LOCKER()
   load_REPO()
   for (pak in tb_DB)
      a[pak]=pak
   n=asort(a)
   delete tb_PKG
   nPKG=0
   for (i=1;i<=n;i++) {
      pak=a[i]
      if ((pak in tb_REPO) && (!(pak in tb_LOCK)) && (tb_DB[pak]!=tb_REPO[pak])) {
         if (Dest=="tb")
            tb_PKG[++nPKG] = pak
         else
            printf("%s ", pak)
      }
   }
   printf("\n")
}


function quickhelp() {
   print "\033[1;33m" _"Usage" "\033[1;0m: " NPKG " command <package1> [package2 ... packageN] [options]"
   print "\033[1;33m" _"Quelques commandes" "\033[1;0m:"
   print "  sync               " _"synchronise avec le dépôt"
   print "  depinst            " _"installe package(s) et ses(leurs) dépendances"
   print "  info               " _"informations concernant le package"
   print "  sysup              " _"met à jour les paquets obsolètes"
   print "  diff               " _"affiche la liste des paquets obsolètes"
   print "\033[1;33m" _"Quelques options:" "\033[1;0m"
   print "  -do                " _"télécharge seulement"
   print "  --install-scripts  " _"utilise les scripts install"
   print "  -r <root>          " _"utilise" " <root> " _"pour" " pkgadd"
   print "\033[1;33m" _"Examples" "\033[1;0m:"
   print "  " NPKG " install sqlite pysqlite"
   print "  " NPKG " help"
   print _"Pour d'autres commandes et examples, voir la page de manuel"
}


function readme(   fl, j, FILE)
{
   for (j=0; j<NPKGDIR; j++) {
      FILE=PKGDIR[j] READ
      while ((getline < FILE) >0) {
         if (fl==1) {
            if (index($0,"##### PKGREADME: "))
               exit
            print $0
         }
         if (index($0,"##### PKGREADME: "PKG))
            fl=1 
      }
      close(FILE)
   }
}


function search(   j, FILE, old_FS)
{
   old_FS = FS
   FS="[:#]"
   for (j=0; j<NPKGDIR; j++) {
      FILE=PKGDIR[j] REPO
      while ((getline < FILE) >0) {
         if (index($1,ARGV[2]))
            print $1
      }
      close(FILE)
   }
   FS = old_FS
}


function sync(   cmd, i, j, pak, rc)
{
   verif_if_root()
   pak[1]="PKGREPO"
   pak[2]="PKGDEPS"
   pak[3]="PKGREAD"
   pak[4]="PKGINST"
   pak[5]="PKGGRP"
   for (j=0; j<NPKGDIR; j++) {
      if (WEBDIR[j] > "") {
         for (i=1; i<=4; i++) {
            cmd="wget -q --connect-timeout=" TIMEOUT " -O "PKGDIR[j] pak[i] " " WEBDIR[j] pak[i]
            if ("--dry" in tb_OPT) {
               print "\033[1;34m" cmd "\033[1;0m"
            } else {
               rc=system(cmd) 
               if (rc>0) {
                  print "ERREUR", rc
                  exit 1
               }
            }
         }
      }
   }
}


function sysup(   i)
{
   quickdiff("tb")
   update()
}


function unlock(   i, n, fl, tb_LCK)
{
   verif_if_root() 
   while (getline < LOCKER) {
      fl="y"
      for (i=1; i<=nPKG; i++)
         if ($1==tb_PKG[i]) 
            fl = "n"
      if (fl=="y")
         tb_LCK[++n] = $1
   }
   close(LOCKER)
   printf("") > LOCKER
   for (i=1; i<=n; i++)
      print tb_LCK[i] >> LOCKER
   close(LOCKER)
}


function update(   i, pak)
{
   verif_if_root() 
   load_LOCKER()
   load_REPO()
   load_DB()
   for (i=1; i<=nPKG; i++) {
      pak = tb_PKG[i]
      if (pak in tb_LOCK)
         print pak ": " _"paquet verouillé !"
      else
         if (!(pak in tb_DB)) 
            print pak ": " _"pas de mise à jour d'un paquet non installé !"
         else
            if (tb_DB[pak]==tb_REPO[pak])
               print pak ": " _"paquet déjà à la dernière version"
            else {
               print _"paquet à mettre à jour:" " " pak
               install2_update2(pak, "-u")
            }
   }
}


function verif_if_root(   cmd)
{
   if (!("--dry" in tb_OPT)) {
      cmd="whoami"
      cmd | getline
      if ($1 !="root") {
         print "\033[1;31m" _"Veuillez exécuter ce programme en tant que root !" "\033[1;0m"
         exit 1
      }
      close(cmd)
   }
}


function verif_md5sum(dir, pk,    cmd, md5, a, rep) 
{
   cmd="md5sum " dir pk   
   cmd | getline 
   split($0,md5," ")
   close(cmd) 
   split(pk,a,"#")
   if (md5[1]!=tb_MD5[a[1]]) {
      print "-------> " _"ERREUR" ": " _"md5sum erroné pour " a[1]
      print _"requis" ": " tb_MD5[a[1]]
      print _"trouvé" ": " md5[1]
      print "Supprimer " dir pk " <y/n> ?"
      getline rep
      if (rep=="y") {
         cmd = "rm " dir pk
         while ((cmd | getline) > 0) 
            print
         close(cmd)
      }
      exit 1
   }
}


function version()
{
   print NPKG " " VERSION " (" DATE ")"
}


function wrongparm()
{
   version()
   print _"Paramètre" " '" OPER "' " _"erroné"
   exit 1
}


#------------------------------------------------
#
#                    M   A  I  N
#
#------------------------------------------------

BEGIN {
   configs()

   if      (OPER=="")           quickhelp()
   else if (OPER=="current")    current()
   else if (OPER=="dependent")  dependent()
   else if (OPER=="depends")    depends()
   else if (OPER=="depinst")    rc=depinst()
   else if (OPER=="diff")       diff()
   else if (OPER=="dsearch")    dsearch()
   else if (OPER=="help")       help()
   else if (OPER=="info")       info()
   else if (OPER=="install")    install()
   else if (OPER=="isinst")     isinst()
   else if (OPER=="list")       list()
   else if (OPER=="listinst")   listinst()
   else if (OPER=="listlocked") listlocked()
   else if (OPER=="lock")       lock()
   else if (OPER=="path")       path()
   else if (OPER=="printf")     printff()
   else if (OPER=="quickdep")   quickdep()
   else if (OPER=="quickdiff")  quickdiff()
   else if (OPER=="readme")     readme()
   else if (OPER=="search")     search()
   else if (OPER=="sync")       sync()
   else if (OPER=="sysup")      sysup()
   else if (OPER=="unlock")     unlock()
   else if (OPER=="update")     update()
   else if (OPER=="version")    version()
   else                         wrongparm()

   exit
}