summaryrefslogtreecommitdiff
path: root/scripts/mkipkg
blob: 54651af15e86fe685c3be26c16d335aa23abd403 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/bin/sh
# vim: et sw=4

# TODO: srcdir!=builddir

usage()
{
    echo "usage: $self --destdir=[destination installation directory]"
    echo "usage: $self [destination installation directory]"
    exit 1
}

self=mkipkg
destdir=
strip=strip
control=
builddir=
srcdir=
prefix=
preinst=
postinst=
prerm=
postrm=
mkfsjffs2=
ipkgbuild=ipkg-build
buildversion=
subst=
user=root
group=root
filesubst=

oldpwd=

for option
do
    case "$option" in
        -*=*)
            arg=`echo "$option" | sed 's/[-_a-zA-Z0-9]*=//'`
            ;;
    esac

    case "$option" in
        --destdir=*)
            destdir=$arg
            ;;
        --strip=*)
            strip=$arg
            ;;
       --control=*)
            control=$arg
            ;;
       --preinst=*)
            preinst=$arg
            ;;
       --postinst=*)
            postinst=$arg
            ;;
       --prerm=*)
            prerm=$arg
            ;;
       --postrm=*)
            postrm=$arg
            ;;
       --builddir=*)
            builddir=$arg
            ;;
       --srcdir=*)
            srcdir=$arg
            ;;
       --prefix=*)
            prefix=$arg
            ;;
       --mkfsjffs2=*)
            mkfsjffs2=$arg
            ;;
       --ipkgbuild=*)
            ipkgbuild=$arg
            ;;
       --buildversion=*)
            buildversion=$arg
            ;;
       --subst=*)
            subst=$arg
            ;;
       --user=*)
            user=$arg
            ;;
       --group=*)
            group=$arg
            ;;
       --filesubst=*)
            filesubst=$arg
            ;;
       --classic)
            classic=-c
            ;;
       -*)
            usage
            ;;
       *)
            destdir=$option
            ;;
    esac
done

if [ -z "$control" ]; then
    control=$destdir/CONTROL/control
fi
if [ -z "$prerm" ]; then
    prerm=$destdir/CONTROL/prerm
fi
if [ -z "$postrm" ]; then
    postrm=$destdir/CONTROL/postrm
fi
if [ -z "$preinst" ]; then
    preinst=$destdir/CONTROL/preinst
fi
if [ -z "$postinst" ]; then
    postinst=$destdir/CONTROL/postinst
fi

# remove leading slash from prefix (to fix globbing)
if [ -n "$prefix" ]; then
    prefix=`echo $prefix | sed -e "s,/\(.*\),\\1,"`
fi

if [ -z "$destdir" ]; then
    usage
fi

if [ ! -r $control ]; then
   echo "$self: cannot find $control, exiting..."
   exit 1
fi

if [ -z "`which $ipkgbuild 2>/dev/null`" ]; then
    echo "$self: cannot find ipkg-build, exiting..."
    exit 1
fi

