summaryrefslogtreecommitdiff
path: root/xibuild/build_package.sh
blob: 44521a9c7627ab62439c707f8942c685a7c8af59 (plain)
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
27
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash

GREEN="\033[0;32m"
BLUE="\033[0;34m"

BUILDFILE=$1
REPO=$(echo $BUILDFILE | rev | cut -d/ -f2 | rev)
NAME=$(basename $BUILDFILE .xibuild)

source $BUILDFILE

extract () {
    FILE=$1
    case "${FILE##*.}" in 
        "gz" )
            tar -zxf $FILE
            ;;
        "lz" )
            tar --lzip -xf "$FILE"
            ;;
        "zip" )
            unzip $FILE
            ;;
        * )
            tar -xf $FILE
            ;;
    esac
}

package_exists () {
    local exported="$XIB_EXPORT/repo/$REPO/$NAME"
    local exported_pkg="$exported.xipkg"
    local exported_pkg_info="$exported.xipkg.info"
    local exported_pkg_build="$exported.xibuild"

    if [ -f $exported_pkg ] && [ -f $exported_pkg_build ]; then
        local built_sum=$(md5sum $exported_pkg_build | cut -d" " -f1)
        local current_sum=$(md5sum $BUILDFILE | cut -d" " -f1)
        
        if [ "$built_sum" = "$current_sum" ]; then
            return 0
        fi
    fi

    return 1
}

fetch_source () {
    # download additional files
    local src_dir="$XIB_CHROOT/build/source"
    mkdir -p $src_dir

    cd $src_dir

    if [ ! -z ${SOURCE} ]; then

        if git ls-remote -q $SOURCE $BRANCH &> /dev/null; then
            # The source is a git repo
            git clone $SOURCE . &> /dev/null
            git checkout $BRANCH &> /dev/null
        else
            # The source is a file

            local downloaded_file=$(basename $SOURCE)
            curl -SsL $SOURCE > $downloaded_file
            extract $downloaded_file

            # if the extracted file only had one directory
            if [ "$(ls -l | wc -l)" = "3" ]; then
                for file in */* */.*; do 
                    echo $file | grep -q '\.$' || mv $file .
                done;
            fi
        fi
    fi

    # download additional files
    if [ ! -z ${ADDITIONAL} ]; then
        for url in ${ADDITIONAL[*]}; do
            local name=$(basename $url)
            curl -SsL $url > $src_dir/$name 
        done
    fi
}


clean_chroot () {
    local export_dir="$XIB_CHROOT/export"
    local build_dir="$XIB_CHROOT/build"

    rm -rf $export_dir
    rm -rf $build_dir

    mkdir -p $export_dir
    mkdir -p $build_dir
    
    mkdir -p "$XIB_EXPORT/repo/$REPO/"
}

make_buildscript () {

    echo MAKEFLAGS="$MAKEFLAGS" >> "$XIB_CHROOT/build/profile"
    echo LDFLAGS="$LDFLAGS" >> "$XIB_CHROOT/build/profile"

    cat > "$XIB_CHROOT/build/build.sh" << "EOF"
#!/bin/bash
source /build/profile
export PKG_NAME=$(cat /build/name)
export PKG_DEST=/export

prepare () {
    echo "passing prepare"
}

build () {
    echo "passing build"
}

check () {
    echo "passing check"
}

package () {
    echo "passing package"
}

cd /build
ls
source $PKG_NAME.xibuild
cd /build/source

echo "==========================PREPARE STAGE=========================="
prepare || exit 1
echo "==========================BUILD STAGE=========================="
build || exit 1
echo "==========================CHECK STAGE=========================="
check || exit 1
echo "==========================PACKAGE STAGE=========================="
package || exit 1

printf "checking for postinstall... "
if command -v postinstall > /dev/null; then 
    echo "adding postinstall"
    POSTINSTALL=$(type postinstall | sed '1,3d;$d')
    if [ ${#POSTINSTALL} != 0 ]; then
        POST_DIR=$PKG_DEST/var/lib/xipkg/postinstall
        mkdir -p $POST_DIR
        echo "#!/bin/sh" > $POST_DIR/$PKG_NAME.sh
        echo $POSTINSTALL >> $POST_DIR/$PKG_NAME.sh
    fi
else
    echo "no postinstall"
fi
EOF
    chmod 700 "$XIB_CHROOT/build/build.sh"
}

package () {
    local export_repo="$XIB_EXPORT/repo/$REPO"
    local export_pkg="$XIB_EXPORT/repo/$REPO/$NAME.xipkg"
    local pkg_dest="$XIB_CHROOT/export"

    cd "$pkg_dest"
    if [ "$(ls -1 | wc -l)" = "0" ]; then
        printf " package is empty;"
        [ -z "${SOURCE}" ] || exit 1;
    fi
    tar -C $pkg_dest -czf $export_pkg ./
}

build () {
    printf "$BLUE\tCleaning chroot..." 
    clean_chroot && printf "$GREEN prepared\n" || return 1

    printf "$BLUE\tfetching source..." 
    fetch_source && printf "$GREEN fetched $(du -sh "$XIB_CHROOT/build/source" | awk '{ print $1 }')\n" || return 1

    printf "$BLUE\tgenerating buildscript..." 
    make_buildscript && printf "$GREEN generated\n" || return 1

    cp "$BUILDFILE" "$XIB_CHROOT/build/"
    printf $NAME > "$XIB_CHROOT/build/name"

    local log_file="$XIB_EXPORT/repo/$REPO/$NAME.log"

    printf "$BLUE\tBuilding package..." 
    xichroot $XIB_CHROOT /build/build.sh &> $log_file && printf "$GREEN built!\n" || return 1

    printf "$BLUE\tPackaging package..." 
    package && printf "$GREEN packaged to $(du -sh "$XIB_EXPORT/repo/$REPO/$NAME.xipkg" | awk '{ print $1 }')!\n" || return 1

    printf "$BLUE\tCreating package info..."
    create_info && printf "$GREEN created info!\n" || return 1

    # TODO check if the key exists, if not, skip signing
    printf "$BLUE\tSigning package..."
    sign && printf "$GREEN signed!\n" || return 1

    cp "$BUILDFILE" "$XIB_EXPORT/repo/$REPO/"
}

[ -z "${XIB_CHROOT}" ] && echo "CRITICAL! No chroot env variable set!" && exit 1;

package_exists && printf "\tPackage exists!\n" || build