-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unzippeds
executable file
·221 lines (193 loc) · 6.19 KB
/
unzippeds
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/bin/bash
# UnzipPeds - Unzips a PEDS collection of Xml files in the arrangement
# determined by the provided argument (a, f, or o).
#
# Copyright © 2020 David Yockey
#
# XSLT_PEDS is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# XSLT_PEDS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with XSLT_PEDS. If not, see <https://www.gnu.org/licenses/>.
function xsltproc () {
# Generate Html file from Xml file
if [ -e "/usr/bin/Xalan" ]
then
Xalan -o "$1" "$2" "$3"
elif [ -e "/mnt/c/windows/system32/msxsl.exe" -o -e "/cygdrive/c/windows/system32/msxsl.exe" ] # Checks from WSL and Cygwin
then
msxsl.exe -o "$1" "$2" "$3"
else
# If a preliminary XSLT processor check is done, we should never get here...
echo -e "No XSLT processor found.\nPlease install Xalan or MSXSL.\n(For MSXSL: msxsl.exe must be placed in C:\Windows\System32)"
fi
}
function xhTransform () {
xsltproc "$htmlname" "$file" "peds.xsl"
}
function usage () {
echo
echo "Usage: unzippeds OPTION [ZIPFILE]"
echo
echo -e " -a Unzips a PEDS collection of Xml files to the current folder\n while insuring that each file has unique name."
echo
echo -e " -f Unzips a PEDS collection of Xml files to a folder\n while insuring that the folder is uniquely named."
echo
echo -e " -o Unzips a PEDS collection of Xml files and combines them\n into one uniquely-named Xml file."
echo
echo " -?, -h Show this usage information."
echo
}
#
while getopts :afo?h opt
do
case $opt in
a) proc="unzipa" ;;
f) proc="unzipf" ;;
o) proc="unzipo"
firstfile=1 ;;
?|h) usage
exit 0 ;;
esac
done
if [ -z $proc ]
then
echo -e "\n$0: An option is required."
usage
exit 0;
fi
# Get the last argument passed to the script.
# From https://www.cyberciti.biz/faq/linux-unix-bsd-apple-osx-bash-get-last-argument/ (retreived 20200930)
for lastarg in $@; do :; done
# Check for zip file
## `file` kept failing when combining conditions in single tests :(
fzip="F" # Flag indicating if a valid zip file is available to process
fziparg="F" # Flag indicating if the zip file was provided as an argument
if [ -f "$lastarg" ]
then
if [ "$(file -b --mime-type "$lastarg")" = "application/zip" ]
then
fzip="T"
fziparg="T"
zipfile=$lastarg
fi
elif [[ "$lastarg" = -* ]]
then
# Find latest zip file in the current directory
# - Based on info from http://mywiki.wooledge.org/BashFAQ/003 (retrieved 202008??)
for file in ./*.zip
do
[[ $file -nt $latest ]] && latest=$file
done
if [ -f "$latest" ]
then
if [ "$(file -b --mime-type "$latest")" = "application/zip" ]
then
fzip="T"
fziparg="F"
zipfile=$latest
fi
fi
fi
if [ "$fzip" = "F" ]
then
errmsg="No zip file found.\nPlease make sure a zip file is located in your XSLT_PEDS folder\nor provide a zip file name on the command line."
if [ -e "/usr/bin/zenity" ]
then
zenity --error --title="$proc" --window-icon='error' --text="$errmsg" --no-wrap
else
echo -e "$errmsg"
fi
exit 1
fi
# Check for XSLT processor including from within Linux, WSL, and Cygwin
if [ ! -e "/usr/bin/Xalan" -a ! -e "/mnt/c/windows/system32/msxsl.exe" -a ! -e "/cygdrive/c/windows/system32/msxsl.exe" ]
then
echo -e "No XSLT processor found.\nPlease install Xalan or MSXSL.\n(For MSXSL: msxsl.exe must be placed in C:\Windows\System32)"
exit 2
fi
# Make a uniquely-named directory and move the zip file into it
datestamp=$(date +%Y%m%d-%H%M%S)
newdir="pairbulk-$datestamp"
mkdir $newdir
# If necessary, create an empty file with a unique datestamped name for the final combined Xml file
if [ $proc == "unzipo" ]
then
newfile="$newdir.xml"
touch $newfile
fi
# Decompress zip file into $newdir
unzip -qq -d $newdir $zipfile
# Begin transformation processing of Xml files to Html files based on XSLT file peds.xsl
for file in $newdir/*.xml
do
[ -e "$file" ] || continue
# Generate unique name for new html file - Based on answer https://stackoverflow.com/a/1224786/8100489 (as edited Sep 14 '17 at 11:59)
case $proc in
unzipa)
htmlname="$(basename -s .xml "$file")-$datestamp.peds.htm"
xhTransform ;;
unzipf)
htmlname="$newdir/$(basename -s .xml "$file").peds.htm"
xhTransform
rm -f "$file" ;;
unzipo)
if [ "$firstfile" -eq 1 ] # Originally set up in the `getopts` while loop
then
# For the first file, do nothing but clear a flag...
firstfile=0
else
# For the second and each subsequent file...
# (-i option requires GNU sed)
## Split line 2 to separate the first uspat:PatentData tag from the opening uspat:PatentBulkData tag
sed -i "2s/></>\n</" "$file"
## Delete the XML header line and the opening uspat:PatentBulkData tag
sed -i '1,2d' "$file"
fi
# Delete the closing uspat:PatentBulkData tag from each file
# (-i option requires GNU sed)
sed -i '$d' "$file"
# Append the current file to the new combined file
cat "$file" >> $newfile ;;
esac
done
# Finish transformation processing
case $proc in
unzipa)
rm -rf $newdir ;;
unzipf)
;;
unzipo)
# Finish the new combined file by appending a closing uspat:PatentBulkData tag
echo "</uspat:PatentBulkData>" >> $newfile
# Generate unique name for new html file - Based on answer https://stackoverflow.com/a/1224786/8100489 (as edited Sep 14 '17 at 11:59)
htmlname="$(basename -s .xml "$newfile").peds.htm"
file="$newfile" # Set $file so it's available within xhTransform
xhTransform
rm -rf $newdir
rm -f $newfile ;;
esac
# Cleanup
if [ "$fziparg" = "F" ]
then
if [ ! -d ./zipTrash ]
then
mkdir zipTrash
fi
mv $zipfile ./zipTrash
fi
# Done
msg="$proc done."
if [ -e "/usr/bin/zenity" ]
then
zenity --notification --window-icon='info' --text="$msg"
else
echo "$msg"
fi