change the date
[tools/build.git] / vc
1 #!/bin/bash
2 # use this script to edit *.changes files
3 #
4 # based on changelog edit script from xqf
5 #
6 # Copyright (C) 2002 Ludwig Nussel
7 # Copyright (C) 2009 SUSE Linux Products GmbH, Nuernberg, Germany.
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 or 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
23 shopt -s nullglob
24
25 if [ -z "$mailaddr" ]; then
26         domain=`dnsdomainname`
27         [ -z "$domain" ] && domain=localhost
28         mailaddr="$USER@$domain"
29 fi
30
31 EDITOR=${EDITOR:-vim}
32 date=`LC_ALL=POSIX TZ=UTC date`
33
34 if ! which mktemp > /dev/null 2>&1; then
35         echo "mktemp is required for this script to work"
36         exit 1
37 fi
38
39 while [ -n "$1" ]; do
40         case "$1" in
41                 -m)
42                         if [ $just_edit ]; then
43                                 echo "You cannot use -m and -e together!"
44                                 exit 1
45                         fi
46                         message="$2"
47                         shift 2
48                         ;;
49                 -e)
50                         if [ -n "${message}" ]; then
51                                 echo "You cannot use -m and -e together!"
52                                 exit 1
53                         fi
54                         just_edit=true
55                         shift 1
56                         ;;
57                 --help)
58                         echo "Usage: $0 [-m MESSAGE|-e] [filename[.changes]|path [file_with_comment]]"
59                         echo
60                         echo "Will use '$mailaddr' for changelog entries"
61                         echo
62                         echo "Options:"
63                         echo "    -m MESSAGE    add MESSAGE to changes (not open an editor)"
64                         echo "    -e            just open changes (cannot be used with -m)"
65                         exit 0
66                         ;;
67                 *) break ;;
68         esac
69 done
70
71 changelog="$1"
72 content="$2"
73 pkgpath=
74 if [ -n "$changelog" -a -d "$changelog" ]; then
75         pkgpath="$changelog/"
76         changelog=''
77 fi
78
79 if [ -n "$changelog" ]; then
80         if [ "${changelog%.changes}" = "$changelog" ]; then
81                 changelog="$changelog.changes"
82         fi
83 else
84         changelog=($pkgpath*.changes)
85         if [ "${#changelog[@]}" -eq 1 ]; then
86                 changelog="$changelog"
87         elif [ -n "$changelog" ]; then
88                 echo "Choose one of ${changelog[@]}"
89                 exit 1
90         fi
91 fi
92
93 if [ -z "$changelog" ]; then
94         changelog=($pkgpath*.spec)
95         if [ "${#changelog[@]}" -eq 1 ]; then
96                 changelog=${changelog%.spec}.changes
97         elif [ -n "$changelog" ]; then
98                 echo "Choose one of ${changelog[@]}"
99                 exit 1
100         fi
101 fi
102
103 if [ -z "$changelog" ]; then
104         echo "no .changes and no .spec file found"
105         exit 1
106 fi
107
108 if [ ! -e "$changelog" ]; then
109         created_new_changelog=true
110         touch $changelog
111 fi
112
113 tmpfile=`mktemp -q $changelog.vctmp.XXXXXX`
114 if [ $? -ne 0 ]; then
115         echo "$0: Can't create temp file, exiting..."
116         exit 1
117 fi
118 trap "rm -f \"$tmpfile\"" EXIT
119
120 set +e
121
122 {
123         if [ ! $just_edit ]; then
124                 echo "-------------------------------------------------------------------"
125                 echo "$date - $mailaddr"
126                 echo
127         fi
128         if [ -n "$message" ]; then
129                 echo -e "- $message"
130                 echo
131         elif [ -n "$content" ]; then
132                 cat "$content"
133                 echo
134         elif [ ! $just_edit ]; then
135                 echo "- "
136                 echo
137         fi
138         cat $changelog
139 } >> "$tmpfile"
140
141 if [ -z "$message" ]; then
142         set -- `md5sum "$tmpfile"`
143         chksum="$1"
144         $EDITOR +4 "$tmpfile"
145         set -- `md5sum "$tmpfile"`
146         if [ -z "$content" -a "$chksum" == "$1" ]; then
147                 echo "no changes made"
148                 if [ "$created_new_changelog" = true ]; then
149                         rm -f "$changelog"
150                 fi
151                 exit 0
152         fi
153 fi
154 mode=`stat -c "%a" "$changelog"`
155 user=`stat -c "%u:%g" "$changelog"`
156 mv "$tmpfile" "$changelog"
157 chmod $mode "$changelog"
158 chown $user "$changelog"