Tear out anything that says "Debathena" and replace it with "Invirt"
[invirt/third/common.git] / invirtificator.sh
1 # Interface: define variables name, ivversionappend, and function
2 # hack_package ().
3
4 set -e
5
6 # Process arguments.
7 dist_arch=$1; shift
8 a=
9 if [ "$1" = "-A" ]; then a=-A; shift; fi
10 chroot=$dist_arch-sbuild
11
12 if [ -z "$dist_arch" -o $# -eq 0 ]; then
13     echo 'No arguments!' >&2
14     exit 2
15 fi
16
17 dist=$(echo "$dist_arch" | sed 's/^\(.*\)-\([^-]*\)$/\1/')
18 arch=$(echo "$dist_arch" | sed 's/^\(.*\)-\([^-]*\)$/\2/')
19 : ${section=invirt-system}
20 : ${ivname=$name}
21 : ${release=-proposed}
22
23 # Create a chroot and define functions for using it.
24 sid=$(schroot -b -c "$chroot")
25 trap 'schroot -e -c "$sid"' EXIT
26 sch() { schroot -r -c "$sid" -- "$@"; }           # Run in the chroot
27 schq() { schroot -q -r -c "$sid" -- "$@"; }       # Run in the chroot quietly
28 schr() { schroot -r -c "$sid" -u root -- "$@"; }  # Run in the chroot as root
29 schr apt-get -qq -y update || exit 3
30
31 quote() {
32   echo "$1" | sed 's/[^[:alnum:]]/\\&/g'
33 }
34
35 munge_sections () {
36     perl -0pe "s/^Section: /Section: $section\\//gm or die" -i debian/control
37 }
38
39 add_changelog () {
40     if [ -n "$dch_done" ]; then
41         dch "$@"
42     else
43         echo | dch -v"${ivversion}" -D unstable "$@"
44         dch_done=1
45     fi
46 }
47
48 append_description () {
49     perl -0pe 'open THREE, "</dev/fd/3"; $x = <THREE>; s/(^Description:.*\n( .*\S.*\n)*)/$1$x/gm or die' -i debian/control 3<&0
50 }
51
52 add_build_depends () {
53     perl -0pe 's/^(Build-Depends:.*(?:\n[ \t].*)*)$/$1, '"$1"'/m or die' -i debian/control
54 }
55
56 add_invirt_provides () {
57     [ "$name" = "$ivname" ]
58     perl -0pe 's/^(Package: (.*)\n(?:(?!Provides:).+\n)*)(?:Provides: (.*)\n((?:.+\n)*))?(?=\n|\z)/$1Provides: $3, invirt-$2\n$4/mg or die; s/^Provides: , /Provides: /mg' -i debian/control
59     add_changelog "Provide invirt-$name."
60 }
61
62 rename_source () {
63     perl -pe "s{^Source: $name\$}{Source: $ivname}" -i debian/control
64     add_changelog "Rename package to $ivname."
65     perl -0pe "s/^$name/$ivname/" -i debian/changelog
66 }
67
68 cmd_source () {
69     if [ "$a" != "-A" ]; then
70         echo "Not building source package for $dist_arch." >&2
71         return
72     fi
73     echo "Building source for $ivname-$ivversion on $dist_arch" >&2
74     
75     if ! [ -e "${name}_$version.dsc" ]; then
76         sch apt-get -d source "$name"
77     fi
78     
79     if ! [ -e "${ivname}_$ivversion.dsc" ]; then
80         (
81             tmpdir=$(mktemp -td "invirtify.$$.XXXXXXXXXX")
82             trap 'rm -rf "$tmpdir"' EXIT
83             origversion=$(echo "$version" | sed 's/-[^-]*$//')
84             cp -a "${name}_$origversion.orig.tar.gz" "$tmpdir/"
85             dscdir=$(pwd)
86             cd "$tmpdir/"
87             dpkg-source -x "$dscdir/${name}_$version.dsc" "$tmpdir/$name-$origversion"
88             cd "$tmpdir/$name-$origversion"
89             dch_done=
90             hack_package
91             if [ "$name" != "$ivname" ]; then
92                 rename_source
93                 cp -a "$tmpdir/${name}_$origversion.orig.tar.gz" "$tmpdir/${ivname}_$origversion.orig.tar.gz"
94                 cp -a "$tmpdir/${ivname}_$origversion.orig.tar.gz" "$dscdir"
95             fi
96             [ -n "$dch_done" ]
97             schr apt-get -q -y install devscripts pbuilder
98             schr /usr/lib/pbuilder/pbuilder-satisfydepends
99             sch debuild -S -sa -us -uc -i -I.svn && cp -a "../${ivname}_$ivversion"* "$dscdir"
100         )
101         [ $? -eq 0 ] || exit 1
102         
103         if [ -n "$IV_CHECK_DIFFS" ]; then
104             interdiff -z "${name}_$version.diff.gz" "${ivname}_$ivversion.diff.gz" | \
105                 enscript --color --language=ansi --highlight=diffu --output=- -q | \
106                 less -R
107             echo -n "Press Enter to continue: " >&2
108             read dummy
109         fi
110     fi
111 }
112
113 cmd_binary () {
114     sbuildhack "$dist_arch" $a "${ivname}_$ivversion.dsc"
115 }
116
117 v () {
118     echo "$@"
119     "$@"
120 }
121
122 cmd_upload () {
123     if [ "$a" = "-A" ]; then
124         v reprepro-env include "${dist}${release}" "${ivname}_${ivversion}_source.changes"
125     fi
126     v reprepro-env include "${dist}${release}" "${ivname}_${ivversion}_${arch}.changes"
127 }
128
129 version=$(
130     sch apt-cache showsrc "$name" | \
131         sed -n 's/^Version: \(.*\)$/\1/ p' | (
132         version='~~~'
133         while read -r newversion; do
134             if [ $(expr "$newversion" : '.*invirt') = 0 ] && \
135                 dpkg --compare-versions "$newversion" '>' "$version"; then
136                 version=$newversion
137             fi
138         done
139         if [ "$version" = '~~~' ]; then
140             echo "No version of $name found." >&2
141             exit 1
142         fi
143         echo "$version"
144         )
145     )
146 ivversion=$version$ivversionappend
147
148 # Look for binary packages built from the named package with the right
149 # version, and exit out if we find one (an architecture-specific one
150 # if we weren't run with the -A flag).  We need to look for either a
151 # Source: or a Package: header matching $name since there is no
152 # Source: header for a package whose name matches its source.
153 pkgfile="/srv/repository/dists/$dist/$section/binary-$arch/Packages.gz"
154 if { zcat "$pkgfile" | \
155     dpkg-awk -f - "Package:^$ivname\$" "Version:^$(quote "$ivversion")\$" -- Architecture;
156     zcat $pkgfiles | \
157     dpkg-awk -f - "Source:^$ivname\$" "Version:^$(quote "$ivversion")\$" -- Architecture; } \
158     | if [ "$a" = "-A" ]; then cat; else fgrep -vx 'Architecture: all'; fi \
159     | grep -q .; then
160     echo "$ivname $ivversion already exists for $dist_arch." >&2
161     exit 0
162 fi
163
164 for cmd; do
165     "cmd_$cmd"
166 done