~moonos-dev/moonos/4.0

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
#!/bin/bash
###############
# moonBundle Framework
# (c) 2010 by Chanrithy Thim <12rithy@gmail.com>
#
# Thank to KLIK2 and RUNZ project
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# See file /usr/share/common-licenses/GPL for more details.
###############

# Gettext support
. gettext.sh
TEXTDOMAIN=moonbundle
export TEXTDOMAIN
TEXTDOMAINDIR=/moonos/System/Locale
export TEXTDOMAINDIR

export MOONBUNDLE_VERSION="0.1"
export BUILD_DIR=$PWD
export RECEIPT=$BUILD_DIR/deb.receipt
export TMP_DIR=/tmp/moonbundle_build/$RANDOM
export UNIONFS=$TMP_DIR/unionfs
export ROOT_DIR=$TMP_DIR/root/
export COW_DIR=$TMP_DIR/cow
export PACKAGES_DIR=$BUILD_DIR/packages
export ZFTREE=$TMP_DIR/zftree
export DATA=$TMP_DIR/data.tar.gz
export DATA_DIR=$TMP_DIR/data
export FUSE_OPT="-o allow_other,use_ino,suid,dev,nonempty"
export UNION_OPT="-ocow,max_files=32768"
export SOURCETAR="$RECEIPT $DESCRIPTION"
export DATE=`date`
export DIBSTO_ID="`lsb_release -sd`"
export GET="wget -c"

function print_help()
{
echo -e "`gettext \"Usage: moonbundle-debbuild OPTION\"`"
echo -e "`gettext \"Building moonBundle file from Debian packages using a receipt\"`"
echo -e ""
echo -e "`gettext \"OPTION:\"`"
echo -e ""
echo -e "`gettext \"\\0-h\\t--help\\t\\tShow this message\"`"
echo -e "`gettext \"\\0-v\\t--version\\tShow version\"`"
echo -e ""
echo -e "`gettext \"Report bug to https://launchpad.net/moonos/+filebug\"`"

exit 0
}

if [[ $UID -ne 0 ]]; then
	echo "`gettext \"moonbundle-debbuild must be run as root\"`"
	exit 1
fi

# Check $1
if [ "$1" == "-h" ]; then
	print_help
elif [ "$1" == "--help" ]; then
	print_help
elif [ "$1" == "-v" ]; then
	echo -e "`eval_gettext \"moonBundle version: \\\$MOONBUNDLE_VERSION running in \\\$DISTRO_ID\"`"
	exit 0
elif [ "$1" == "--version" ]; then
	echo -e "`eval_gettext \"moonBundle version: \\\$MOONBUNDLE_VERSION running in \\\$DISTRO_ID\"`"
	exit 0
fi

# Check if receipt file exist then read it
if [ -f "$RECEIPT" ]; then
	chmod +x $RECEIPT
	. $RECEIPT
	mkdir -p $DATA_DIR
	cp $RECEIPT $DATA_DIR/receipt
else
	echo "`gettext \"Can not find receipt file!\"`"
	exit 1
fi

# Check if icon file exist
if [ -f "$BUILD_DIR/icon.png" ]; then
	cp $BUILD_DIR/icon.png $DATA_DIR/icon.png
elif [ -f "$BUILD_DIR/icon.svg" ]; then
	cp $BUILD_DIR/icon.svg $DATA_DIR/icon.svg
else
	echo "`gettext \"Using default icon...\"`"
	cp /usr/share/icons/hicolor/scalable/mimetypes/application-x-msb.svg $DATA_DIR/icon.svg
fi

export OUTPUT=$BUILD_DIR/${NAME}_${VERSION}-${BUNDLEVERSION}_`arch`.msb
echo "`eval_gettext \"Starting build on \\\$DATE\"`"

# Clean old msb
if [ -f "$OUTPUT" ]; then
	echo "`gettext \"Cleaning old build file...\"`"
	rm -f $OUTPUT
fi

# Mounting Unionfs
mkdir -p $UNIONFS
mkdir -p $ROOT_DIR
mkdir -p $COW_DIR
if ! mount --bind / $ROOT_DIR; then
	echo "`eval_gettext \"Error while try to mount / in \\\$ROOT_DIR\"`"
	exit 1
fi
if ! unionfs-fuse $FUSE_OPT $UNION_OPT $COW_DIR=RW:$ROOT_DIR=RO $UNIONFS; then
	echo "`gettext \"Can not mount unionfs...\"`"
	exit 1
fi

# Make moonbundle-dpkg
mkdir $COW_DIR/bin
echo '#!/bin/bash' > $COW_DIR/bin/moonbundle-dpkg
echo '' >> $COW_DIR/bin/moonbundle-dpkg
echo 'dpkg -i packages/*.deb >> /dev/null' >> $COW_DIR/bin/moonbundle-dpkg
echo '' >> $COW_DIR/bin/moonbundle-dpkg
echo 'dpkg --configure -a >> /dev/null' >> $COW_DIR/bin/moonbundle-dpkg
echo 'dpkg --configure -a >> /dev/null' >> $COW_DIR/bin/moonbundle-dpkg
echo 'dpkg --configure -a >> /dev/null' >> $COW_DIR/bin/moonbundle-dpkg
echo 'dpkg --configure -a >> /dev/null' >> $COW_DIR/bin/moonbundle-dpkg
chmod 755 $COW_DIR/bin/moonbundle-dpkg

