Megatest

Diff
Login

Differences From Artifact [29750f33f2]:

To Artifact [6c415a23c3]:


43
44
45
46
47
48
49



50
51
52
53
54
55
56
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59







+
+
+







;; (declare (uses db))
;; (declare (uses synchash))
;; (declare (uses server))
(declare (uses megatest-version))
;; (declare (uses tbd))

(include "megatest-fossil-hash.scm")
;;; please create this file before using sautherise. For sample file is avaliable sample-sauth-paths.scm. 
(include "sauth-paths.scm")
(include "sauth-common.scm")

;;
;; GLOBALS
;;
(define *verbosity* 1)
(define *logging* #f)
(define *exe-name* (pathname-file (car (argv))))
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
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







-
+
-
-
-
-
+
+
+
+
-
-
+
-
-
+
-
-
+
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
+
-
-
+
-
-
+
-
-
+
+
-
-
+
-
-
-
+
+
-
-
+

-
-
-
-
-
-
-
-
-
-
-
-


-
+
-
-
-
-
-
+
+
-
-
+
-
-
-
-
+
+
+
-
-
-
-
-
+
+
+
-
-
-
-
+
+
+

-
+
+



-
-
+
+


-
-
-
-
-












-
-
+
+




-
+




-
-
-
-
-
+







	      (if (not dbexists)(sretrieve:initialize-db db))
	      (proc db)))))
	(debug:print 0 "ERROR: invalid path for storing database: " path)))