findFile()
{
    local path=
    if [ $# = 1 ]; then
        find $1 -type f -o -type b -o -type c -o -type l -o -type d 
    else
        find . -type f -o -type b -o -type c -o -type l -o -type d | \
	sed -e "s,\./\(.*\),\\1,g"
    fi

}

_pushd() { oldpwd=`pwd`; cd $1; }
_popd() { cd $oldpwd; }

setVar()
{
    eval "$1='$2'"
}

expandMaskToList()
{
    local _list=`echo $1`
    local _tmpFileList=
    for f in $_list; do
        if [ -d $f ]; then
            find="`findFile $f`"
            if [ -z "$find" ]; then
                find="$f"
            fi
            f="$f `findFile $f`"
        fi
        _tmpFileList=$f" $_tmpFileList"
    done
    setVar $2 "$_tmpFileList"
}

createFileList()
{
    local excludeMask
    local includeMask
    if (cat $1|grep -q ^FileExcludeMask); then
        excludemaskpresent=1
        excludeMask=$(eval echo '"'$(sed -n -e "s,^FileExcludeMask: *,,p" $1)'"')
    fi
    if (cat $1|grep -q ^FileIncludeMask); then
        includemaskpresent=1
        includeMask=$(eval echo '"'$(sed -n -e "s,^FileIncludeMask: *,,p" $1)'"')
    else
        if (cat $1|grep -q ^Files:); then
            includemaskpresent=1
            includeMask=$(eval echo '"'$(sed -n -e "s,^Files: *,,p" $1)'"')
        fi
    fi
 
    excludeMask="$excludeMask CONTROL/* usr/share/* CVS/* SCCS/*"

    if [ $includemaskpresent == 1 ]; then
        if [ -z "$includeMask" ]; then
            setVar $2 ""
            return 0
        fi
        _pushd $destdir
        expandMaskToList "$includeMask" _fileList
	_popd
    else
        _fileList=`cd $destdir && findFile`
    fi

    _excludeList=
    if [ -n "$excludeMask" ]; then
        _pushd $destdir
        expandMaskToList "$excludeMask" _excludeList
        _popd
    fi

    local realFileList=
    local missing=0
    for file in $_fileList; do
        local containedInList=0
        for i in $_excludeList; do
            if [ $file = $i ]; then
                containedInList=1
            fi
        done
        if [ $containedInList = 0 ]; then
            if [ ! -e $file ]; then
#                echo "$self: $file not found"
                missing=1
            fi
            realFileList=$file" $realFileList"
        fi
    done

    if [ $missing = 1 ]; then
        return 1
    fi

    setVar $2 "$realFileList"
}

stripFile()
{
    if [ -f $1 -a -x $1 ]; then
        $strip --strip-all $1
    fi
}

substFile()
{
    local oldfile=$1

    if [ ! -e $2 ]; then return 1; fi

    if [ -e $oldfile ]; then
        newfile=./`echo $1|sed -f $2|sed -e's,^/,,g'`
        olddir=./`dirname $1`
        newdir=`dirname $newfile`

        if [ "$newdir" = "$olddir" ]; then
            return 0
        fi

        mkdir -p $newdir
        mv $1 $newfile
        rmdir -p $olddir 2>/dev/null
    fi
}

stripFiles()
{
    for f in $1; do
        stripFile $f
    done
}

substFiles()
{
    for f in $1; do
        substFile $f
    done
}

substAndStripFiles()
{
    for f in $1; do
        stripFile $f
        substFile $f $2
    done
}

installScript()
{
    if [ -n "$1" -a -f "$1" ]; then
        destfile=`basename $1`
        if [ -n "$subst" ]; then
            sed -f $subst < $1 > $ctrldir/$destfile
        else
            cat $1 > $ctrldir/$destfile
        fi
        chmod +x $ctrldir/$destfile
    fi
}

tempDir=/tmp/`basename $self`.$$
mkdir -p $tempDir
if [ $? != 0 ]; then
    echo "$self: cannot create $tempDir, exiting..."
    exit 1
fi

cd $destdir; createFileList $control ipkgFileList
if [ "$?" != "0" ]; then
    echo "$self: ERROR: missing files, not building $control"
    exit 1
fi

( cd $destdir && tar cf - $ipkgFileList 2>/dev/null ) | \
( cd $tempDir && tar xf - 2>/dev/null )

if [ -z "$filesubst" ]; then
    ( cd $tempDir && stripFiles "$ipkgFileList" )
else
    if ! (echo $filesubst|grep -q '^/'); then
        filesubst="$oldpwd/$filesubst"
    fi
    ( cd $tempDir && substAndStripFiles "$ipkgFileList" $filesubst )
fi

path="`echo "$PATH" | sed -e "s/\:/ /g"`"
if [ -z "$mkfsjffs2" ]; then
    for i in $path; do
      if [ -x "$i/mkfs.jffs2" ]; then
        mkfsjffs2="$i/mkfs.jffs2"
        break
      fi
    done
fi

if [ -z "$mkfsjffs2" ]; then
    echo "$self: WARNING: no mkjfs.jffs2 found in path. Falling back to using du"
    echo "for size calculation. mkfs.jffs2 is recommended for size calculation"
    echo "as it calculates the real package size on the compressed file system,"
    echo "in contrast to du calculating the uncompressed size!"
    buildsize=`du -h -s $tempDir | awk '{print $1}'`
else
    buildsize=`$mkfsjffs2 -r $tempDir | wc -c`
fi

ctrldir=$tempDir/CONTROL

mkdir -p $ctrldir

if [ ! -e $subst ] || [ -z "$subst" ]; then
    cat $control | egrep -v '^(FileIncludeMask|FileExcludeMask|Files):' > $ctrldir/control
else
    sed -f $subst < $control | egrep -v '^(FileIncludeMask|FileExcludeMask|Files):' > $ctrldir/control
fi

if [ "$buildsize" != "0" ]; then
    echo "Installed-Size: $buildsize" >> $ctrldir/control
fi

installScript $preinst
installScript $postinst
installScript $prerm
installScript $posrm

$ipkgbuild $classic -o $user -g $group $tempDir

rm -rf $tempDir