#!/bin/sh
exec ${GUILE-guile} -e '(ttn-do random-info-node)' -s $0 "$@" # -*-scheme-*-
!#
;;; random-info-node
;; Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010 Thien-Thi Nguyen
;;
;; This file is part of ttn-do, released under the terms of the
;; GNU General Public License as published by the Free Software
;; Foundation; either version 3, or (at your option) any later
;; version. There is NO WARRANTY. See file COPYING for details.
;;; Commentary:
;; Usage: random-info-node [--list] [--sexp] INFO
;;
;; Display a random info node from INFO file to stdout.
;; Optional arg "--list" (or "-l") means to instead display a list of nodes.
;; Optional arg "--sexp" (or "-s") means to display the list as a sexp.
;;; Code:
(define-module (ttn-do random-info-node)
#:export (main)
#:use-module ((ttn-do zzz banalities) #:select (check-hv
qop<-args))
#:use-module ((ice-9 rdelim) #:select (read-line write-line))
#:use-module ((ice-9 regex) #:select (match:substring))
#:use-module ((ttn-do zzz personally) #:select (accumulator
FE fso))
#:use-module ((ttn-do zzz subprocess) #:select (sysfmt)))
(define (main/qop qop)
(set! *random-state* (seed->random-state
(let ((pair (gettimeofday)))
(* (car pair) (cdr pair)))))
(let ((rx (make-regexp (string-append
"^Node: (.*)"
(make-string 1 (integer->char 127)))))
(acc (accumulator))
(p (or (and (pair? (qop '()))
(open-input-file (car (qop '()))))
(error "No input file specified"))))
(let loop ((line (read-line p)))
(cond ((eof-object? line)
(cond ((qop 'sexp)
(fso "~S~%" (acc)))
((qop 'list)
(FE (acc) write-line))
(else
(set! acc (acc))
(sysfmt "info -o- -n '~A' -f ~A 2>/dev/null"
(list-ref acc (random (length acc)))
(car (qop '()))))))
((regexp-exec rx line)
=> (lambda (m)
(acc (match:substring m 1))
(loop (read-line p))))
(else
(loop (read-line p)))))))
(define (main args)
(check-hv args '((package . "ttn-do")
(version . "1.2")
;; 1.2 -- better random seed init
;; 1.1 -- info(1) stderr discarded
(help . commentary)))
(main/qop
(qop<-args
args '((list (single-char #\l))
(sexp (single-char #\s))))))
;;; random-info-node ends here