;; function to find sheets to which use has access 
(define (sretrieve:has-permission sheet configfile)
(define (sretrieve:has-permission  area)
  (let* ((users (get-rowcol-names  configfile sheet car))
          (retuser ""))
    (if (member (current-user-name) users)
        #t
  (let ((username     (current-user-name)))
  (cond
   ((is-admin username)
     #t)
        #f))) 
    
   ((is-user "retrieve" username area) 
 ;; function  to check if user is trying to access a restricted area
 
     #t)
(define (sretrieve:is-permitted-area dir allowed-list)
    (for-each 
    ((is-user "publish" username area) 
        (lambda (allowed-dir)
         (if (equal? dir allowed-dir)
          allowed-dir))
        (cdr allowed-list)))

     #t)
   ((is-user "writer-admin" username area) 
;; function to validate the users input for target path and resolve the path
;; TODO: Check for restriction in subpath 
(define (sretrieve:resolve-path  new current allowed-sheets)
   (let* ((target-path (append  current (string-split new "/")))
          (target-path-string (string-join target-path "/"))
          (normal-path (normalize-pathname target-path-string))
          (normal-list (string-split normal-path "/"))
          ;(sheet (car normal-list))
           (ret '()))
     #t)
   (if (string-contains   normal-path "..")
    (begin
   ((is-user "read-admin" username area) 
      (print "ERROR: Path  " new " resolved outside target area ")
      #f)
     #t)
    (if(equal? normal-path ".")
      ret  
   ((is-user "area-admin" username area) 
     #t)
    (if (not (member  (car normal-list) allowed-sheets))
      (begin
   (else  
      (print "ERROR: Permision denied to  " new )
       #f)
    normal-list)))
    #f))))
 
))

    

(define (sretrieve:is-access-valid sheet configfile)
   (let* ((exp-str (lookup configfile sheet (current-user-name) "expiration")))
    (if (equal? exp-str #f)
        #f
        (let* ((date-parts  (string-split exp-str "/"))
                (yr (string->number (car date-parts)))
                (month (string->number(car (cdr date-parts)))) 
                (day (string->number(caddr date-parts)))
                (exp-date (make-date 0 0 0 0 day month yr )))
               (if (< (date-compare exp-date  (current-date)) 1)
                   #f
                   #t)))))


(define (sretrieve:get-accessable-projects  sheets configfile)
(define (sretrieve:get-accessable-projects  area)
 ;;(print sheets)
 (if (null? sheets)
      #f
      (let loop ((hed (car sheets))
		 (tal (cdr sheets))
   (let* ((projects `()))
         
                  (res '()))
        (let* ((user (sretrieve:has-permission hed configfile))
        (if (sretrieve:has-permission area)
              (access-valid (sretrieve:is-access-valid  hed configfile)))
        (if (and (equal? user #t ) (equal? access-valid #t))
               (begin  
               ;;(print "got perm " (sretrieve:has-permission hed configfile)) 
               (set! projects (cons area projects))
               (begin
                 (print "User cannot access area " area "!!")  
               (if (null? tal)
		 (cons hed res)
		 (loop (car tal)(cdr tal)(cons hed res))))
                (if (null? tal)
                     res
                (exit 1))) 
   ; (print projects)
    projects))
                   (loop (car tal)(cdr tal) res)))))))

(define (sretrieve:shell-ls-cmd base-path-list ext-path top-areas configfile db-location tail-cmd-list)
  (if (and (null? base-path-list) (equal? ext-path "") )
		 
(define (sauth-common:shell-ls-cmd base-path-list ext-path top-areas base-path tail-cmd-list)
    (if (and (null? base-path-list) (equal? ext-path "") )
      (print (string-intersperse top-areas " "))
  (let* ((resolved-path (sretrieve:resolve-path ext-path base-path-list top-areas )))
  (let* ((resolved-path (sauth-common:resolve-path ext-path base-path-list top-areas )))
           ;(print resolved-path)
           (if (not (equal? resolved-path #f))
           (if (null? resolved-path) 
             (print (string-intersperse top-areas " "))
           (let* ((target-path (sretrieve:get-target-path  base-path-list  ext-path top-areas configfile)))
                ;(print "Resolved path: " target-path)
           (let* ((target-path (sauth-common:get-target-path  base-path-list  ext-path top-areas base-path)))
                (print "Resolved path: " target-path)
                (if (not (equal? target-path #f))
                (begin 
                 
                (sretrieve:shell-db-do
         	 db-location
     		(lambda (db)
       		(sretrieve:register-action db "ls" (current-user-name) target-path (conc "Executing cmd: ls "  target-path))))
                (cond
		  ((null? tail-cmd-list)
		     (run (pipe
      	      	      (ls "-lrt" ,target-path))))
		  ((not (equal? (car tail-cmd-list) "|"))
                         (print "ls cmd cannot accept "(string-join tail-cmd-list) " as an argument!!"))
                  (else  
                    (run (pipe
      	      	      (ls "-lrt" ,target-path)
                      (begin (system (string-join (cdr tail-cmd-list))))))
      ))))))))))

(define (sretrieve:shell-cat-cmd base-pathlist ext-path top-areas configfile db-location tail-cmd-list)
  (let* ((resolved-path (sretrieve:resolve-path ext-path base-pathlist top-areas ))
(define (sretrieve:shell-cat-cmd base-pathlist ext-path top-areas base-path tail-cmd-list)
  (let* ((resolved-path (sauth-common:resolve-path ext-path base-pathlist top-areas ))
          (data "") )
         (if (not (equal? resolved-path #f))
           (if (null? resolved-path) 
             (print "Path could not be resolved!!")
           (let* ((target-path (sretrieve:get-target-path  base-pathlist  ext-path top-areas configfile)))
           (let* ((target-path (sauth-common:get-target-path  base-pathlist  ext-path top-areas base-path)))
             (if (not (equal? target-path #f))
               (if (or (not (file-exists? target-path)) (directory? target-path))
               (print "Target path does not exist or is a directory!") 
               (begin 
                 (sretrieve:shell-db-do
         	    db-location
     		    (lambda (db)
       		      (sretrieve:register-action db "cat" (current-user-name) target-path (conc "Executing cmd: cat " target-path))))   
		 (cond
                 (cond
		  ((null? tail-cmd-list)
		     (run (pipe
      	      	      (cat  ,target-path))))
		  ((not (equal? (car tail-cmd-list) "|"))
                         (print "cat cmd cannot accept "(string-join tail-cmd-list) " as an argument!!"))
                  (else  
                    (run (pipe
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
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







-
-
+
+






-
+



-
+


-
+




-
-
-
-
-
-
-
-
-
-







                      (list res tal))
                   ((null? tal)
                      (list (cons hed res) tal)) 
                   (else 
		  	(loop (car tal)(cdr tal)(cons hed res)))))))

  
(define (sretrieve:shell-grep-cmd base-pathlist ext-path top-areas configfile db-location tail-cmd-list)
  (let* ((resolved-path (sretrieve:resolve-path ext-path base-pathlist top-areas ))
(define (sretrieve:shell-grep-cmd base-pathlist ext-path top-areas base-path tail-cmd-list)
  (let* ((resolved-path (sauth-common:resolve-path ext-path base-pathlist top-areas ))
          (pattern (car tail-cmd-list))
          (pipe-cmd-list (get-options (cdr tail-cmd-list) "|"))
          (options (string-join (car pipe-cmd-list)))
          (pipe-cmd (cadr  pipe-cmd-list))
          (redirect-split (string-split (string-join  tail-cmd-list) ">"))   )
         (if(and ( > (length redirect-split) 2 ))
            (print "grep  cmd cannot accept > " (string-join redirect-split) " as an argument!!" )  
            (print "sgrep  cmd cannot accept > " (string-join redirect-split) " as an argument!!" )  
         (if (not (equal? resolved-path #f))
           (if (null? resolved-path) 
             (print "Path could not be resolved!!")
           (let* ((target-path (sretrieve:get-target-path  base-pathlist  ext-path top-areas configfile))
           (let* ((target-path (sauth-common:get-target-path  base-pathlist  ext-path top-areas base-path))
                  (restrictions (if (equal? target-path #f)
                                                 ""
                                               (sretrieve:shell-lookup base-pathlist ext-path  top-areas configfile)))
                                               (sretrieve:shell-lookup base-path)))
                 (rest-str  (string-split (conc " --exclude-dir=" (string-join (string-split restrictions ",") " --exclude-dir=") ))))
             (if (not (file-exists? target-path))
               (print "Target path does not exist!") 
               (begin 
                 (sretrieve:shell-db-do
         	    db-location
     		    (lambda (db)
       		      (sretrieve:register-action db "grep" (current-user-name) target-path (conc "Executing cmd: grep " target-path pattern (string-join tail-cmd-list) )))) 
              ; (sretrieve:do-as-calling-user
    	      ;    (lambda ()
              ;     (if (null? pipe-cmd)
              ;     (process-execute "/usr/bin/grep" (append (list options pattern target-path)  rest-str))
              ;     (process-execute "/usr/bin/grep" (append (append (list options pattern target-path) rest-str) (append (list "|") pipe-cmd))))))
               ;  (print rest-str) 
                 (cond
	 	  ((and (null? pipe-cmd) (string-null? options))
	 	     (run (pipe
      	      	      (grep ,pattern ,target-path ))))
		  ((and (null? pipe-cmd) (not (string-null? options)))
		     (run (pipe
      	      	      (begin (process-execute "/usr/bin/grep" (append (list options pattern target-path) rest-str))))))
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
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







-
-
+
+



-
+




-
-
-
-
+
+
+
+







-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
+
-
-
+
-
-
-
-
+
-
-
+







                       (begin (process-execute "/usr/bin/grep" (append (list options pattern target-path) rest-str)))

                      (begin (system (string-join  pipe-cmd)))))))
))))
             (print "Path could not be resolved!!")))))


(define (sretrieve:shell-less-cmd base-pathlist ext-path top-areas configfile db-location)
  (let* ((resolved-path (sretrieve:resolve-path ext-path base-pathlist top-areas )))
(define (sretrieve:shell-less-cmd base-pathlist ext-path top-areas base-path)
  (let* ((resolved-path (sauth-common:resolve-path ext-path base-pathlist top-areas )))
         (if (not (equal? resolved-path #f))
           (if (null? resolved-path) 
             (print "Path could not be resolved!!")
           (let* ((target-path (sretrieve:get-target-path  base-pathlist  ext-path top-areas configfile)))
           (let* ((target-path (sauth-common:get-target-path  base-pathlist  ext-path top-areas base-path)))
               (if (not (equal? target-path #f))
           	 (if (or (not (file-exists? target-path)) (directory? target-path))
                   (print "Target path does not exist or is a directory!") 
                   (begin 
                     (sretrieve:shell-db-do
         	          db-location
     			  (lambda (db)
       				(sretrieve:register-action db "less" (current-user-name) target-path (conc "Executing cmd: less "  target-path))))   
                     ;(sretrieve:shell-db-do
         	     ;     db-location
     		     ;	  (lambda (db)
       		;		(sretrieve:register-action db "less" (current-user-name) target-path (conc "Executing cmd: less "  target-path))))   
 
                     (setenv "LESSSECURE" "1")
                     (run (pipe
      	      	     (less ,target-path))))))))
             (print "Path could not be resolved!!"))))


(define (sretrieve:get-target-path base-path-list ext-path top-areas configfile)
  (let* ((resolved-path (sretrieve:resolve-path ext-path base-path-list top-areas ))
          (usr (current-user-name) ) )
          (if (not (equal? resolved-path #f))
           (if (null? resolved-path) 
             #f
           (let* ((sheet (car resolved-path))
                   (fname  (conc  configfile "/" sheet ".dat"))
                   (config-data (sretrieve:load-shell-config fname))  
        	   (base-path (configf:lookup config-data "basepath" usr))
                   (restrictions (conc ".*" (string-join (string-split (configf:lookup config-data "restricted areas" usr) ",") ".*|.*") ".*"))
           	   (target-path (conc base-path "/" (string-join (cdr resolved-path) "/"))))
                    

                    (if (string-match (regexp  restrictions) target-path) 
                        (begin
                          (print "Access denied to " (string-join resolved-path "/"))   
                         #f)
                                        target-path)))
             #f)))

(define (sretrieve:shell-lookup base-path-list ext-path top-areas configfile)
(define (sretrieve:shell-lookup base-path)
  (let* ((resolved-path (sretrieve:resolve-path ext-path base-path-list top-areas ))
          (usr (current-user-name))
  (let* ((usr (current-user-name))
          (sheet (car resolved-path))
          (fname  (conc  configfile "/" sheet ".dat"))
          (config-data (sretrieve:load-shell-config fname))  
          (base-path (configf:lookup config-data "basepath" usr))
          (value (get-restrictions base-path usr)))
          (value (configf:lookup config-data "restricted areas" usr)))
                    value))
                              value))
             

(define (sretrieve:load-shell-config fname)
          (if (file-exists? fname)
	(read-config fname #f #f)
	))

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
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







-
+
+
+
+


-
-
-
-
-
+


+
+
+
-
+
+

+
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


-
-
+
+

-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+



-
+
+
+


















-
-
+
+





-
-
-
-
-
-
-
-
+
-
-
+
+
+
+
+
+

-
-
-
+
+
+
+

+




+







-
-
+
+



+
-
+
+
+
+
+










-
+

-
+
+
+
+

+
-
+
+
+
+


-
-
+
+
+
+
+









-
+
+
+
+
+

-
-
+
+
+
+
+
+










-
+
+
+
+








-
+

-
+
+
+
+

-
-
-
+









-
+


-
+
-
-
-
-
+
+
+
+

-
+
+
+



-
+
-
-
+
-







                  (ret-str ""))
                   (cond
                   ((null? tal)
                      (conc ret-str " --exclude='*" hed "*'")) 
                   (else 
		  	(loop (car tal)(cdr tal)(conc ret-str " --exclude='*" hed "*'"))))))    )

(define (sretrieve:get-shell-cmd target-path db-location restrictions)
(define (sretrieve:get-shell-cmd target-path base-path restrictions iport)
     (if (not (file-exists? target-path))
        (print "Target path does not exist!")
    (begin
    (if (not (equal? target-path #f))
    (begin     
    (sretrieve:shell-db-do
    	  db-location
     	  (lambda (db)
       		(sretrieve:register-action db "get" (current-user-name) target-path (conc "Executing cmd: get "  target-path))))
     (if (is_directory target-path) 
        (if (is_directory target-path) 
        (begin
           (let* ((parent-dir target-path)
                  (last-dir-name (if  (pathname-extension target-path)  
                                      (conc(pathname-file target-path) "." (pathname-extension target-path))
                                      (pathname-file target-path)))
                  (start-dir (current-directory))
                  (curr-dir (current-directory))   
                  (start-dir (conc (current-directory) "/" last-dir-name))
                  (execlude (make-exclude-pattern (string-split restrictions ","))))
                   (print execlude) 
                   (print (file-exists? start-dir))
                  (if  (file-exists? start-dir)
                    (begin
                         (print last-dir-name " already exist in your work dir. Do you want to over write it? [y|n]")
                        (let* ((inl (read-line iport)))
                            (if (equal? inl "y")
                              (begin
                  (change-directory parent-dir)
                                    
                 (run (pipe
                   (tar "chfv" "-" "." )
                   (begin (system (conc "cd " start-dir ";tar  xf - "   execlude )))))
                 )) 
                                 (change-directory parent-dir)
                                  (run (pipe
                   		   (tar "chfv" "-" "." )
                   		   (begin (system (conc "cd " start-dir ";tar  xUf - "   execlude )))))
                                   (change-directory curr-dir))
			      (begin	
                               (print  "Nothing has been retrieved!!  ")))))
                     (begin
                       (sretrieve:do-as-calling-user
                    (lambda ()
		      (create-directory start-dir #t)))
                          (change-directory parent-dir)
                                  (run (pipe
                   		   (tar "chfv" "-" "." )
                   		   (begin (system (conc "cd " start-dir ";tar  xUf - "   execlude )))))
                           (change-directory curr-dir))))) 
        (begin
           (let*((parent-dir (pathname-directory target-path))
                  (start-dir (current-directory))
                (filename (if  (pathname-extension target-path)  
                 (start-dir (current-directory))
                 (filename (if  (pathname-extension target-path)  
                                      (conc(pathname-file target-path) "." (pathname-extension target-path))
                                      (pathname-file target-path))))
                                      (pathname-file target-path)))
                 (work-dir-file (conc (current-directory) "/" filename)))
                 (if  (file-exists? work-dir-file)
                    (begin
                       (print filename " already exist in your work dir. Do you want to over write it? [y|n]")
                        (let* ((inl (read-line iport)))
                            (if (equal? inl "y")
                              (begin
                                 (change-directory parent-dir)
                                  (run (pipe
                   		   (tar "chfv" "-" "." )
                   		   (begin (system (conc "cd " start-dir ";tar  xUf - "   execlude )))))
                                     (change-directory curr-dir))
			      (begin	
                               (print  "Nothing has been retrieved!!  ")))))
                    (begin
               (change-directory parent-dir)  
                 (run (pipe
                   (tar "chfv" "-" ,filename)
                   (begin (system (conc "cd " start-dir ";tar xf -")))))))))))
                   (begin (system (conc "cd " start-dir ";tar xUf -")))))
                    (change-directory start-dir))))))))
                     (print (current-directory)))))

(define (sretrieve:shell-help)
(conc "Usage: " *exe-name* " [action [params ...]]

  ls    [target path]               	  : list contents of target area. The output of the cmd can be piped into other system cmd. eg ls <path> | grep txt
  cd    <target directory>	     	  : To change the current directory within the sretrive shell. 
  pwd				     	  : Prints the full pathname of the current directory within the sretrive shell.
  get   <file or directory path>     	  : download directory/files into the directory where sretrieve shell cmd was invoked   
  less  <file path>		     	  : Read input file to allows backward movement in the file as well as forward movement 
  cat   <file path>                  	  : show the contents of a file. The output of the cmd can be piped into other system cmd.

  sgrep <search path> <pattern> [options] : Similar to unix grep cmd But with diffrent parameter ordering. The output of the cmd can be piped into other system cmd. 
Part of the Megatest tool suite.
Learn more at http://www.kiatoa.com/fossils/megatest

Version: " megatest-fossil-hash)
)	
(define (toplevel-command . args) #f)
(define (sretrieve:shell)
 ;; (print (current-effective-user-id))
(define (sretrieve:shell area)
 ; (print area)
  (use readline)
  (let* ((path      '())
	 (prompt    "sretrieve> ")
	 (args      (argv))
         (usr (current-user-name) )   
	 (prog      (car args)) 
         (exe-name  (pathname-file (car (argv))))
	 (exe-dir   (or (pathname-directory prog)
			(sretrieve:find exe-name (string-split (get-environment-variable "PATH") ":"))))
         (config-file  (conc exe-dir "/sretrieve_configs"))  
         (db-location (conc exe-dir "/db"))
         (sheets (list-sheets config-file)) 
         (top-areas (sretrieve:get-accessable-projects sheets config-file))
         (top-areas (sretrieve:get-accessable-projects area))
       
          (close-port     #f)     
         (close-port     #f)
         (area-obj  (get-obj-by-code area))
         (user-obj (get-user usr)) 
         (base-path (if (null? area-obj) 
                         "" 
                        (caddr (cdr area-obj))))      
	 (iport     (make-readline-port prompt)))
  ;  (install-history-file) ;;  [homedir] [filename] [nlines])
  ;  (with-input-from-port iport
  ;    (lambda ()
        (if (null? area-obj)
          (begin 
             (print "Area " area " does not exist")
          (exit 1)))
	(let loop ((inl (read-line iport)))
          ;(print 1) 
	  (if (not (or (or (eof-object? inl)
		       (equal? inl "exit")) (port-closed? iport)))
	      (let* ((parts (string-split inl))
		     (cmd   (if (null? parts) #f (car parts))))
              ;  (print "2")
		(if (and (not cmd) (not (port-closed? iport)))
		    (loop (read-line))
		    (case (string->symbol cmd)
		      ((cd)
		       (if (> (length parts) 1) ;; have a parameter
                           (begin
                             (let*((arg (cadr parts))
                                   (resolved-path (sretrieve:resolve-path  arg path top-areas))
                                   (target-path (sretrieve:get-target-path path  arg top-areas config-file)))
                                   (resolved-path (sauth-common:resolve-path  arg path top-areas))
                                   (target-path (sauth-common:get-target-path path  arg top-areas base-path)))
                                 (if (not (equal? target-path #f))
                                 (if (or (equal? resolved-path #f) (not (file-exists? target-path)))    
                                 (print "Invalid argument " arg ".. ")
                                  (begin      
			         (set! path resolved-path)))))  
			            (set! path resolved-path)
                                     (sauthorize:do-as-calling-user
                              (lambda ()
			    (run-cmd (conc *sauth-path* "/sauthorize") (list "register-log" (conc "\"" inl "\"") (number->string (car user-obj))  (number->string (caddr area-obj))  "cd"))))
                                  )))))  
   			   (set! path '())))
                      ((pwd)
                         (if (null? path)
                           (print "/")  
                           (print "/" (string-join path "/")))) 
		      ((ls)
		       (let* ((thepath (if (> (length parts) 1) ;; have a parameter
					   (cdr parts)
					   `()))
			      (plen    (length thepath)))
			 (cond
                         (cond
			  ((null? thepath)
                           (sretrieve:shell-ls-cmd path "" top-areas config-file db-location '()))
                           (sauth-common:shell-ls-cmd path "" top-areas base-path  '())
                            (sauthorize:do-as-calling-user
                              (lambda ()
			    (run-cmd (conc *sauth-path* "/sauthorize") (list "register-log" (conc "\"" inl "\"") (number->string (car user-obj))  (number->string (caddr area-obj))  "ls"))))   )
			  ((< plen 2)
                  
                            (sretrieve:shell-ls-cmd path  (car thepath) top-areas config-file db-location  '()))
                            (sauth-common:shell-ls-cmd path  (car thepath) top-areas base-path '())
                              (sauthorize:do-as-calling-user
                              (lambda ()
			    (run-cmd (conc *sauth-path* "/sauthorize") (list "register-log" (conc "\"" inl "\"") (number->string (car user-obj))  (number->string (caddr area-obj))  "ls")))))
                          (else 
                            (if (equal? (car thepath) "|")
                              (sretrieve:shell-ls-cmd path "" top-areas config-file db-location thepath)
                              (sretrieve:shell-ls-cmd path  (car thepath) top-areas config-file db-location (cdr thepath)))))))
                              (sauth-common:shell-ls-cmd path "" top-areas base-path thepath)
                              (sauth-common:shell-ls-cmd path  (car thepath) top-areas base-path (cdr thepath)))
                           (sauthorize:do-as-calling-user
                              (lambda ()
			    (run-cmd (conc *sauth-path* "/sauthorize") (list "register-log" (conc "\"" inl "\"") (number->string (car user-obj))  (number->string (caddr area-obj))  "ls"))))))))
                       ((cat)
		       (let* ((thepath (if (> (length parts) 1) ;; have a parameter
					   (cdr parts)
					   `()))
			      (plen    (length thepath)))
			 (cond
			  ((null? thepath)
                          (print "Error: Missing argument to cat"))
			  ((< plen 2)
                            (sretrieve:shell-cat-cmd path  (car thepath) top-areas config-file db-location '()))
                            (sretrieve:shell-cat-cmd path  (car thepath) top-areas base-path '())
                              (sauthorize:do-as-calling-user
                              (lambda ()
			    (run-cmd (conc *sauth-path* "/sauthorize") (list "register-log" (conc "\"" inl "\"") (number->string (car user-obj))  (number->string (caddr area-obj))  "cat")))))

			  (else
                             (sretrieve:shell-cat-cmd path  (car thepath) top-areas config-file db-location (cdr thepath))))))
                       ((grep)
                             (sretrieve:shell-cat-cmd path  (car thepath) top-areas base-path (cdr thepath))
                             (sauthorize:do-as-calling-user
                              (lambda ()
			    (run-cmd (conc *sauth-path* "/sauthorize") (list "register-log" (conc "\"" inl "\"") (number->string (car user-obj))  (number->string (caddr area-obj))  "cat"))))
))))
                       ((sgrep)
		       (let* ((thepath (if (> (length parts) 1) ;; have a parameter
					   (cdr parts)
					   `()))
			      (plen    (length thepath)))
			 (cond
			  ((null? thepath) 
                            (print "Error: Missing arguments to grep!! Useage: grep <search path> <pattern> [options] "))
                          ((< plen  2)
                          (print "Error: Missing arguments to grep!! Useage: grep <search path> <pattern> [options] "))
			  (else
                             (sretrieve:shell-grep-cmd path  (car thepath) top-areas config-file db-location (cdr thepath))))))
                             (sretrieve:shell-grep-cmd path  (car thepath) top-areas base-path  (cdr thepath))
				(sauthorize:do-as-calling-user
                              (lambda ()
			    (run-cmd (conc *sauth-path* "/sauthorize") (list "register-log" (conc "\"" inl "\"") (number->string (car user-obj))  (number->string (caddr area-obj))  "grep"))))))))

                      ((less)
		       (let* ((thepath (if (> (length parts) 1) ;; have a parameter
					   (cdr parts)
					   `()))
			      (plen    (length thepath)))
			 (cond
			  ((null? thepath)
                          (print "Error: Missing argument to cat"))
                          (print "Error: Missing argument to less"))
			  ((< plen 2)
                            (sretrieve:shell-less-cmd path  (car thepath) top-areas config-file db-location))
                            (sretrieve:shell-less-cmd path  (car thepath) top-areas base-path)
                             (sauthorize:do-as-calling-user
                              (lambda ()
			    (run-cmd (conc *sauth-path* "/sauthorize") (list "register-log" (conc "\"" inl "\"") (number->string (car user-obj))  (number->string (caddr area-obj))  "less")))))
			  (else
                             ;(sretrieve:shell-cat-cmd path  (car thepath) top-areas config-file))
))))

                             (print "less cmd takes only one (<file path>) argument!!")))))
                      ((get)
                         (let* ((thepath (if (> (length parts) 1) ;; have a parameter
					   (cdr parts)
					   `()))
			      (plen    (length thepath)))
			 (cond
			  ((null? thepath)
                          (print "Error: Missing argument <path> to get"))
			  ((< plen 2)
                           (let* ((target-path (sretrieve:get-target-path path  (car thepath) top-areas config-file))
                           (let* ((target-path (sauth-common:get-target-path path  (car thepath) top-areas base-path))
				 (restrictions (if (equal? target-path #f)
                                                 ""
                                               (sretrieve:shell-lookup path  (car thepath) top-areas config-file))))
                                               (sretrieve:shell-lookup base-path))))
                                    
                                  (sretrieve:get-shell-cmd target-path db-location restrictions)
                                  ;;(print path)
                           ))
                                 (sretrieve:get-shell-cmd target-path base-path restrictions iport)
                                  (sauthorize:do-as-calling-user
                              (lambda ()
			    (run-cmd (conc *sauth-path* "/sauthorize") (list "register-log" (conc "\"" inl "\"") (number->string (car user-obj))  (number->string (caddr area-obj))  "get")))) ))
			  (else
                            (print "Error: get cmd takes only one argument "))))) 
                            (print "Error: get cmd takes only one argument ")))))
                      ((exit)
                          (print "got exit"))  
                      ((help)
                          (print (sretrieve:shell-help)))
		      (else 
		       (print "Got command: " inl)
		       (print "Got command: " inl))))
                       )))
                 (loop (read-line iport))
                 (loop (read-line iport)))))))
                )))))
;;))
    

