build: add vc command
[opensuse: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 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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=Europe/Berlin 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                         message="$2"
43                         shift 2
44                         ;;
45                 --help)
46                         echo "Usage: $0 [filename[.changes]|path [file_with_comment]]"
47                         echo
48                         echo "Will use '$mailaddr' for changelog entries"
49                         exit 0
50                         ;;
51                 *) break ;;
52         esac
53 done
54
55 changelog="$1"
56 content="$2"
57 pkgpath=
58 if [ -n "$changelog" -a -d "$changelog" ]; then
59         pkgpath="$changelog/"
60         changelog=''
61 fi
62
63 if [ -n "$changelog" ]; then
64         if [ "${changelog%.changes}" = "$changelog" ]; then
65                 changelog="$changelog.changes"
66         fi
67 else
68         changelog=($pkgpath*.changes)
69         if [ "${#changelog[@]}" -eq 1 ]; then
70                 changelog="$changelog"
71         elif [ -n "$changelog" ]; then
72                 echo "Choose one of ${changelog[@]}"
73                 exit 1
74         fi
75 fi
76
77 if [ -z "$changelog" ]; then
78         changelog=($pkgpath*.spec)
79         if [ "${#changelog[@]}" -eq 1 ]; then
80                 changelog=${changelog%.spec}.changes
81         elif [ -n "$changelog" ]; then
82                 echo "Choose one of ${changelog[@]}"
83                 exit 1
84         fi
85 fi
86
87 if [ -z "$changelog" ]; then
88         echo "no .changes and no .spec file found"
89         exit 1
90 fi
91
92 if [ ! -e "$changelog" ]; then
93         touch $changelog
94 fi
95
96 tmpfile=`mktemp -q $changelog.XXXXXX`
97 if [ $? -ne 0 ]; then
98         echo "$0: Can't create temp file, exiting..."
99         exit 1
100 fi
101 trap "rm -f \"$tmpfile\"" EXIT
102
103 set +e
104
105 {
106         echo "-------------------------------------------------------------------"
107         echo "$date - $mailaddr"
108         echo
109         if [ -n "$message" ]; then
110                 echo "- $message"
111         elif [ -n "$content" ]; then
112                 cat "$content"
113         else
114                 echo "- "
115         fi
116         echo
117         cat $changelog
118 } >> "$tmpfile"
119
120 if [ -z "$message" ]; then
121         set -- `md5sum "$tmpfile"`
122         chksum="$1"
123         $EDITOR +4 "$tmpfile"
124         set -- `md5sum "$tmpfile"`
125         if [ -z "$content" -a "$chksum" == "$1" ]; then
126                 echo "no changes made"
127                 exit 0
128         fi
129 fi
130 mv "$tmpfile" "$changelog"