~dricottone/my-utils

my-utils/archives/epub -rwxr-xr-x 3.7 KiB
a5400e39Dominic Ricottone Starting point for cryptographic utilities 2 years ago
                                                                                
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash

name="epub"
version="1.0"
read -r -d '' help_message <<-EOF
	Dumps HTML from an epub e-book archive
	Usage: epub TARGET [FILENAMES] [OPTIONS]
	Options:
	 -d, --dump            print to STDOUT
	 -h, --help            print this message
	 -l, --list            list HTML entries of archive
	 -L, --list-all        list all entries of archive
	 -q, --quiet           suppress error messages and prompts
	 -v, --verbose         show additional messages
	 -V, --version         print version number and exit
	 -w WIDTH,             set width of pager
	     --width WIDTH
EOF

source /usr/local/lib/mylib.bash

target_archive=""
positional=()
quiet=0
verbose=0
dump=0
list=0
list_all=0
width="$(/usr/bin/tput cols)"
while [[ $# -gt 0 ]]; do
  case "$1" in

  -d|--dump)
    debug_msg "Setting dump option to 1 (was ${dump})"
    dump=1
    shift
    ;;

  -h|--help)
    help_msg
    shift
    ;;

  -l|--list)
    debug_msg "Setting list option to 1 (was ${list})"
    list=1
    shift
    ;;

  -L|--list-all)
    debug_msg "Setting list all option to 1 (was ${list_all})"
    list_all=1
    shift
    ;;

  -q|--quiet)
    debug_msg "Setting quiet option to 1 (was ${quiet})"
    quiet=1
    shift
    ;;

  -v|--verbose)
    debug_msg "Setting verbose option to 1 (was ${verbose})"
    verbose=1
    shift
    ;;

  -V|--version)
    version_msg
    ;;

  -w|--width)
    if ! is_positive_integer "$2"; then
      error_msg "Cannot set column width to '${2}' (not an integer >= 1)"
    fi
    debug_msg "Setting column width to ${2} (was ${width})"
    width="$2"
    shift; shift
    ;;

  *)
    if [ -z "$target_archive" ]; then
      debug_msg "Setting target filename to '${1}'"
      target_archive="$1"
    else
      debug_msg "Argument '${1}' added to positional array"
      positional+=("$1")
    fi
    shift
    ;;
  esac
done

# error if no filenames given
if [[ -z "$target_archive" ]]; then
  debug_msg "No input filename was given"
  usage_msg
elif [[ ! -f "$target_archive" ]]; then
  error_msg "No such file '${target_archive}'"
fi

# listing subroutine
list_target_archive() {
  if ! /usr/bin/zipinfo -1 "$target_archive" 2>/dev/null; then
    debug_msg "Error in listing subroutine"
    code=1
  fi
}

# extract subroutine
extract_from_target_archive() {
  if ! /usr/bin/unzip -caaqq "$target_archive" "$target_fn"; then
    code=1
  fi
}

# render subroutine
render_html() {
  if ! /usr/bin/w3m -T text/html -cols "$width" -dump; then
    code=1
  fi
}

# dump subroutine
dump_target_archive() {
  if [[ "${#positional[@]}" -eq 0 ]]; then
    debug_msg "Opening all HTML files within '${target_archive}'..."
    for fn in "${list_fn[@]}"; do
      target_fn="$fn"
      debug_msg "Opening '${target_fn}'..."
      extract_from_target_archive | render_html
    done
  else
    for fn in "${positional[@]}"; do
      target_fn="$fn"
      debug_msg "Opening '${target_fn}'..."
      extract_from_target_archive | render_html
    done
  fi
}

# main routine
code=0
if [[ "$list_all" -eq 1 ]]; then
  list_target_archive | sort
elif [[ "$list" -eq 1 ]]; then
  list_target_archive | grep -E '\.xml|\.html|\.xhtml' | sort
else
  IFS=$'\n' list_fn=( $(list_target_archive | grep -E '\.xml|\.html|\.xhtml' | sort) )

  if [[ "$code" -eq 1 || "${#list_fn[@]}" -eq 0 ]]; then
    error_msg "'${target_archive}' is not a valid archive"
  else
    for target_fn in "${positional[@]}"; do
      debug_msg "Opening '${target_fn}'..."
      if ! contains "$target_fn" "${list_fn[@]}"; then
        error_msg "'${target_fn}' not in archive '${target_archive}'"
      fi
    done
  fi

  if [[ "$dump" -eq 1 ]]; then
    dump_target_archive
  else
    dump_target_archive | ${PAGER:=/usr/bin/less}
  fi
fi

# return stored code
exit "$code"