# Get all dependencies need by application
if grep -q ^getdeb_rules $RECEIPT; then
	echo "`gettext \"executing getdeb_rules\"`"
	cd $PACKAGES_DIR/
	getdeb_rules
	cd $BUILD_DIR/
else
	echo "`gettext \"Running APT to get main packages and all dependencies...\"`"
	if ! apt-get --print-uris --yes install $DEBIAN_PACKAGE | grep ^\' | cut -d\' -f2 > $PACKAGES_DIR/download.list >> /dev/null; then
		echo "`gettext \"Can not run APT\"`"
		exit 1
	else
		cd $PACKAGES_DIR/
		if ! wget -c $PACKAGES_DIR/download.list; then
			echo "`gettext \"Can not run wget\"`"
			exit 1
		fi
	fi
fi

# Check if debian packages exist
if [ -d "$PACKAGES_DIR" ]; then
	mkdir -p $COW_DIR
	cp -r $PACKAGES_DIR $COW_DIR/
else
	echo "`eval_gettext \"Can not find any packages in \\\$BUILD_DIR\"`"
	exit 1
fi

# Installing packages
echo "`eval_gettext \"Installing packages to \\\$UNIONFS\"`"
if [ -d "$PACKAGES_DIR" ]; then
	if ! chroot $UNIONFS moonbundle-dpkg; then
		echo "`eval_gettext \"Error while try to install \\\$DEBIAN_PACKAGE\"`"
		if ! fusermount -u $UNIONFS; then
			echo "`eval_gettext \"Can not unmount \\\$UNIONFS\"`"
			sleep 0.5
			exit 1
		fi
		sleep 0.5
		if ! umount $ROOT_DIR; then
			echo "`eval_gettext \"Can not unmount \\\$ROOT_DIR\"`"
			sleep 0.5
			exit 1
		fi
		sleep 0.5
		exit 1
	fi
fi

# Unmounting Unionfs
sleep 1
if ! fusermount -u $UNIONFS; then
	echo "`eval_gettext \"Can not unmount \\\$UNIONFS\"`"
	sleep 2
	exit 1
fi
if ! umount $ROOT_DIR; then
	echo "`eval_gettext \"Can not unmount \\\$ROOT_DIR\"`"
	sleep 0.5
	exit 1
fi

# Execute genmsb_rules
mkdir -p $DATA_DIR/usr/share
if grep -q ^genmsb_rules $RECEIPT; then
	echo "`gettext \"executing genpkg_rules\"`"
	genmsb_rules
else
	echo "`gettext \"Skipping genmsb_rules\"`"
fi

# Packaging mini-runtime and generic data
echo "`gettext \"Packaging runtime and generic data...\"`"
mkdir -p $DATA_DIR/runtime
cp $(which moonbundle) $DATA_DIR/runtime
cp $(which moonbundle-launch) $DATA_DIR/runtime
cp $(which mhddfs) $DATA_DIR/runtime
cp $(which fuseiso) $DATA_DIR/runtime
cp /usr/lib/fakechroot/libfakechroot.so $DATA_DIR/runtime
echo "$EXEC" > $DATA_DIR/exec
cd $DATA_DIR
if ! tar -czf $DATA *; then
	echo "`gettext \"Can not pack runtime and generic data\"`"
	exit 1
fi

# DBus compatible
echo "`eval_gettext \"Copying DBus wrapper to \\\$ROOT_DIR...\"`"
mkdir -p $COW_DIR/bin
cp /moonos/System/moonBundle/moonbundle-dbus $COW_DIR/bin/

# Delete unneed files
rm -r $COW_DIR/packages
rm -r $COW_DIR/bin/moonbundle-dpkg
rm -r $COW_DIR/var/lib/dpkg
rm -r $COW_DIR/var/log
rm -r $COW_DIR/var/cache/man
rm -r $COW_DIR/var/cache/debconf

# Compress files
echo "`gettext \"Compressing...\"`"
cd $TMP_DIR
if ! mkzftree -V 1 $COW_DIR $ZFTREE >> /dev/null; then
	echo "`gettext \"Can not compress files\"`"
	exit 1
fi

# Turn it into ISO
echo "`gettext \"Creating bundle...\"`"
if ! genisoimage -zUR -input-charset utf-8 -l -D -p "$WEB" -V "$NAME" -volset "`gettext \"moonBundle archive\"`" -publisher "$MAINTAINER" -no-pad -o $OUTPUT $ZFTREE; then
	echo "`gettext \"Can not create bundle\"`"
	exit 1
fi

# Embed runtime
echo "`gettext \"Embeding runtime...\"`"
cp /moonos/System/moonBundle/mini-runtime/exec.sh $TMP_DIR
sed -i "s|@@@APP@@@|$NAME|" $TMP_DIR/exec.sh
if ! python /moonos/System/moonBundle/mini-runtime/runtimeinject $OUTPUT $TMP_DIR >> /dev/null; then
	echo "`gettext \"Can not embed runtime into bundle\"`"
	exit 1
fi

# Delete TMP folder
rm -r $TMP_DIR
cd $BUILD_DIR

# Make it executable
chmod 755 $OUTPUT

echo "`gettext \"Finish...\"`"