summaryrefslogtreecommitdiff
path: root/rfc.sh
blob: 461b62b494a601fb6432a8e60313fdffec97f422 (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
#!/usr/bin/bash

# TEXTVIEW="nvim -R"
TEXTVIEW="less"
PDFVIEW="zathura"

# arch package rfc(any)
RFC_DIR=/usr/share/doc/rfc
RFC_INDEX=$RFC_DIR/rfc-index.txt

print_help(){
    echo "usage:"
    echo "    rfc [option] <rfc-number>"
    echo "options:"
    echo "    --pdf open rfc in pdf viewer"
    echo "    -q    quick query, show only the summary"
}

print_summary(){
    # if sed takes no filename, it will read stdin
    # but we should let it fail...
    if test -z "$RFC_INDEX" 
    then
        echo "BAD RFC_INDEX"
        exit 1
    fi
    sed -n "/^$1/,/^$/p" $RFC_INDEX
}

open_pdf(){
    PDF=$RFC_DIR/pdf/rfc$1.pdf
    if [ ! -f "$PDF" ]; then
        echo "$PDF not available."
        exit 1;
    fi
    $PDFVIEW $PDF
}


open_txt(){
    TXT=$RFC_DIR/txt/rfc$1.txt
    if [ ! -f "$TXT" ]; then
        echo "$TXT not available."
        exit 1;
    fi
    $TEXTVIEW $TXT
}


re='^[0-9]+$'
# parse argumentsa
while [[ $# -gt 0 ]]; do
  case $1 in
    -q)
      if ! [[ $2 =~ $re ]] ; then
        echo "invalid input" >&2; print_help; exit 1
      fi
      print_summary $2
      exit
      ;;
    --pdf)
      if ! [[ $2 =~ $re ]] ; then
        echo "invalid input" >&2; print_help; exit 1
      fi
      open_pdf $2
      exit
      ;;
    -*|--*)
      print_help
      exit 1
      ;;
    *)
      if ! [[ $1 =~ $re ]] ; then
        echo "invalid input $2" >&2; print_help; exit 1
      fi
      open_txt $1
      exit
      ;;
  esac
done

if [ ! $# -eq 1 ]; then
    print_help
    exit 1
fi