bib2html.py: Add incollection bibtype
[shutils.git] / office-gpg
1 #!/bin/sh
2 # Copyright (c) 2013 Stefan Huber
3 #
4 # Permission is hereby granted, free of charge, to any person
5 # obtaining a copy of this software and associated documentation
6 # files (the "Software"), to deal in the Software without
7 # restriction, including without limitation the rights to use,
8 # copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following
11 # conditions:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 # OTHER DEALINGS IN THE SOFTWARE.
24
25 # Author: Stefan Huber <shuber@sthu.org>
26
27 #
28 # This script does a decrypt-edit-encrypt cycle with gpg and OpenOffice resp.
29 # LibreOffice files.
30
31
32 SUMCMD="sha1sum"
33 CRYPTID=""
34
35 CMD="/usr/bin/libreoffice"
36 [ -x ${CMD} ] || CMD="/usr/bin/ooffice"
37
38 [ -f "$HOME/.office-gpg.cfg" ] && source "$HOME/.office-gpg.cfg"
39
40 if [ -z "$CRYPTID" ]; then
41 echo "No CRYPTID set in $HOME/.office-gpg.cfg"
42 exit 1
43 fi
44
45 #No valid invocation
46 if [ $# -ne 1 ]
47 then
48 echo "Usage: $CMD-gpg <gpg-file>"
49 exit -1
50 fi
51
52
53 #The corresponding gpg-file
54 gpgfile=$1
55
56 if [ ! -f "$gpgfile" ]
57 then
58 echo "\`$gpgfile\` does not exist..."
59 else
60 echo "Opening \`$gpgfile\`..."
61 fi
62
63
64
65 #Search for filename to to encrypt file to...
66 uncryptfilePost=${gpgfile%%.gpg}
67 i=1
68 uncryptfile="$i.$uncryptfilePost"
69
70 while [ -f "$uncryptfile" ]
71 do
72 let "i = $i + 1"
73 uncryptfile="$i.$uncryptfilePost"
74 done
75
76
77 echo "Uncrypt \`$gpgfile\` to \`$uncryptfile\`..."
78
79
80
81 #Decrypt file and get checksum...
82 gpg --output "$uncryptfile" --decrypt "$gpgfile"
83 hsumpre=`$SUMCMD "$uncryptfile" | awk '{ print $1 }'`
84 echo "Checksum is '$hsumpre'."
85
86
87 #Edit file and get checksum of altered file
88 $CMD "$uncryptfile"
89 #pid=`lsof | grep "$uncryptfile" | cut -d ' ' -f 2`
90 #echo "wait for pid: $pid"
91 #while [ -n "`ps | grep "$pid"`" ] ; do
92 # sleep 1
93 #done
94
95
96 hsumpost=`$SUMCMD "$uncryptfile" | awk '{ print $1 }'`
97 echo "Checksum is '$hsumpost'."
98
99
100 #Check if file has been altered
101 if [ "$hsumpost" != "$hsumpre" ]
102 then
103 gpg --output "$gpgfile" -r "$CRYPTID" --encrypt "$uncryptfile"
104
105 #Get checksum of crypted version
106 hsumcrypt=`gpg --decrypt $gpgfile | $SUMCMD | awk '{ print $1 }'`
107 echo "Checksum of crypted file is '$hsumcrypt'"
108
109 if [ "$hsumpost" != "$hsumcrypt" ]
110 then
111 echo ""
112 echo "*** WARNING ***"
113 echo "*** WARNING *** Encrypted file \`$gpgfile\` leads to different checksum."
114 echo "*** WARNING *** Not removing unencrypted file \`$uncryptfile\`."
115 echo "*** WARNING ***"
116 echo ""
117
118 exit -1
119 fi
120
121 else
122 echo "No changes. Skip re-encrypting"
123 fi
124
125
126
127
128 #Remove old file...
129 rm -i "$uncryptfile"
130
131
132 #File not removed!?
133 if [ -f "$uncryptfile" ]
134 then
135 echo ""
136 echo "*** WARNING ***"
137 echo "*** WARNING *** Unencrypted file \`$uncryptfile\` not removed!"
138 echo "*** WARNING ***"
139 echo ""
140 fi
141