xref: /haiku/build/scripts/build_haiku_image (revision ed6250c95736c0b55da79d6e9dd01369532260c0)
1#!/bin/sh
2
3# The first argument is the shell script that initializes the variables:
4# sourceDir
5# outputDir
6# tmpDir
7# installDir
8# isImage
9# imagePath
10# imageSize
11# addBuildCompatibilityLibDir
12# sourceDirsToCopy
13# updateOnly
14# dontClearImage
15# isVMwareImage
16#
17# bfsShell
18# copyattr
19# fsShellCommand
20# makebootable
21# resattr
22# rc
23# unzip
24# vmdkheader
25#
26if [ $# -gt 0 ]; then
27	. $1
28	shift
29fi
30
31# this adds the build library dir to LD_LIBRARY_PATH
32eval "$addBuildCompatibilityLibDir"
33
34# map the shell commands
35if [ $isImage ]; then
36	sPrefix=:
37	tPrefix=/myfs/
38	cd="$fsShellCommand cd"
39	scd="$fsShellCommand cd"
40	cp="$fsShellCommand cp"
41	copyAttrs="$fsShellCommand cp -a"
42	ln="$fsShellCommand ln"
43	mkdir="$fsShellCommand mkdir"
44	rm="$fsShellCommand rm"
45	mkindex="$fsShellCommand mkindex"
46else
47	sPrefix=
48	# TODO: This should come from the environment.
49	tPrefix="$installDir/"
50	cd=cd
51	scd=:
52	cp="$copyattr -d"
53	copyAttrs="$copyattr"
54	ln=ln
55	mkdir=mkdir
56	rm=rm
57	mkindex=mkindex
58fi
59
60
61# attribute-safe rm -rf
62# This makes sure there are no leftover attribute file before removing each file
63attrrmrf()
64{
65	test -e "$1" || return
66	if [ -d "$outputDir/attributes" ]; then
67		find "$1" -print0 | xargs -0 stat -c %i | awk "{ print \"$outputDir/attributes/\" \$1 }" | xargs rm -rf
68	fi
69	rm -rf "$1"
70}
71
72unzipFile()
73{
74	# unzipFile <archive> <directory>
75	zipFile=$1
76	targetUnzipDir=$2
77
78	echo "Unzipping $zipFile ..."
79
80	if [ $isImage ]; then
81		unzipDir=$tmpDir/unzip
82		attrrmrf "$unzipDir"
83		mkdir -p "$unzipDir"
84
85		$unzip -q -d "$unzipDir" "$zipFile"
86		$cp -r "${sPrefix}$unzipDir/." "${tPrefix}$targetUnzipDir"
87
88		attrrmrf "$unzipDir"
89	else
90		$unzip -q -o -d "${tPrefix}$targetUnzipDir" "${sPrefix}$zipFile"
91	fi
92}
93
94
95# create the image and mount it
96if [ $isImage ]; then
97	echo
98
99	imageOffsetFlags=
100	if [ $isVMwareImage ]; then
101		imageOffsetFlags="--start-offset 65536"
102	fi
103
104	if [ ! $updateOnly ]; then
105		echo "Creating image ..."
106
107		ddFlags=
108		if [ $isVMwareImage ]; then
109			rm -f $imagePath
110			$vmdkheader -h 64k -i${imageSize}M $imagePath || exit 1
111			ddFlags="conv=notrunc oflag=append"
112			dontClearImage=
113		fi
114
115		if [ ! -e $imagePath -o ! "$dontClearImage" ]; then
116			dd if=/dev/zero of=$imagePath bs=1048576 count=$imageSize $ddFlags
117		fi
118		$bfsShell --initialize $imageOffsetFlags $imagePath Haiku
119		$makebootable $imageOffsetFlags $imagePath
120	fi
121	$bfsShell -n $imageOffsetFlags $imagePath > /dev/null &
122	sleep 1
123fi
124
125# create BEOS:APP_SIG index -- needed to launch apps via signature
126if [ ! $updateOnly ]; then
127	$mkindex BEOS:APP_SIG
128fi
129
130echo "Populating image ..."
131while [ $# -gt 0 ]; do
132	. $1
133	shift
134done
135
136
137# install MIME database
138# TODO: It should be possible to do that in the build system too.
139
140if [ ! $updateOnly ]; then
141	mimeDBSource=$sourceDir/src/data/beos_mime
142	mimeDBDest=${tPrefix}home/config/settings/beos_mime
143
144	echo "Deleting old MIME database ..."
145
146	$rm -rf $mimeDBDest
147	$mkdir -p $mimeDBDest
148	mkdir -p $tmpDir
149	mimeTmpDir=$tmpDir/mime
150	mimeTmpIndex=0
151
152	# create tmp dir for the MIME conversion stuff
153	mkdir -p $mimeTmpDir
154
155	echo "Installing MIME database ..."
156
157	for inSuperFile in $mimeDBSource/*.super; do
158		superType=$(basename $inSuperFile .super)
159		outSuperDir=$mimeDBDest/$superType
160
161		# compile rdef to rsrc file and the rsrc file to attributes
162		mimeTmpIndex=$(($mimeTmpIndex + 1))
163		tmpFile=$mimeTmpDir/mimedb$$_${mimeTmpIndex}.rsrc
164		tmpFile2=$mimeTmpDir/mimedb$$_${mimeTmpIndex}.mime
165		$rc -o $tmpFile $inSuperFile
166		mkdir -p $tmpFile2
167		$resattr -O -o $tmpFile2 $tmpFile
168		$cp -r ${sPrefix}$tmpFile2 $outSuperDir
169
170		# iterate through the sub types
171		for inSubFile in $mimeDBSource/$superType/*; do
172			# check, if the type exists
173			if test -f $inSubFile && grep META:TYPE $inSubFile > /dev/null 2>&1 ; then
174				subType=$(basename $inSubFile)
175				outSubFile=$outSuperDir/$subType
176
177				# compile rdef to rsrc file and the rsrc file to attributes
178				mimeTmpIndex=$(($mimeTmpIndex + 1))
179				tmpFile=$mimeTmpDir/mimedb$$_${mimeTmpIndex}.rsrc
180				tmpFile2=$mimeTmpDir/mimedb$$_${mimeTmpIndex}.mime
181				$rc -o $tmpFile $inSubFile
182				$resattr -O -o $tmpFile2 $tmpFile
183				$cp ${sPrefix}$tmpFile2 $outSubFile
184			fi
185		done
186	done
187
188	# cleanup tmp dir
189	attrrmrf $mimeTmpDir
190fi	# ! updateOnly
191
192
193# install sources
194
195sourcesDest=${tPrefix}home/HaikuSources
196
197# create sources directory
198if [ -n "${sourceDirsToCopy}" ]; then
199	echo "Installing Haiku Sources ..."
200
201	$mkdir -p ${sourcesDest}
202fi
203
204# When building in the root of the source directory, sourceDir is "." and
205# the source directory are not prefixed, which breaks the sed expressions
206# used below. We work around, by adjusting the strings to match and insert.
207sourcePathPrefix=$sourceDir
208destPathPrefix=$sourcesDest
209if [ $sourcePathPrefix = "." ]; then
210	sourcePathPrefix=""
211	destPathPrefix="${sourcesDest}/"
212fi
213
214for sourcesDir in ${sourceDirsToCopy}; do
215	echo "  sources dir:  ${sourcesDir}"
216
217	# create all subdirectories
218	subDirs=$(find ${sourcesDir} -type d | grep -v '.svn' |
219		sed -e "s@^${sourcePathPrefix}@${destPathPrefix}@")
220	$mkdir -p ${subDirs}
221
222	# get all files and copy each one individually
223	sourceFiles=$(find $sourcesDir -type f | grep -v '.svn')
224	for sourceFile in $sourceFiles; do
225		destSourceFile=$(echo $sourceFile |
226			sed -e "s@^${sourcePathPrefix}@${destPathPrefix}@")
227		$cp ${sPrefix}$sourceFile $destSourceFile
228	done
229done
230
231
232# unmount
233if [ $isImage ]; then
234	echo "Unmounting ..."
235	$fsShellCommand sync
236	$fsShellCommand quit
237fi
238