vim: new colorscheme, also for terminal vim
[shutils.git] / libreoffice-gpg
1 #!/bin/sh
2 #shuber, 2008-10-18
3 #
4 # This script does a decrypt-edit-encrypt cycle with gpg and OpenOffice.
5 # The concrete editing program (ooffice) can be set by the variable
6 # $CMD. The detection if the file has been altered is done by a hash sum
7 # algorithm which can be set by $SUMCMD.
8 #
9 # Changelog:
10 # * 2008-10-18 Initial version
11 # * 2008-10-19 Introducing a CRYPTID variable
12 # Checking if crypted file has same checksum
13 # * 2010-10-23 ooffice only spawns OpenOffice and gives control back
14 # to shell. Hence, we have to wait for pid now.
15
16
17 CMD="libreoffice"
18 SUMCMD="sha1sum"
19 CRYPTID="user@example.org"
20
21
22 #No valid invocation
23 if [ $# -ne 1 ]
24 then
25 echo "Usage: $CMD-gpg <gpg-file>"
26 exit -1
27 fi
28
29
30 #The corresponding gpg-file
31 gpgfile=$1
32
33 if [ ! -f "$gpgfile" ]
34 then
35 echo "\`$gpgfile\` does not exist..."
36 else
37 echo "Opening \`$gpgfile\`..."
38 fi
39
40
41
42 #Search for filename to to encrypt file to...
43 uncryptfilePost=${gpgfile%%.gpg}
44 i=1
45 uncryptfile="$i.$uncryptfilePost"
46
47 while [ -f "$uncryptfile" ]
48 do
49 let "i = $i + 1"
50 uncryptfile="$i.$uncryptfilePost"
51 done
52
53
54 echo "Uncrypt \`$gpgfile\` to \`$uncryptfile\`..."
55
56
57
58 #Decrypt file and get checksum...
59 gpg --output "$uncryptfile" --decrypt "$gpgfile"
60 hsumpre=`$SUMCMD "$uncryptfile" | awk '{ print $1 }'`
61 echo "Checksum is '$hsumpre'."
62
63
64 #Edit file and get checksum of altered file
65 $CMD "$uncryptfile"
66 #pid=`lsof | grep "$uncryptfile" | cut -d ' ' -f 2`
67 #echo "wait for pid: $pid"
68 #while [ -n "`ps | grep "$pid"`" ] ; do
69 # sleep 1
70 #done
71
72
73 hsumpost=`$SUMCMD "$uncryptfile" | awk '{ print $1 }'`
74 echo "Checksum is '$hsumpost'."
75
76
77 #Check if file has been altered
78 if [ "$hsumpost" != "$hsumpre" ]
79 then
80 gpg --output "$gpgfile" -r "$CRYPTID" --encrypt "$uncryptfile"
81
82 #Get checksum of crypted version
83 hsumcrypt=`gpg --decrypt $gpgfile | $SUMCMD | awk '{ print $1 }'`
84 echo "Checksum of crypted file is '$hsumcrypt'"
85
86 if [ "$hsumpost" != "$hsumcrypt" ]
87 then
88 echo ""
89 echo "*** WARNING ***"
90 echo "*** WARNING *** Encrypted file \`$gpgfile\` leads to different checksum."
91 echo "*** WARNING *** Not removing unencrypted file \`$uncryptfile\`."
92 echo "*** WARNING ***"
93 echo ""
94
95 exit -1
96 fi
97
98 else
99 echo "No changes. Skip re-encrypting"
100 fi
101
102
103
104
105 #Remove old file...
106 rm -i "$uncryptfile"
107
108
109 #File not removed!?
110 if [ -f "$uncryptfile" ]
111 then
112 echo ""
113 echo "*** WARNING ***"
114 echo "*** WARNING *** Unencrypted file \`$uncryptfile\` not removed!"
115 echo "*** WARNING ***"
116 echo ""
117 fi
118