;;======================================================================
;; MAIN
;;======================================================================
;;(define *default-log-port* (current-error-port))
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
887
888
889
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







-
-
-
-
+


















-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-












-
-








(define (sretrieve:load-packages configdat exe-dir package-type)
  (push-directory exe-dir)
  (let* ((packages-metadir  (configf:lookup configdat "settings" "packages-metadir"))
	 (conversion-script (configf:lookup configdat "settings" "conversion-script"))
	 (upstream-file     (configf:lookup configdat "settings" "upstream-file"))
	 (package-config    (conc packages-metadir "/" package-type ".config")))
    ;; this section here does a timestamp based rebuild of the
    ;;   <packages-metadir>/<package-type>.config file using
    ;;   <upstream-file> as an input
    (if (file-exists? upstream-file)
       (if (file-exists? upstream-file)
	(if (or (not (file-exists? package-config)) ;; if not created call the updater, otherwise call only if upstream newer
		(> (file-modification-time upstream-file)(file-modification-time package-config)))
	    (handle-exceptions
	     exn
	     (debug:print 0 "ERROR: failed to run script " conversion-script " with params " upstream-file " " package-config)
	     (let ((pid (process-run conversion-script (list upstream-file package-config))))
	       (process-wait pid)))
	    (debug:print 0 "Skipping update of " package-config " from " upstream-file))
	(debug:print 0 "Skipping update of " package-config " as " upstream-file " not found"))
       (let ((res (if (file-exists? package-config)
		   (begin
		     (debug:print 0 "Reading package config " package-config)
		     (read-config package-config #f #t))
		   (make-hash-table))))
      (pop-directory)
      res)))

(define (sretrieve:process-action configdat action . args)
  (let* ((base-dir      (configf:lookup configdat "settings" "base-dir"))
	 (user          (current-user-name))
         (allowed-sub-paths (configf:lookup configdat "settings" "allowed-sub-paths")) 
	 (allowed-users (string-split
			 (or (configf:lookup configdat "settings" "allowed-users")
			     "")))
	 (default-area  (configf:lookup configdat "settings" "default-area"))) ;; otherwise known as the package
    
    (if (not base-dir)
	(begin
	  (debug:print 0 "[settings]\nbase-dir /some/path\n\n Is MISSING from the config file!")
	  (exit)))
    (if (null? allowed-users)
	(begin
	  (debug:print 0 "[setings]\nallowed-users user1 user2 ...\n\n Is MISSING from the config file!")
	  (exit)))
    (if (not (member user allowed-users))
	(begin
	  (debug:print 0 "User \"" (current-user-name) "\" does not have access. Exiting")
	  (exit 1)))
    (case (string->symbol action)
      ((get)
       (if (< (length args) 1)
	   (begin 
	     (debug:print 0 "ERROR: Missing arguments; " (string-intersperse args ", "))
	     (exit 1)))
       (let* ((remargs     (args:get-args args '("-m" "-i" "-package") '() args:arg-hash 0))
              (version     (car args))
	      (msg         (or (args:get-arg "-m") ""))
	      (package-type (or (args:get-arg "-package")
				default-area))
	      (exe-dir     (configf:lookup configdat "exe-info" "exe-dir")))
;;	      (relconfig   (sretrieve:load-packages configdat exe-dir package-type)))

	 (debug:print 0 "retrieving " version " of " package-type " as tar data on stdout")
	 (sretrieve:get configdat user version msg)))
         ((cp)
            (if (< (length args) 1)
             (begin 
	     (debug:print 0 "ERROR: Missing arguments; " (string-intersperse args ", "))
	     (exit 1)))
900
901
902
903
904
905
906






907
908


909
910
911
912
913
914
915
916
917
918
919
920
862
863
864
865
866
867
868
869
870
871
872
873
874


875
876





877
878
879
880
881
882
883







+
+
+
+
+
+
-
-
+
+
-
-
-
-
-







	     (exit 1)))
          (let* ((remargs     (args:get-args args '("-m" "-i" "-package") '() args:arg-hash 0))
              (dir     (car args))
	      (msg         (or (args:get-arg "-m") "")) )

	 (debug:print 0 "Listing files in " )
	 (sretrieve:ls configdat user dir msg)))
       ((shell)
          (if (< (length args) 1)
             (begin 
	     (print  "ERROR: Missing arguments area!!" )
	     (exit 1))
             (sretrieve:shell (car args)))
 
      (else (debug:print 0 "Unrecognised command " action)))))
       ) 
      (else (debug:print 0 "Unrecognised command " action))))
  
