;;; Gash -- Guile As SHell
;;; Copyright © 2018 Timothy Sample <samplet@ngyro.com>
;;;
;;; This file is part of Gash.
;;;
;;; Gash is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; Gash is distributed in the hope that it will be useful, but WITHOUT ANY
;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
;;; FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
;;; details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Gash.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gash built-ins echo)
  #:use-module (ice-9 getopt-long)

  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)

  #:use-module (gash config)
  #:export (echo))

;;; Commentary:
;;;
;;; The 'echo' utility.
;;;
;;; Code:

(define (escape->control string)
  (list->string
   (let loop ((lst (string->list string)))
     (if (null? lst) '()
         (let ((char (car lst)))
           (if (or (not (eq? char #\\))
                   (null? (cdr lst))) (cons char (loop (cdr lst)))
                   (let* ((lst (cdr lst))
                          (char (car lst)))
                     (case char
                       ((#\\) (cons #\\ (loop (cdr lst))))
                       ((#\a) (cons #\alarm (loop (cdr lst))))
                       ((#\b) (cons #\backspace (loop (cdr lst))))
                       ((#\c) '())
                       ((#\e) (cons #\escape (loop (cdr lst))))
                       ((#\f) (cons #\page (loop (cdr lst))))
                       ((#\n) (cons #\newline (loop (cdr lst))))
                       ((#\r) (cons #\return (loop (cdr lst))))
                       ((#\t) (cons #\tab (loop (cdr lst))))
                       ((#\v) (cons #\vtab (loop (cdr lst))))
                       ((#\0) (error "echo: TODO: \\0NNN"))
                       ((#\x) (error "echo: TODO: \\xNNN"))))))))))

(define (echo . args)
  (let* ((option-spec
	  '((no-newline (single-char #\n))
            (escapes (single-char #\e))
            (no-escapes (single-char #\E))

            (help (single-char #\h))
            (version (single-char #\V))))
         ;; FIXME: getopt-long's should really have an option to stop
         ;; at first non-recognized option too
         ;; Revert to previous all manual parsing and add these?
         (sanitized-args (cons "echo"
                               (take-while (cut member <> '("-E" "-e" "-n" "--help" "--version"
                                                            "-en" "-ne" "-En" "-Ne")) args)))
	 (options (getopt-long sanitized-args option-spec))

         (escapes? (option-ref options 'escapes #f))
         (no-newline? (option-ref options 'no-newline #f))
	 (help? (option-ref options 'help #f))
         (version? (option-ref options 'version #f))
	 ;; (files (option-ref options '() '()))
         (files (drop args (1- (length sanitized-args))))
         (usage? #f))
    (cond (version? (format #t "echo (GASH) ~a\n" %version) (exit 0))
          ((or help? usage?) (format (if usage? (current-error-port) #t)
                                     "\
Usage: echo [OPTION]... [STRING]...
  -n             do not output the trailing newline
  -e             enable interpretation of backslash escapes
  -E             disable interpretation of backslash escapes (default)
      --help     display this help and exit
      --version  output version information and exit
")
           (exit (if usage? 2 0)))
          (else
           (let* ((string (string-join files))
                  (string (if escapes? (escape->control string) string)))
             (display string))
           (unless no-newline?
             (newline))
           0))))

(define main echo)
