Initial import.
[dcp.git] / dcp-init
1 #!/bin/bash
2
3 set -e
4
5 usage() {
6     echo "usage: dcp-init [-C key=val] [-d REPODIR] PACKAGE [PATCH...]"
7     echo "       PATCH ::= [-p LEVEL] [-bB BRANCH] FILE"
8     echo "       -b creates a new branch at the current patch"
9     echo "       -B creates a new branch at the upstream sources"
10 }
11
12 rungit() {
13     (
14         cd "$td"
15         git "$@"
16     ) || false
17 }
18
19 getaptsrc() {
20     mkdir "$td/apt-tmp"
21     (cd "$td/apt-tmp"; apt-get source "$1") || false
22     
23     echo Achtung
24     sleep 10
25
26     sdir=
27     for f in "$td/apt-tmp"/*; do
28         if [ -d "$f" ]; then
29             if [ -z "$sdir" ]; then
30                 sdir="$f"
31             else
32                 echo "dcp-init: got more than one directory from apt-get; cannot continue" >&2
33                 exit 1
34             fi
35         fi
36     done
37     if [ -z "$sdir" ]; then
38         echo "dcp-init: could not find any source directory" >&2
39         exit 1
40     fi
41     mv "$sdir" "$td/src"
42     rm -rf "$td/apt-tmp"
43
44     rungit add src
45 }
46
47 initrepo() {
48     rungit init -q
49     mkdir "$td/control"
50     touch "$td/control/conf"
51     rungit add control
52     rungit commit -q -m "Initial repo"
53     rungit tag empty
54 }
55
56 initbase() {
57     mkdir "$td/control/build.d"
58     mkdir "$td/control/update.d"
59     cat >"$td/control/functions" <<EOF
60 readconf() {
61     while read key val; do
62         export "CONF_\$key"="\$val"
63     done <control/conf
64 }
65
66 rungit() {
67     (cd repo; git "\$@") || false
68 }
69 EOF
70     cat >"$td/control/build" <<EOF
71 #!/bin/sh
72
73 set -e
74
75 . control/functions
76 readconf
77
78 for file in control/build.d/*; do
79     if [ -x "\$file" ]; then "\$file"; fi
80 done
81 EOF
82     chmod 755 "$td/control/build"
83     cat >"$td/control/update" <<EOF
84 #!/bin/sh
85
86 set -e
87
88 . control/functions
89 readconf
90
91 for branch in repo/.git/refs/heads/*; do
92     branch="\${branch##*/}"
93     if [ -x "control/update.d/\$branch" ]; then
94         rungit checkout "\$branch"
95         lastrev="\$(rungit rev-parse HEAD)"
96         "control/update.d/\$branch"
97         newrev="\$(rungit rev-parse HEAD)"
98         rungit checkout master
99         if [ "\$newrev" != "\$lastrev" ]; then
100             rungit merge -n "\$branch"
101         fi
102     fi
103 done
104 EOF
105     chmod 755 "$td/control/update"
106     rungit add control
107 }
108
109 initapt() {
110     cat >"$td/control/build.d/99dpkg" <<EOF
111 #!/bin/bash
112
113 set -e
114
115 cmd=(dpkg-buildpackage -b)
116 if [ -n "\$CONF_MAINTAINER" ]; then
117     cmd=("\${cmd[@]}" "-m\$CONF_MAINTAINER")
118 fi
119 if [ -n "\$CONF_GPGKEY" ]; then
120     cmd=("\${cmd[@]}" "-k\$CONF_GPGKEY")
121 fi
122 (cd repo/src; "\${cmd[@]}") || false
123 mv repo/*.deb output/
124 EOF
125     chmod 755 "$td/control/build.d/99dpkg"
126     rungit add control/build.d/99dpkg
127     cat >"$td/control/update.d/upstream" <<EOF
128 #!/bin/sh
129
130 set -e
131
132 cd repo
133 dcp-update-apt "\$CONF_APTPKG"
134
135 EOF
136     chmod 755 "$td/control/update.d/upstream"
137     rungit add control/update.d/upstream
138     echo "APTPKG $pkg" >>"$td/control/conf"
139     rungit add control/conf
140 }
141
142 defdir=/srv/dcp
143 repodir=
144 confopts=()
145
146 while [ "${1:0:1}" = - ]; do
147     opt="${1:1}"
148     shift
149     if [ "$opt" = d ]; then
150         repodir="$1"
151         shift
152     elif [ "$opt" = h ]; then
153         usage
154         exit 0
155     elif [ "$opt" = C ]; then
156         confopts=("${confopts[@]}" "$1")
157         shift
158     else
159         echo "dcp-init: unknown option '$opt'" >&2
160         exit 1
161     fi
162 done
163
164 if [ $# -lt 1 ]; then
165     usage >&2
166     exit 1
167 fi
168 pkg="$1"
169 shift
170 if [ -z "$repodir" ]; then repodir="$pkg"; fi
171 if [[ "$repodir" != */* ]]; then
172     repodir="$defdir/${repodir}.git"
173 fi
174
175 td="$(mktemp -d "/tmp/dcp-XXXXXX")"
176 initrepo
177
178 initbase
179 rungit commit -q -m "Base control files"
180
181 if [ "${#confopts[@]}" -gt 0 ]; then
182     for opt in "${confopts[@]}"; do
183         key="${opt%%=*}"
184         val="${opt#*=}"
185         echo "$key $val" >>"$td/control/conf"
186     done
187     rungit add control/conf
188     rungit commit -q -m "Custom configuration file"
189 fi
190
191 rungit checkout -q -b upstream empty
192 getaptsrc "$pkg"
193 rungit commit -q -m "Initial upstream import"
194 rungit checkout master
195 rungit merge -n upstream
196
197 initapt
198 rungit commit -q -m "APT control files"
199
200 initvals() {
201     level=0
202 }
203 initvals
204 while [ $# -gt 0 ]; do
205     arg="$1"
206     shift
207     if [ "${arg:0:1}" = - ]; then
208         if [ "$arg" = -p ]; then
209             level="$1"
210             shift
211         elif [ "$arg" = -b ]; then
212             rungit checkout -q -b "$1"
213             shift
214         elif [ "$arg" = -B ]; then
215             rungit checkout -q -b "$1" upstream
216         else
217             echo "dcp-init: unknown patch option '$arg'" >&2
218             exit 1
219         fi
220     else
221         (
222             if [[ "$arg" == *.gz ]]; then
223                 zcat "$arg"
224             else
225                 cat "$arg"
226             fi
227         ) | patch -d "$td/src" -p"$level"
228         rungit add src
229         rungit commit -q -m "Applied $(basename "$arg")"
230         initvals
231     fi
232 done
233
234 git clone -q --bare "$td" "$repodir"
235
236 rm -rf "$td"