;; ease debugging by loading ~/.dashboardrc - REMOVE FROM PRODUCTION!
;; (let ((debugcontrolf (conc (get-environment-variable "HOME") "/.sretrieverc")))
;;   (if (file-exists? debugcontrolf)
;;       (load debugcontrolf)))

(define (main)
  (let* ((args      (argv))
	 (prog      (car args))
	 (rema      (cdr args))
	 (exe-name  (pathname-file (car (argv))))
	 (exe-dir   (or (pathname-directory prog)
943
944
945
946
947
948
949
950
951


952
953
954
955
956
957
958
959
960
961
962
963
906
907
908
909
910
911
912


913
914
915
916
917
918
919
920
921
922
923
924
925
926







-
-
+
+












	((log)
	 (sretrieve:db-do configdat (lambda (db)
				     (print "Logs : ")
				     (query (for-each-row
					     (lambda (row)
					       (apply print (intersperse row " | "))))
					    (sql db "SELECT * FROM actions")))))
	((shell)
	 (sretrieve:shell))
	;((shell)
	; (sretrieve:shell))
	(else
	 (print "ERROR: Unrecognised command. Try \"sretrieve help\""))))
     ;; multi-word commands
     ((null? rema)(print sretrieve:help))
     ((>= (length rema) 2)
      (apply sretrieve:process-action configdat (car rema)(cdr rema)))
     (else (debug:print 0 "ERROR: Unrecognised command. Try \"sretrieve help\"")))))

(main)