/
maff
executable file
·129 lines (116 loc) · 3.04 KB
/
maff
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
#!/usr/bin/env bash
# ,---------------,
# | Help Function |
# '---------------'
printhelp () {
cat <<'EOF'
Usage: maff [OPTION]... [DIRECTORY]
Convert an HTML directory containing an index.html file into a .maff file.
Options:
-h display this help and exit
-i rel_addr create index.html file which redirects to rel_addr
-I create index.html file - query redirect target (query user: SPC to complete, CR to select)
-o outname specify output filename
-q suppress archive output; again to suppress all output
-u convert spaces in directory name to underscores
EOF
}
# ,------------------,
# | Options Handling |
# '------------------'
NAMEOPT='d'
QUIET=''
CREATEINDEX=''
while getopts hi:Io:uq opt; do
case "$opt" in
h) printhelp
exit
;;
i) CREATEINDEX='t'
startfile="$OPTARG"
;;
I) SELECTINDEX='t'
;;
o) NAMEOPT='c'
archive_name="$OPTARG"
;;
u) NAMEOPT='u'
;;
q) if [[ -z $QUIET ]]; then
QUIET='q'
else
QUIET='qq'
fi
;;
esac
done
shift $((OPTIND - 1))
dir="${1:-.}"
dir="${dir%/}"
# ,-------------------,
# | Create Index File |
# '-------------------'
if [[ -n $CREATEINDEX || -n $SELECTINDEX ]]; then
# check that index.html doesn't already exist
if [[ -f "$dir/index.html" ]]; then
if [[ $QUIET != "qq" ]]; then
echo "-i/I specified, but top level index.html file already exists in '$dir'" >&2
fi
exit 1
fi
# if -I was passed:
if [[ -n $SELECTINDEX ]]; then
until [[ -f $startfile_abs ]]; do
startfile_abs=$(dialog --stdout --title "index.html target" --fselect "$dir"/ "" 80)
startfile="${startfile_abs#$dir/}"
done
fi
# create index.html
startfileescaped=`recode utf8..html <<<"$startfile"`;
> "$dir/index.html" cat <<EOF
<html>
<head>
<meta http-equiv="refresh" content="0; url=$startfile" />
</head>
</html>
EOF
else
if [[ -f "$dir/index.htm" ]]; then
cp "$dir/index.htm" "$dir/index.html";
fi
fi
# ,-----------------,
# | Output Filename |
# '-----------------'
case $NAMEOPT in
c) ;;
d) archive_name="${dir%/}.maff"
;;
u) archive_name=`echo "${dir%/}" | sed -e 's/ /_/g'`.maff
;;
esac
# ,------------------,
# | Debugging Output |
# '------------------'
if [[ -n $DEBUG ]]; then
>&2 cat <<EOF
dir: $dir
NAMEOPT: $NAMEOPT
QUIET: $QUIET
CREATEINDEX: $CREATEINDEX
startfile: $startfile
archive_name: $archive_name
EOF
fi
# ,---------------------,
# | Create MAFF Archive |
# '---------------------'
if [[ ! -f "$dir/index.html" ]]; then
[[ $QUIET != "qq" ]] && echo "No top level index.html file in '$dir'" >&2
exit 1
fi
case $QUIET in
q) 7z a -tzip "$archive_name" "$dir" >/dev/null ;;
qq) 7z a -tzip "$archive_name" "$dir" &>/dev/null ;;
*) 7z a -tzip "$archive_name" "$dir" ;;
esac