]> nuage.metani.fr Git - 3GPP.git/blob - bin/38331-convert.sh
38331-convert.sh now does all the processing
[3GPP.git] / bin / 38331-convert.sh
1 #!/bin/bash
2
3 if [[ ! $# -eq 1 ]] ; then
4 echo "Usage: ${0##*/} 38331-xyz.docx"
5 echo "38331-xyz.docx is a version of TS 38.331"
6 echo "Creates one .asn pure ASN.1 text file per ASN.1 module in TS 38.331"
7 echo "Message, IE, field and presence condition descriptions are included as comments"
8 echo "Produces 2 versions:"
9 echo " - one with descriptions of fields and presence conditions at the end of the IE definition"
10 echo " - one with those descriptions before the concerned field(s)"
11 exit 1
12 fi
13
14 if [[ ! -w . ]] ; then
15 echo "Cannot write to current directory"
16 exit 1
17 fi
18
19 if [[ ! -f "$1" ]] || [[ ! -r "$1" ]] ; then
20 echo "$1 is not a readable file"
21 exit
22 fi
23
24 if [[ -e "${1%.docx}.txt" ]] ; then
25 echo "${1%.docx}.txt already exists: skipping conversion"
26 else
27 platform=$(uname)
28 case $platform in
29 Linux) libreoffice="libreoffice" ;;
30 CYGWIN*) libreoffice="soffice" ;;
31 *) echo "Unkown platform $platform"
32 exit 1;;
33 esac
34
35 if [[ ! -x $(which "$libreoffice") ]] ; then
36 echo "Cannot find libreoffice executable"
37 echo "Make sure it is installed and the PATH variable includes its path"
38 exit 1
39 fi
40 $libreoffice --convert-to txt "$1"
41
42 if [[ ! -f "${1%.docx}.txt" ]] || [[ ! -r "${1%.docx}.txt" ]] ; then
43 echo "libreoffice did not create ${1%.docx}.txt"
44 exit
45 fi
46
47 case $platform in
48 # Replace EN-DASH (UTF-8 encoding) at line start with -
49 Linux) LC_ALL=C sed -i $'s/^\u2013/-/g' "${1%.docx}.txt" ;;
50
51 # Replace EN-DASH (Windows-1252 encoding) at line start with -
52 # Remove CR (0x0d) put by libreoffice in windows before each LF (0x0a)
53 CYGWIN*) LC_ALL=C sed -i $'s/^\x96/-/g; s/\r$//' "${1%.docx}.txt" ;;
54 *) echo "Unkown platform $platform"
55 exit 1;;
56 esac
57
58 # Changes things that should not be so in the specification
59 # Change "-- Cond N3C MP" to "-- Cond N3C-MP" (to respect condition naming rules)
60 LC_ALL=C sed -i 's/-- Cond N3C MP/-- Cond N3C-MP/' "${1%.docx}.txt"
61 LC_ALL=C sed -i 's/^N3C MP/N3C-MP/' "${1%.docx}.txt"
62 LC_ALL=C sed -i 's/-- Cond HOAndServCellAdd,$/-- Cond HOAndServCellAdd/' "${1%.docx}.txt"
63
64 iconv -f UTF-8 -t ASCII//TRANSLIT "${1%.docx}.txt" > "${1%.docx}.ascii"
65 mv "${1%.docx}.ascii" "${1%.docx}.txt"
66 fi
67
68 cat "${1%.docx}.txt" | awk '/^-- ASN1START$/,/^-- ASN1STOP$/' | awk '!/^[[:space:]]*--/ && !/^[[:space:]]*$/'| sed -e 's/--.*$//g' > "${1%.docx}.asncheck"
69
70 if [[ ! -e "${1%.docx}.asncheck" ]] ; then
71 echo "${1%.docx}.asn generation failed"
72 exit
73 fi
74
75 mkdir -p by-IE
76 cd by-IE
77 txt2asn.awk "../${1%.docx}.txt" > "${1%.docx}.asn"
78 cat "${1%.docx}.asn" | awk '!/^[[:space:]]*--/ && !/^[[:space:]]*$/'| sed -e 's/--.*$//g' > "${1%.docx}.asncheck"
79 compare1=$(diff -b -B "../${1%.docx}.asncheck" "${1%.docx}.asncheck")
80 if [[ "$compare1" ]] ; then
81 echo "The ASN.1 in by-IE/${1%.docx}.asn does not fully match with the ASN.1 in ${1%.docx}.asncheck"
82 echo "Check with:"
83 echo " diff -b -B by-IE/${1%.docx}.asncheck ${1%.docx}.asncheck"
84 else
85 rm "${1%.docx}.asncheck"
86 asn1-split.awk "${1%.docx}.asn"
87 fi
88
89 cd ..
90 mkdir -p before-field
91 cd before-field
92 txt2asn-integrated.awk "../${1%.docx}.txt" > "${1%.docx}.asn"
93 cat "${1%.docx}.asn" | awk '!/^[[:space:]]*--/ && !/^[[:space:]]*$/'| sed -e 's/--.*$//g' > "${1%.docx}.asncheck"
94 compare2=$(diff -b -B "../${1%.docx}.asncheck" "${1%.docx}.asncheck")
95 if [[ "$compare2" ]] ; then
96 echo "The ASN.1 in before-field/${1%.docx}.asn does not fully match with the ASN.1 in ${1%.docx}.asncheck"
97 echo "Check with:"
98 echo " diff -b -B before-field/${1%.docx}.asncheck ${1%.docx}.asncheck"
99 else
100 rm "${1%.docx}.asncheck"
101 asn1-split.awk "${1%.docx}.asn"
102 fi
103
104 cd ..
105 if [[ "$compare1" ]] || [[ "$compare1" ]] ; then
106 exit
107 else
108 rm "${1%.docx}.asncheck"
109 fi