#!/bin/bash set -e usage() { echo "usage: dcp-init [-sh] [-C key=val] [-d REPODIR] PACKAGE [(PATCH|-m)...]" echo " PATCH ::= [-p LEVEL] [-bB BRANCH] FILE" echo " -b creates a new branch at the current patch" echo " -B creates a new branch at the upstream sources" echo " -m merges the current branch into the master branch" } rungit() { ( cd "$td" git "$@" ) || false } getaptsrc() { mkdir "$td/apt-tmp" (cd "$td/apt-tmp"; apt-get source "$1") || false sdir= for f in "$td/apt-tmp"/*; do if [ -d "$f" ]; then if [ -z "$sdir" ]; then sdir="$f" else echo "dcp-init: got more than one directory from apt-get; cannot continue" >&2 exit 1 fi fi done if [ -z "$sdir" ]; then echo "dcp-init: could not find any source directory" >&2 exit 1 fi mv "$sdir" "$td/src" rm -rf "$td/apt-tmp" rungit add src } initrepo() { rungit init mkdir "$td/control" touch "$td/control/conf" rungit add control rungit commit -q -m "Initial repo" rungit tag empty } initbase() { mkdir "$td/control/build.d" mkdir "$td/control/update.d" cat >"$td/control/functions" <repo/src/debian/changelog.new <"$td/control/build" <"$td/control/update" <"$td/control/build.d/99dpkg" <"$td/control/update.d/upstream" <"$td/control/update.d/post-logchange" <>"$td/control/conf" rungit add control/conf } defdir=/srv/dcp repodir= confopts=() shared=n while [ "${1:0:1}" = - ]; do opt="${1:1}" shift if [ "$opt" = d ]; then repodir="$1" shift elif [ "$opt" = h ]; then usage exit 0 elif [ "$opt" = s ]; then shared=y elif [ "$opt" = C ]; then confopts=("${confopts[@]}" "$1") shift else echo "dcp-init: unknown option '$opt'" >&2 exit 1 fi done if [ $# -lt 1 ]; then usage >&2 exit 1 fi pkg="$1" shift if [ -z "$repodir" ]; then repodir="$pkg"; fi if [[ "$repodir" != */* ]]; then repodir="$defdir/${repodir}.git" fi td="$(mktemp -d "/tmp/dcp-XXXXXX")" initrepo initbase rungit commit -q -m "Base control files" if [ "${#confopts[@]}" -gt 0 ]; then for opt in "${confopts[@]}"; do key="${opt%%=*}" val="${opt#*=}" echo "$key $val" >>"$td/control/conf" done rungit add control/conf rungit commit -q -m "Custom configuration file" fi rungit checkout -q -b upstream empty getaptsrc "$pkg" rungit commit -q -m "Initial upstream import" rungit checkout master rungit merge -n upstream initapt rungit commit -q -m "APT control files" initvals() { level=0 } initvals curbranch=master while [ $# -gt 0 ]; do arg="$1" shift if [ "${arg:0:1}" = - ]; then if [ "$arg" = -p ]; then level="$1" shift elif [ "$arg" = -b ]; then curbranch="$1" shift rungit checkout -q -b "$curbranch" elif [ "$arg" = -B ]; then curbranch="$1" shift rungit checkout -q -b "$curbranch" upstream elif [ "$arg" = -m ]; then rungit checkout -q master rungit merge -n "$curbranch" else echo "dcp-init: unknown patch option '$arg'" >&2 exit 1 fi else ( if [[ "$arg" == *.gz ]]; then zcat "$arg" else cat "$arg" fi ) | patch -d "$td/src" -p"$level" rungit add src rungit commit -q -m "Applied $(basename "$arg")" initvals fi done git clone -q --bare "$td" "$repodir" if [ "$shared" = y ]; then chmod -R g+w "$repodir" td="$repodir" rungit config core.sharedrepository 1 fi rm -rf "$td"