build_package.sh 2.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash

PREFIX="$(dirname $0)"
REPOROOT="$PREFIX/.."
PACKAGESDEST="$REPOROOT/pkgs"

function showHelp() {
    echo "$0 <package> [major|minor|revision]"
    echo ""
    echo -n "Generate <package> passed as argument, increment the package version "
    echo    "according to the second argument. The version is 'major.minor.revision'."
    echo "If the second argument is empty, revision is assumed."
    echo "The generated packages will be stored in $PACKAGESDEST"
}

# Generate a debian package for $pkg ($1), updating the version based on
# $kind ($2), where $kind can be revision, minor, or major.
function createPackage() {
    local pkg=$1
    local kind=$2

    local version=""
    local newVersion=""
    local major=""
    local minor=""
    local revision=""
27
    local controlFile="$REPOROOT/$pkg/package/DEBIAN/control"
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    local ret=""

    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

55
    fakeroot dpkg -b $REPOROOT/$pkg/package/ $PACKAGESDEST/
56
57
    ret=$?

58
59
60
61
62
63
# FIXME This might not be what the user wants. He might lose some other
# changes on the control file, besides the Version field
#    if test $ret -ne 0; then
#        git checkout $controlFile
#        return $ret
#    fi
64
65
66
67
68
69
70
71
72
73
74
75

    return 0
}

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

# Check arguments
if test $# -lt 1 -o $# -gt 2; then
    showHelp
    exit 1
fi

76
77
78
79
80
if ! which fakeroot &>/dev/null; then
    echo "ERROR: make sure you have the fakeroot package installed."
    exit 1
fi

81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Globals
PKG="$1"
KIND="$2"

if ! test -d $REPOROOT/$PKG; then
    echo "Package $PKG not found in $REPOROOT"
    exit 2
fi

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

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
prebuild="$REPOROOT/$PKG/prebuild"
postbuild="$REPOROOT/$PKG/postbuild"
if test -x "$prebuild"; then
    echo "Running prebuild script..."
    if ! $prebuild; then
        echo "ERROR while running prebuild script."
        exit 4
    fi
fi

if ! createPackage "$PKG" "$KIND"; then
    echo "ERROR while building package"
    exit 5
fi

if test -x "$postbuild"; then
    echo "Running postbuild script..."
    if ! $postbuild; then
        echo "ERROR while running postbuild script."
        exit 6
    fi
fi

exit 0