build_package.sh 3.85 KB
Newer Older
1 2
#!/bin/bash

3
PREFIX="$(dirname $0)"
4 5
REPOROOT="$PREFIX/.."
PACKAGESDEST="$REPOROOT/pkgs"
6 7
AUTHORS="$REPOROOT/doc/AUTHORS"
COPYING="$REPOROOT/doc/COPYING"
8 9 10 11

function showHelp() {
    echo "$0 <package> [major|minor|revision]"
    echo ""
12 13 14
    echo -n "Generate <package> passed as argument, increment the package"
    echo -n " version according to the second argument (unless -n is"
    echo    " supplied). Version is formatted as 'major.minor.revision'."
15 16
    echo "If the second argument is empty, revision is assumed."
    echo "The generated packages will be stored in $PACKAGESDEST"
17 18 19 20
    echo ""
    echo "Options are:"
    echo -e "-n\tDoes not increment the package version."

21 22
}

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
# This function is used to keep all the packages with their AUTHORS and
# COPYING files up to date.
function copyDoc() {

    local pkg=$1
    local copyDir="$REPOROOT/$pkg/package/usr/local/share/doc/$pkg/"

    if ! test -d "$copyDir"; then
        mkdir -p "$copyDir"
    fi

    cp "$AUTHORS" "$copyDir"
    cp "$COPYING" "$copyDir"
}

38 39 40
# Update the package ($1) version based on $kind ($2), where $kind can be
# revision, minor, or major.
function updatePackageVersion () {
41 42 43
    local pkg=$1
    local kind=$2

44
    local controlFile="$REPOROOT/$pkg/package/DEBIAN/control"
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    local version=""
    local newVersion=""
    local major=""
    local minor=""
    local revision=""

    version=$(grep "^Version:" $controlFile | awk '{print $2}')

    major=$(echo $version | cut -d. -f1)
    minor=$(echo $version | cut -d. -f2)
    revision=$(echo $version | cut -d. -f3)

    case $kind in
        major)
            ((major++))
            minor=0
            revision=0
            ;;
        minor)
            ((minor++))
            revision=0
            ;;
        *)
            ((revision++))
            ;;
    esac

    newVersion="${major}.${minor}.${revision}"

    sed -i -e "s/Version:.*/Version: $newVersion/" $controlFile
75
}
76

77 78 79 80 81
# Generate a debian package for $pkg ($1)
function createPackage() {
    local pkg=$1

    local ret=0
82

83
    fakeroot dpkg -b $REPOROOT/$pkg/package/ $PACKAGESDEST/
84

85
    return $?
86 87 88 89 90
}

# ------------- main

# Check arguments
91
if test $# -lt 1 -o $# -gt 3; then
92 93 94 95
    showHelp
    exit 1
fi

96 97
# Args
PKG=""
98
KIND=""
99 100 101 102 103 104 105 106
UPDATEVERSION=true

while test $# -gt 0; do
    arg=$1
    case $arg in
        -n)
            UPDATEVERSION=false
            ;;
107
        *)
108 109 110 111 112 113 114 115 116 117 118 119 120 121
            if test -z "$PKG"; then
                PKG=$arg
            elif test -z "$KIND"; then
                KIND=$arg
            else
                echo "Unrecognized parameter $arg"
                showHelp
                exit 1
            fi
            ;;
    esac
    shift
done

122 123 124 125 126
if ! which fakeroot &>/dev/null; then
    echo "ERROR: make sure you have the fakeroot package installed."
    exit 1
fi

127 128 129 130
if test -z "$PKG"; then
    echo "Error: The package to build must be specified"
    exit 2
fi
131 132 133

if ! test -d $REPOROOT/$PKG; then
    echo "Package $PKG not found in $REPOROOT"
134
    exit 3
135 136 137 138
fi

if ! mkdir -p $PACKAGESDEST; then
    echo "Error while creating directory to store packages ($PACKAGESDEST)"
139
    exit 4
140 141
fi

142 143 144 145 146
# Updating the revision is the default behavior
if test -z "$KIND"; then
    KIND="revision"
fi

147
# Run prebuild script
148 149
prebuild="$REPOROOT/$PKG/prebuild"
if test -x "$prebuild"; then
150
    echo "Running prebuild script..."
151
    if ! $prebuild; then
152
        echo "ERROR while running prebuild script."
153
        exit 5
154 155 156
    fi
fi

157 158 159
# Update package doc (authors and license)
copyDoc "$PKG"

160 161 162 163 164
# Update Version
if test "$UPDATEVERSION" = "true"; then
    updatePackageVersion "$PKG" "$KIND"
fi

165
# Build Package
166
if ! createPackage "$PKG"; then
167
    echo "ERROR while building package"
168
    exit 6
169 170
fi

171
# Run postbuild script
172 173
postbuild="$REPOROOT/$PKG/postbuild"
if test -x "$postbuild"; then
174
    echo "Running postbuild script..."
175
    if ! $postbuild; then
176
        echo "ERROR while running postbuild script."
177
        exit 7
178 179 180 181
    fi
fi

exit 0