| 1 | #!/bin/bash |
| 2 | |
| 3 | set -e |
| 4 | |
| 5 | usage() { |
| 6 | echo "usage: dcp-init [-sh] [-C key=val] [-d REPODIR] PACKAGE [(PATCH|-m)...]" |
| 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 | echo " -m merges the current branch into the master branch" |
| 11 | } |
| 12 | |
| 13 | rungit() { |
| 14 | ( |
| 15 | cd "$td" |
| 16 | git "$@" |
| 17 | ) || false |
| 18 | } |
| 19 | |
| 20 | getaptsrc() { |
| 21 | mkdir "$td/apt-tmp" |
| 22 | (cd "$td/apt-tmp"; apt-get source "$1") || false |
| 23 | |
| 24 | sdir= |
| 25 | for f in "$td/apt-tmp"/*; do |
| 26 | if [ -d "$f" ]; then |
| 27 | if [ -z "$sdir" ]; then |
| 28 | sdir="$f" |
| 29 | else |
| 30 | echo "dcp-init: got more than one directory from apt-get; cannot continue" >&2 |
| 31 | exit 1 |
| 32 | fi |
| 33 | fi |
| 34 | done |
| 35 | if [ -z "$sdir" ]; then |
| 36 | echo "dcp-init: could not find any source directory" >&2 |
| 37 | exit 1 |
| 38 | fi |
| 39 | mv "$sdir" "$td/src" |
| 40 | rm -rf "$td/apt-tmp" |
| 41 | |
| 42 | rungit add src |
| 43 | } |
| 44 | |
| 45 | initrepo() { |
| 46 | rungit init |
| 47 | mkdir "$td/control" |
| 48 | touch "$td/control/conf" |
| 49 | rungit add control |
| 50 | rungit commit -q -m "Initial repo" |
| 51 | rungit tag empty |
| 52 | } |
| 53 | |
| 54 | initbase() { |
| 55 | mkdir "$td/control/build.d" |
| 56 | mkdir "$td/control/update.d" |
| 57 | cat >"$td/control/functions" <<EOF |
| 58 | readconf() { |
| 59 | if [ -r "\$HOME/.dcp-build-conf" ]; then |
| 60 | while read key val; do |
| 61 | export "CONF_\$key"="\$val" |
| 62 | done <"\$HOME/.dcp-build-conf" |
| 63 | fi |
| 64 | if [ -r "/etc/dcp-conf" ]; then |
| 65 | while read key val; do |
| 66 | export "CONF_\$key"="\$val" |
| 67 | done <"/etc/dcp-conf" |
| 68 | fi |
| 69 | while read key val; do |
| 70 | export "CONF_\$key"="\$val" |
| 71 | done <control/conf |
| 72 | } |
| 73 | |
| 74 | rungit() { |
| 75 | (cd repo; git "\$@") || false |
| 76 | } |
| 77 | |
| 78 | logchange() { |
| 79 | tag="\${CONF_VERTAG:-dcp}" |
| 80 | ver="\$(dpkg-parsechangelog -lrepo/src/debian/changelog | sed -n 's/^Version: \(.*\)$/\1/p')" |
| 81 | [ -n "\$ver" ] |
| 82 | maint="\$CONF_MAINTAINER" |
| 83 | if [ -z "\$maint" ]; then |
| 84 | maint="\$(id -un) <\$(id -un)@\$(hostname -f)>" |
| 85 | fi |
| 86 | cat - repo/src/debian/changelog >repo/src/debian/changelog.new <<ENDCL |
| 87 | \${CONF_APTPKG} (\${ver}+\${tag}1) unstable; urgency=low |
| 88 | |
| 89 | * Remerged changes in DCP |
| 90 | |
| 91 | -- \${maint} \$(date -R) |
| 92 | ENDCL |
| 93 | mv -f repo/src/debian/changelog.new repo/src/debian/changelog |
| 94 | } |
| 95 | EOF |
| 96 | cat >"$td/control/build" <<EOF |
| 97 | #!/bin/sh |
| 98 | |
| 99 | set -e |
| 100 | |
| 101 | . control/functions |
| 102 | readconf |
| 103 | |
| 104 | for file in control/build.d/*; do |
| 105 | if [ -x "\$file" ]; then "\$file"; fi |
| 106 | done |
| 107 | EOF |
| 108 | chmod 755 "$td/control/build" |
| 109 | cat >"$td/control/update" <<EOF |
| 110 | #!/bin/sh |
| 111 | |
| 112 | set -e |
| 113 | |
| 114 | . control/functions |
| 115 | readconf |
| 116 | |
| 117 | updated=n |
| 118 | for branch in repo/.git/refs/heads/*; do |
| 119 | branch="\${branch##*/}" |
| 120 | if [ -x "control/update.d/\$branch" ]; then |
| 121 | rungit checkout "\$branch" |
| 122 | lastrev="\$(rungit rev-parse HEAD)" |
| 123 | "control/update.d/\$branch" |
| 124 | newrev="\$(rungit rev-parse HEAD)" |
| 125 | rungit checkout master |
| 126 | if [ "\$newrev" != "\$lastrev" ]; then |
| 127 | rungit merge -n "\$branch" |
| 128 | updated=y |
| 129 | fi |
| 130 | fi |
| 131 | done |
| 132 | |
| 133 | if [ "\$updated" = y ]; then |
| 134 | for file in control/update.d/post-*; do |
| 135 | if [ -x "\$file" ]; then "\$file"; fi |
| 136 | done |
| 137 | fi |
| 138 | EOF |
| 139 | chmod 755 "$td/control/update" |
| 140 | rungit add control |
| 141 | } |
| 142 | |
| 143 | initapt() { |
| 144 | cat >"$td/control/build.d/99dpkg" <<EOF |
| 145 | #!/bin/bash |
| 146 | |
| 147 | set -e |
| 148 | |
| 149 | cmd=(dpkg-buildpackage -b -rfakeroot) |
| 150 | if [ -n "\$CONF_MAINTAINER" ]; then |
| 151 | cmd=("\${cmd[@]}" "-m\$CONF_MAINTAINER") |
| 152 | fi |
| 153 | if [ -n "\$CONF_GPGKEY" ]; then |
| 154 | cmd=("\${cmd[@]}" "-k\$CONF_GPGKEY") |
| 155 | fi |
| 156 | (cd repo/src; "\${cmd[@]}") || false |
| 157 | mv repo/*.deb output/ |
| 158 | EOF |
| 159 | chmod 755 "$td/control/build.d/99dpkg" |
| 160 | rungit add control/build.d/99dpkg |
| 161 | cat >"$td/control/update.d/upstream" <<EOF |
| 162 | #!/bin/sh |
| 163 | |
| 164 | set -e |
| 165 | |
| 166 | . control/functions |
| 167 | |
| 168 | cd repo |
| 169 | dcp-update-apt "\$CONF_APTPKG" |
| 170 | EOF |
| 171 | chmod 755 "$td/control/update.d/upstream" |
| 172 | rungit add control/update.d/upstream |
| 173 | cat >"$td/control/update.d/post-logchange" <<EOF |
| 174 | #!/bin/sh |
| 175 | |
| 176 | set -e |
| 177 | . control/functions |
| 178 | |
| 179 | logchange |
| 180 | rungit add src/debian/changelog |
| 181 | rungit commit -q -m "Added changelog entry" |
| 182 | |
| 183 | EOF |
| 184 | echo "APTPKG $pkg" >>"$td/control/conf" |
| 185 | rungit add control/conf |
| 186 | } |
| 187 | |
| 188 | defdir=/srv/dcp |
| 189 | repodir= |
| 190 | confopts=() |
| 191 | shared=n |
| 192 | |
| 193 | while [ "${1:0:1}" = - ]; do |
| 194 | opt="${1:1}" |
| 195 | shift |
| 196 | if [ "$opt" = d ]; then |
| 197 | repodir="$1" |
| 198 | shift |
| 199 | elif [ "$opt" = h ]; then |
| 200 | usage |
| 201 | exit 0 |
| 202 | elif [ "$opt" = s ]; then |
| 203 | shared=y |
| 204 | elif [ "$opt" = C ]; then |
| 205 | confopts=("${confopts[@]}" "$1") |
| 206 | shift |
| 207 | else |
| 208 | echo "dcp-init: unknown option '$opt'" >&2 |
| 209 | exit 1 |
| 210 | fi |
| 211 | done |
| 212 | |
| 213 | if [ $# -lt 1 ]; then |
| 214 | usage >&2 |
| 215 | exit 1 |
| 216 | fi |
| 217 | pkg="$1" |
| 218 | shift |
| 219 | if [ -z "$repodir" ]; then repodir="$pkg"; fi |
| 220 | if [[ "$repodir" != */* ]]; then |
| 221 | repodir="$defdir/${repodir}.git" |
| 222 | fi |
| 223 | |
| 224 | td="$(mktemp -d "/tmp/dcp-XXXXXX")" |
| 225 | initrepo |
| 226 | |
| 227 | initbase |
| 228 | rungit commit -q -m "Base control files" |
| 229 | |
| 230 | if [ "${#confopts[@]}" -gt 0 ]; then |
| 231 | for opt in "${confopts[@]}"; do |
| 232 | key="${opt%%=*}" |
| 233 | val="${opt#*=}" |
| 234 | echo "$key $val" >>"$td/control/conf" |
| 235 | done |
| 236 | rungit add control/conf |
| 237 | rungit commit -q -m "Custom configuration file" |
| 238 | fi |
| 239 | |
| 240 | rungit checkout -q -b upstream empty |
| 241 | getaptsrc "$pkg" |
| 242 | rungit commit -q -m "Initial upstream import" |
| 243 | rungit checkout master |
| 244 | rungit merge -n upstream |
| 245 | |
| 246 | initapt |
| 247 | rungit commit -q -m "APT control files" |
| 248 | |
| 249 | initvals() { |
| 250 | level=0 |
| 251 | } |
| 252 | initvals |
| 253 | curbranch=master |
| 254 | while [ $# -gt 0 ]; do |
| 255 | arg="$1" |
| 256 | shift |
| 257 | if [ "${arg:0:1}" = - ]; then |
| 258 | if [ "$arg" = -p ]; then |
| 259 | level="$1" |
| 260 | shift |
| 261 | elif [ "$arg" = -b ]; then |
| 262 | curbranch="$1" |
| 263 | shift |
| 264 | rungit checkout -q -b "$curbranch" |
| 265 | elif [ "$arg" = -B ]; then |
| 266 | curbranch="$1" |
| 267 | shift |
| 268 | rungit checkout -q -b "$curbranch" upstream |
| 269 | elif [ "$arg" = -m ]; then |
| 270 | rungit checkout -q master |
| 271 | rungit merge -n "$curbranch" |
| 272 | else |
| 273 | echo "dcp-init: unknown patch option '$arg'" >&2 |
| 274 | exit 1 |
| 275 | fi |
| 276 | else |
| 277 | ( |
| 278 | if [[ "$arg" == *.gz ]]; then |
| 279 | zcat "$arg" |
| 280 | else |
| 281 | cat "$arg" |
| 282 | fi |
| 283 | ) | patch -d "$td/src" -p"$level" |
| 284 | rungit add src |
| 285 | rungit commit -q -m "Applied $(basename "$arg")" |
| 286 | initvals |
| 287 | fi |
| 288 | done |
| 289 | |
| 290 | git clone -q --bare "$td" "$repodir" |
| 291 | if [ "$shared" = y ]; then |
| 292 | chmod -R g+w "$repodir" |
| 293 | td="$repodir" rungit config core.sharedrepository 1 |
| 294 | fi |
| 295 | |
| 296 | rm -rf "$td" |