]> nuage.metani.fr Git - 3GPP.git/blob - bin/docx2asn.sh
373abb9c73ef72e8eba644c53348da7cb4a2f88c
[3GPP.git] / bin / docx2asn.sh
1 #!/bin/bash
2
3 if [[ $# -eq 0 ]] ; then
4 echo "Usage: ${0##*/} file [files]"
5 echo "Extract ASN.1 from one or more 3GPP 38.331 docx files and save then to .asn files"
6 exit 1
7 fi
8
9 if [[ ! -w . ]] ; then
10 echo "Cannot write to current directory"
11 exit 1
12 fi
13
14 platform=$(uname)
15
16 case $platform in
17 Linux) libreoffice="libreoffice" ;;
18 CYGWIN*) libreoffice="soffice" ;;
19 *) echo "Unkown platform $platform"
20 exit 1;;
21 esac
22
23 if [[ ! -x $(which "$libreoffice") ]] ; then
24 echo "Cannot find libreoffice executable"
25 echo "Make sure it is installed and the PATH variable includes its path"
26 exit 1
27 fi
28
29 while [ "$#" -gt 0 ]; do
30
31 echo "Processing $1"
32
33 if [[ ! -f "$1" ]] || [[ ! -r "$1" ]] ; then
34 echo "$1 is not a readable file"
35 shift
36 continue
37 fi
38
39 if [[ -e "${1%.docx}.txt" ]] ; then
40 echo "${1%.docx}.txt already exists: skipping conversion"
41
42 else
43 $libreoffice --convert-to txt "$1"
44
45 if [[ ! -f "${1%.docx}.txt" ]] || [[ ! -r "${1%.docx}.txt" ]] ; then
46 echo "libreoffice did not create ${1%.docx}.txt"
47 shift
48 continue
49 fi
50
51 # On windows, soffice adds CR characters at line ends and produces EN-DASH in Windows-1252 enconding, i.e. 0x96
52 # Remove the CR at each line end and convert EN-DASH to normal minus
53 echo "${1%.docx}.txt"
54 LC_ALL=C sed -i $'s/\x96/-/g' "${1%.docx}.txt"
55 LC_ALL=C sed -i $'s/\r$//' "${1%.docx}.txt"
56
57 fi
58
59 if [[ -e "${1%.docx}.asn" ]] ; then
60 echo "${1%.docx}.asn already exists: not regenerated"
61 shift
62 continue
63 fi
64
65
66 cat "${1%.docx}.txt" | awk '/^-- ASN1START$/,/^-- ASN1STOP$/' | grep -Evxe '-- ASN1ST(ART|OP)' | grep -v '^--' | awk 'NF || p; { p = NF }' p=0 > "${1%.docx}.asn"
67
68 if [[ -e "${1%.docx}.asn" ]] ; then
69 echo "${1%.docx}.asn was generated"
70 fi
71 shift
72 done
73