]> git.armaanb.net Git - bin.git/blob - fortune
xsel: new script
[bin.git] / fortune
1 #!/usr/bin/sh -e
2 # Fortune implementation in POSIX sh/awk
3
4 usage() {
5                 echo 'Usage: fortune [OPTIONS]
6 a simple POSIX sh/awk implementation of fortune
7
8         -h, --help                    show this help message
9         -l, --list                    list installed cookie files
10         -c FILE, --cookie FILE        specify cookie file
11         -n LENGTH, --min LENGTH       specify min fortune length
12         -N LENGTH, --max LENGTH       specify max fortune length
13
14         fortune looks for fortunes in $FORTUNEDIR, which defaults to
15         /usr/share/fortune.
16
17 Exit codes:
18         0    success
19         1    generic error
20         2    argument parsing error'
21 }
22
23 parse_err() {
24                 echo "$1"
25                 $2
26                 exit 2
27 }
28
29 DIR=${FORTUNEDIR:-"/usr/share/fortune/"}
30 FILE="$(find $DIR -type f -not -name '*.dat')"
31 MAXLENGTH=1000000000000
32 MINLENGTH=0
33
34 while [ "$1" != "" ]; do
35                 case $1 in
36                                 -h | --help)
37                                                 usage
38                                                 exit
39                                                 ;;
40                                 -c | --cookie)
41                                                 FILE=$DIR/$2
42                                                 ;;
43                                 -n | --min)
44                                                 MINLENGTH=$2
45                                                 ;;
46                                 -N | --max)
47                                                 MAXLENGTH=$2
48                                                 ;;
49                                 -l | --list)
50                                                 ls -1 $DIR
51                                                 exit
52                                                 ;;
53                                 *)
54                                                 parse_err "ERROR: unknown option \"$1\"" usage
55                                                 ;;
56                 esac
57                 shift 2
58 done
59
60 [ "$MAXLENGTH" -lt "$MINLENGTH" ] && {
61                 parse_err "ERROR: maximum length is less than minimum length"
62 }
63
64 [ -d "$FILE" ] && {
65                 parse_err "ERROR: directory given, please provide a file or glob of files"
66 }
67
68 [ "$(find $FILE -maxdepth 1 -print -quit 2> /dev/null)" ] || {
69                 parse_err "ERROR: no such file or directory"
70 }
71
72 awk -v maxlen=$MAXLENGTH -v minlen=$MINLENGTH \
73                 'BEGIN {
74         f++
75 }
76
77 $1 == "%" {
78         len = length(fortunes[f])
79         if ( len>maxlen || len<minlen ) {
80                 fortunes[f] = ""
81         } else {
82                 f++
83         }
84 }
85
86 {
87         sub("%", "")
88         fortunes[f] = fortunes[f] (length(fortunes[f])?"\n":"") $0
89 }
90
91 END {
92         "od -A n -t d -N 3 /dev/urandom" | getline seed
93         srand(seed)
94         print fortunes[int(rand() * (f - 2)) + 1]
95 }' $FILE