#!/bin/sh

# Usage: makehdimage [-i includeFile] [image path/target dir]
#
# This script will compile all files needed to create a bootable Haiku image,
# create such an image, and copy the files to the correct location.
#
# If you don't specify the image, $sourceDir/haiku.image will be used. In
# the current default configuration, you have to start this script in the
# "current" directory of the Haiku tree. A 80 MB image will be created there.
#
# If you specify a target directory, no image is going to be created at
# all.
#
# If you pass `-i includeFile', the file is sourced before unmounting the
# image.
#
# The image is populated using the bfs_shell which is created from the sources.
# The bfs_shell emulates a mini-kernel evironment with a mounted BFS. The
# program fs_shell_command is used to deliver commands to it. Due to this
# setup a few things need to be kept in mind, if you intend to modify the
# script and want it to still work.
#
# All commands that modify the target (directory or image) are to be called
# through variables (defined after the jam invocation block). This ensures
# that both applications (creating and image and just installing the stuff in
# a target directory) do still work. Additionally there's a variable `sPrefix'
# defined, which has to be prepended to every source path, when passing the
# path to such a command.
#
# A further particularity to consider is changing the current working
# directory. When the shell command `cd' is invoked here, it affects only
# the shell which executes this script; the bfs_shell will know nothing of
# it. Hence, if you want the bfs_shell to know of a changed directory, you
# must tell it. There are two commands bound to variables: `$cd' should be
# used, when you want to change into a target directory, `$scd' when the
# source directory shall be changed. In the latter case you usually invoke
# first a normal shell `cd' and then the `$scd', so that both, the shell and
# the bfs_shell know about the new dir (depending on what you intend to do
# one may be enough, though).


# path of the checked out Haiku sources
sourceDir=.
alreadyMounted=
targetDir=/haiku
includeFile=

# check whether there's a file to be included
if [ $# \> 0 ]; then
	if [ "$1" = "-i" ]; then
		shift
		if [ $# \> 0 ]; then
			includeFile="$1"
			shift
		else
			echo "Error: Option \`-i' requires a parameter!"
			exit 1
		fi
	fi
fi

# the architecture; ToDo: set correctly (using uname?)
arch=x86

if [ "$DEBUG" \> 0 ]; then
	type=debug_$DEBUG
else
	type=release
fi
date=`date +%Y-%m-%d.%H:%M`

# disk image path, size in MB
if [ -d "$1" ]; then
	alreadyMounted=true
	targetDir=$1
elif [ "$1" == "" ]; then
	imagePath=$sourceDir/haiku.${arch}.${type}.${date}.image
else
	imagePath=$1
fi

imageSize=80
	# Bochs parameters:
	# 40 MB: cylinders=82, heads=16, spt=63
	# 60 MB: cylinders=122, heads=16, spt=63
	# 80 MB: cylinders=164, heads=16, spt=63
	# 100 MB: cylinders=204, heads=16, spt=63

# build everything needed
previousDir=`pwd`
cd $sourceDir
sourceDir=`pwd`
	# we accept relative source directories as well

if [ $alreadyMounted ]; then
	echo "Building binaries and copying them to $targetDir"
else
	echo "Building $imageSize MB image at $imagePath..."
fi

# paths of helper commands
subpath=${arch}.${type}.haiku
rc=$sourceDir/objects/${subpath}/tools/rc/rc
resattr=$sourceDir/objects/${subpath}/tools/resattr/resattr
fsShellCommand=$sourceDir/objects/${subpath}/tests/add-ons/kernel/file_systems/fs_shell/fs_shell_command
bfsShell=$sourceDir/objects/${subpath}/tests/add-ons/kernel/file_systems/bfs/bfs_shell/bfs_shell

export TARGET_PLATFORM=haiku

BEOS_BIN="touch sync ln listarea listattr listsem listport \
	true false ls df mount unmount cp mv ps sh mkdir sleep \
	grep cat less clockconfig du rm date find locate xargs \
	isvolume shutdown safemode sysinfo kill diff cmp more \
	renice rmattr addattr listdev pwd chmod chown chgrp dd \
	tee md5sum catattr query lsindex mkindex roster listimage \
	quit open translate setvolume waitfor uname iroster keymap \
	strace rmdir error ifconfig ping traceroute arp pppconfig \
	ppp_up gdb tail head zip unzip mountvolume top finddir groups \
	id env basename factor tty dstcheck comm modifiers version \
	csplit cut beep screen_blanker play eject uptime"

BEOS_APPS="MiniTerminal Terminal Expander People ShowImage Clock Pulse Playground \
	SoundRecorder BitmapDrawing Magnify DiskProbe AboutHaiku StyledEdit \
	CDPlayer"
BEOS_PREFERENCES="Backgrounds DataTranslations FileTypes Fonts Media Menu Mouse \
	Keyboard Keymap Screen ScrollBar Sounds Time VirtualMemory Workspaces ScreenSaver"

BEOS_SYSTEM_LIB="libbe.so libstdc++.r4.so libnet.so libmedia.so libtracker.so \
	libtranslation.so libbind.so libnetapi.so libsocket.so libdebug.so \
	libtextencoding.so libz.so libfreetype.so libpng.so libappserver.so \
	libdevice.so libgame.so libscreensaver.so"

BEOS_SYSTEM_SERVERS="registrar debug_server syslog_daemon media_server \
	media_addon_server input_server app_server fake_app_server"

BEOS_NETWORK_CORE="core"
BEOS_NETWORK_INTERFACES="ethernet loopback ppp"
BEOS_NETWORK_PPP="ipcp modem pap pppoe"
BEOS_NETWORK_PROTOCOLS="icmp ipv4 raw route tcp udp"

BEOS_ADD_ONS_ACCELERANTS="radeon.accelerant nv.accelerant mga.accelerant \
	nm.accelerant vesa.accelerant"
BEOS_ADD_ONS_TRANSLATORS="STXTTranslator RTF-Translator PNGTranslator \
	JPEGTranslator GIFTranslator"
BEOS_ADD_ONS_MEDIA="mixer.media_addon legacy.media_addon multi_audio.media_addon"
BEOS_ADD_ONS_MEDIA_PLUGINS="aiff_reader avcodec mp3_decoder musepack raw_decoder \
	wav_reader mov_reader au_reader avi_reader mp3_reader ogg speex vorbis \
	matroska" # ac3_decoder theora
BEOS_ADD_ONS_INPUT="<input>keyboard <input>mouse canna screen_saver"
BEOS_ADD_ONS_SCREENSAVERS="Haiku"
BEOS_ADD_ONS_DRIVERS_MISC="keyboard scsi_dsk scsi_cd dprintf null zero random \
	ps2_hid <driver>tty console <driver>config"
BEOS_ADD_ONS_DRIVERS_GRAPHICS="radeon.driver nv.driver nm.driver mga.driver vesa"
BEOS_ADD_ONS_DRIVERS_AUDIO="ich_ac97 auvia emuxki"
BEOS_ADD_ONS_DRIVERS_NET="ipro1000 rtl8139 rtl8169 sis900 \
	via-rhine wb840 net_stack_driver"
	# bcm440x bcm570x (only available with GPLd add-ons)
BEOS_ADD_ONS_BUS_MANAGERS="pci isa ide scsi config_manager"
BEOS_ADD_ONS_KERNEL_MISC="ide_isa generic_ide_pci block_io fast_log ide_adapter \
	locked_pool scsi_periph intel keyboard vga_text"
BEOS_ADD_ONS_FILESYSTEMS="bfs"
BEOS_DATA_FILES="timezone_files keymap_files"

echo "Building executables and libraries..."

jam -j$(sysinfo -cpu | head -c 2) -q rc resattr kernel boot_loader \
	rld.so kernel_fortune consoled \<bin\>route \
	Tracker Deskbar \
	$BEOS_BIN \
	$BEOS_APPS \
	$BEOS_PREFERENCES \
	$BEOS_SYSTEM_LIB \
	$BEOS_SYSTEM_SERVERS \
	$BEOS_NETWORK_CORE \
	$BEOS_NETWORK_INTERFACES \
	$BEOS_NETWORK_PPP \
	$BEOS_NETWORK_PROTOCOLS \
	$BEOS_ADD_ONS_DRIVERS_MISC \
	$BEOS_ADD_ONS_DRIVERS_AUDIO \
	$BEOS_ADD_ONS_DRIVERS_GRAPHICS \
	$BEOS_ADD_ONS_DRIVERS_NET \
	$BEOS_ADD_ONS_BUS_MANAGERS \
	$BEOS_ADD_ONS_KERNEL_MISC \
	$BEOS_ADD_ONS_FILESYSTEMS \
	$BEOS_ADD_ONS_ACCELERANTS \
	$BEOS_ADD_ONS_TRANSLATORS \
	$BEOS_ADD_ONS_MEDIA \
	$BEOS_ADD_ONS_MEDIA_PLUGINS \
	$BEOS_ADD_ONS_INPUT \
	$BEOS_ADD_ONS_SCREENSAVERS \
	$BEOS_DATA_FILES \
	bfs_shell fs_shell_command \
	|| { echo "*** Build failed!" 1>&2 ; exit 1; }

# map the shell commands
if [ $alreadyMounted ]; then
	sPrefix=
	cd=cd
	scd=:
	cp="copyattr -d"
	ln=ln
	mkdir=mkdir
	rm=rm
	mkindex=mkindex
else
	sPrefix=:
	cd="$fsShellCommand cd"
	scd="$fsShellCommand cd"
	cp="$fsShellCommand cp"
	ln="$fsShellCommand ln"
	mkdir="$fsShellCommand mkdir"
	rm="$fsShellCommand rm"
	mkindex="$fsShellCommand mkindex"
fi

# create the image and the directory structure

if [ ! $alreadyMounted ]; then
	echo
	echo "Creating image..."
	dd if=/dev/zero of=$imagePath bs=1M count=$imageSize
	mkbfs $imagePath Haiku >/dev/null
	targetDir=/myfs
	$bfsShell -n $imagePath > /dev/null &
	sleep 1
fi

$cd $targetDir

echo
echo "Creating directory structure..."

$mkindex BEOS:APP_SIG
	# needed to launch apps via signature

$mkdir -p beos/apps
$mkdir -p beos/bin
$mkdir -p beos/etc
$mkdir -p beos/preferences
$mkdir -p beos/system/add-ons/kernel/boot
$mkdir -p beos/system/add-ons/kernel/bus_managers
$mkdir -p beos/system/add-ons/kernel/busses/ide
$mkdir -p beos/system/add-ons/kernel/drivers/bin
$mkdir -p beos/system/add-ons/kernel/drivers/dev/audio/multi
$mkdir -p beos/system/add-ons/kernel/drivers/dev/disk/scsi
$mkdir -p beos/system/add-ons/kernel/drivers/dev/graphics
$mkdir -p beos/system/add-ons/kernel/drivers/dev/input
$mkdir -p beos/system/add-ons/kernel/drivers/dev/misc
$mkdir -p beos/system/add-ons/kernel/drivers/dev/net
$mkdir -p beos/system/add-ons/kernel/file_systems
$mkdir -p beos/system/add-ons/kernel/console
$mkdir -p beos/system/add-ons/kernel/generic
$mkdir -p beos/system/add-ons/kernel/network
$mkdir -p beos/system/add-ons/kernel/network/interfaces
$mkdir -p beos/system/add-ons/kernel/network/ppp
$mkdir -p beos/system/add-ons/kernel/network/protocols
$mkdir -p beos/system/add-ons/kernel/partitioning_systems
$mkdir -p beos/system/add-ons/Translators
$mkdir -p beos/system/add-ons/accelerants
$mkdir -p beos/system/add-ons/media/plugins
$mkdir -p beos/system/add-ons/input_server/devices
$mkdir -p beos/system/add-ons/input_server/filters
$mkdir -p beos/system/add-ons/input_server/methods
$mkdir -p beos/system/add-ons/Screen\ Savers
$mkdir -p beos/system/boot
$mkdir -p beos/system/lib
$mkdir -p beos/system/servers
$mkdir -p home/config
$mkdir -p home/config/settings/kernel/drivers
$mkdir -p home/Desktop
$mkdir -p var/tmp
$mkdir -p var/log

# modules

echo "Installing kernel modules..."

cd $sourceDir/distro/${subpath}/beos/system/add-ons/kernel
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/system/add-ons/kernel

for f in $BEOS_ADD_ONS_BUS_MANAGERS; do
	$cp ${sPrefix}bus_managers/$f $targetDir/beos/system/add-ons/kernel/bus_managers/$f
done

for f in busses/ide/ide_isa file_systems/bfs generic/block_io generic/fast_log \
		generic/ide_adapter generic/locked_pool generic/scsi_periph \
		partitioning_systems/intel console/vga_text busses/ide/generic_ide_pci; do
	$cp ${sPrefix}$f $targetDir/beos/system/add-ons/kernel/$f
done

for f in drivers/dev/disk/scsi/scsi_dsk drivers/dev/disk/scsi/scsi_cd; do
	name=drivers/bin/$(basename $f)
	$cp ${sPrefix}$name $targetDir/beos/system/add-ons/kernel/$name
done

cd $sourceDir/objects/${subpath}/add-ons/kernel
$scd ${sPrefix}$sourceDir/objects/${subpath}/add-ons/kernel

# drivers
for f in drivers/arch/${arch}/keyboard/keyboard; do
	$cp ${sPrefix}$f $targetDir/beos/system/add-ons/kernel/drivers/bin
done

cd $sourceDir/distro/${subpath}/beos/system/add-ons/kernel/drivers/bin
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/system/add-ons/kernel/drivers/bin
for f in $BEOS_ADD_ONS_DRIVERS_AUDIO; do
	$cp ${sPrefix}$f $targetDir/beos/system/add-ons/kernel/drivers/bin
done
for f in $BEOS_ADD_ONS_DRIVERS_GRAPHICS; do
	$cp ${sPrefix}$f $targetDir/beos/system/add-ons/kernel/drivers/bin
done
for f in $BEOS_ADD_ONS_DRIVERS_NET; do
	$cp ${sPrefix}$f $targetDir/beos/system/add-ons/kernel/drivers/bin
done
for f in ps2_hid; do
	$cp ${sPrefix}$f $targetDir/beos/system/add-ons/kernel/drivers/bin
done

cd $sourceDir/distro/${subpath}/beos/system/add-ons/kernel/drivers/dev
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/system/add-ons/kernel/drivers/dev
for f in misc/config console tty random dprintf null zero; do
	$cp ${sPrefix}$f $targetDir/beos/system/add-ons/kernel/drivers/bin
done


# kernel
echo "Installing kernel..."
$cp ${sPrefix}$sourceDir/objects/${subpath}/system/kernel_${arch} $targetDir/beos/system/


# libs
echo "Installing libraries..."
cd $sourceDir/distro/${subpath}/beos/system/lib
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/system/lib
for f in $BEOS_SYSTEM_LIB; do
	$cp ${sPrefix}$f $targetDir/beos/system/lib/
done

cd $sourceDir/objects/${subpath}/system/
$scd ${sPrefix}$sourceDir/objects/${subpath}/system/
$cp ${sPrefix}runtime_loader/rld.so ${sPrefix}libroot.so $targetDir/beos/system/lib/


# servers
echo "Installing servers..."
cd $sourceDir/distro/${subpath}/beos/system/servers
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/system/servers
for f in $BEOS_SYSTEM_SERVERS; do
	$cp ${sPrefix}$f $targetDir/beos/system/servers/
done


# apps
echo "Installing apps..."

cd $sourceDir/objects/${subpath}/tests/kernel/boot_floppy
$scd ${sPrefix}$sourceDir/objects/${subpath}/tests/kernel/boot_floppy
for f in fortune; do
	$cp ${sPrefix}kernel_$f $targetDir/beos/bin/$f
done

cd $sourceDir/distro/${subpath}/beos/bin
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/bin
for f in ../apps/consoled $BEOS_BIN route; do
	$cp ${sPrefix}$f $targetDir/beos/bin/
done

cd $sourceDir/distro/${subpath}/beos/apps
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/apps
for f in $BEOS_APPS; do
	$cp ${sPrefix}$f $targetDir/beos/apps/
done


cd $sourceDir/distro/${subpath}/beos/preferences
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/preferences
for f in $BEOS_PREFERENCES; do
	$cp ${sPrefix}$f $targetDir/beos/preferences/
done

cd $sourceDir/distro/${subpath}/beos/system
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/system
for f in Deskbar Tracker; do
	$cp ${sPrefix}$f $targetDir/beos/system/
done

# scripts and data files
echo "Installing scripts and data files..."
cd $sourceDir
$scd ${sPrefix}$sourceDir
$cp ${sPrefix}data/system/boot/Bootscript \
	${sPrefix}data/system/boot/SetupEnvironment \
	${sPrefix}data/system/boot/Netscript $targetDir/beos/system/boot/
$cp ${sPrefix}data/etc/profile ${sPrefix}data/etc/termcap $targetDir/beos/etc/
$cp -r ${sPrefix}data/etc/fonts $targetDir/beos/etc/
#$cp -r ${sPrefix}data/etc/KanBe $targetDir/beos/etc/
if [ -e /boot/beos/etc/fonts/ttfonts/Swiss721.ttf ]; then
	$cp ${sPrefix}/boot/beos/etc/fonts/ttfonts/Courier10Pitch.ttf $targetDir/beos/etc/fonts/ttfonts/
	$cp ${sPrefix}/boot/beos/etc/fonts/ttfonts/Swiss721_Bold.ttf $targetDir/beos/etc/fonts/ttfonts/
	$cp ${sPrefix}/boot/beos/etc/fonts/ttfonts/Swiss721.ttf $targetDir/beos/etc/fonts/ttfonts/
fi
#$cp -r ${sPrefix}data/etc/KanBe $targetDir/beos/etc/
$cp ${sPrefix}src/tests/kernel/boot_floppy/fortune/fortunes $targetDir/beos/etc/
$cp -r ${sPrefix}distro/${subpath}/beos/etc $targetDir/beos/

$cp ${sPrefix}data/settings/kernel/drivers/kernel \
	$targetDir/home/config/settings/kernel/drivers/
if [ -e data/settings/kernel/drivers/vesa ]; then
	$cp ${sPrefix}data/settings/kernel/drivers/vesa \
		$targetDir/home/config/settings/kernel/drivers/
fi

$ln -sf /boot/beos/etc/timezones/Europe/Paris \
	$targetDir/home/config/settings/timezone
$cp ${sPrefix}distro/${subpath}/beos/etc/Keymap/US-International \
	$targetDir/home/config/settings/Key_map


# boot loader
echo "Installing boot loader..."
cd $sourceDir/objects/${subpath}/system/
rm -f zbeos
objcopy -O binary boot_loader zbeos
$cd $targetDir/beos/system
$cp ${sPrefix}$sourceDir/objects/${subpath}/system/zbeos .


# boot module links
echo "Creating boot module links..."
$cd $targetDir/beos/system/add-ons/kernel/boot
for f in bus_managers/config_manager bus_managers/pci bus_managers/isa bus_managers/ide \
		bus_managers/scsi busses/ide/ide_isa file_systems/bfs generic/block_io \
		generic/fast_log generic/ide_adapter generic/locked_pool generic/scsi_periph \
		partitioning_systems/intel busses/ide/generic_ide_pci; do
	$ln -fs /boot/beos/system/add-ons/kernel/$f $(basename $f)
done
for f in drivers/dev/disk/scsi/scsi_dsk drivers/dev/disk/scsi/scsi_cd; do
	$ln -fs /boot/beos/system/add-ons/kernel/drivers/bin/$(basename $f) .
done

# driver links
echo "Creating driver links..."
$cd $targetDir/beos/system/add-ons/kernel
for f in drivers/dev/dprintf drivers/dev/keyboard drivers/dev/null \
		drivers/dev/misc/config drivers/dev/tty drivers/dev/zero \
		drivers/dev/disk/scsi/scsi_dsk drivers/dev/disk/scsi/scsi_cd \
		drivers/dev/misc/config drivers/dev/input/ps2_hid \
		drivers/dev/console drivers/dev/graphics/radeon.driver \
		drivers/dev/graphics/nv.driver drivers/dev/graphics/mga.driver \
		drivers/dev/graphics/nm.driver drivers/dev/net/ipro1000 \
		drivers/dev/net/rtl8139 drivers/dev/net/rtl8169 \
		drivers/dev/net/sis900 drivers/dev/net/via-rhine \
		drivers/dev/net/wb840 drivers/dev/net/net_stack_driver \
		drivers/dev/graphics/vesa drivers/dev/random \
		drivers/dev/audio/multi/ich_ac97 drivers/dev/audio/multi/auvia \
		drivers/dev/audio/multi/emuxki; do
	relName=$(echo $f | sed -e s@drivers/dev/@@)
	linkName=bin/$(basename $f)
	while [ $relName != . ]; do
		relName=$(dirname $relName)
		linkName=../$linkName
	done
	$ln -fs $linkName $(dirname $f)
done


# add-ons
echo "Copying add-ons..."

cd $sourceDir/distro/${subpath}/beos/system/add-ons/accelerants
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/system/add-ons/accelerants
for f in $BEOS_ADD_ONS_ACCELERANTS; do
	$cp ${sPrefix}$f $targetDir/beos/system/add-ons/accelerants/
done

cd $sourceDir/distro/${subpath}/beos/system/add-ons/Translators
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/system/add-ons/Translators
for f in $BEOS_ADD_ONS_TRANSLATORS; do
	$cp ${sPrefix}$f $targetDir/beos/system/add-ons/Translators/
done

cd $sourceDir/distro/${subpath}/beos/system/add-ons/media
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/system/add-ons/media
for f in $BEOS_ADD_ONS_MEDIA; do
	$cp ${sPrefix}$f $targetDir/beos/system/add-ons/media/
done

cd $sourceDir/distro/${subpath}/beos/system/add-ons/media/plugins
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/system/add-ons/media/plugins
for f in $BEOS_ADD_ONS_MEDIA_PLUGINS; do
	$cp ${sPrefix}$f $targetDir/beos/system/add-ons/media/plugins/
done

cd $sourceDir/distro/${subpath}/beos/system/add-ons/input_server/
$cd ${sPrefix}$sourceDir/distro/${subpath}/beos/system/add-ons/input_server/
for f in devices/keyboard devices/mouse filters/screen_saver methods/canna; do
	$cp ${sPrefix}$f $targetDir/beos/system/add-ons/input_server/$(dirname $f)/
done

# network add-ons
cd $sourceDir/distro/${subpath}/beos/system/add-ons/kernel/obos_network
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/system/add-ons/kernel/obos_network
for f in $BEOS_NETWORK_CORE; do
	$cp ${sPrefix}$f $targetDir/beos/system/add-ons/kernel/network
done

cd $sourceDir/distro/${subpath}/beos/system/add-ons/kernel/obos_network/interfaces
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/system/add-ons/kernel/obos_network/interfaces
for f in $BEOS_NETWORK_INTERFACES; do
	$cp ${sPrefix}$f $targetDir/beos/system/add-ons/kernel/network/interfaces
done

cd $sourceDir/distro/${subpath}/beos/system/add-ons/kernel/obos_network/ppp
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/system/add-ons/kernel/obos_network/ppp
for f in $BEOS_NETWORK_PPP; do
	$cp ${sPrefix}$f $targetDir/beos/system/add-ons/kernel/network/ppp
done

cd $sourceDir/distro/${subpath}/beos/system/add-ons/kernel/obos_network/protocols
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/system/add-ons/kernel/obos_network/protocols
for f in $BEOS_NETWORK_PROTOCOLS; do
	$cp ${sPrefix}$f $targetDir/beos/system/add-ons/kernel/network/protocols
done

cd $sourceDir/distro/${subpath}/beos/system/add-ons/Screen\ Savers
$scd ${sPrefix}$sourceDir/distro/${subpath}/beos/system/add-ons/Screen\ Savers
for f in $BEOS_ADD_ONS_SCREENSAVERS; do
	$cp ${sPrefix}$f $targetDir/beos/system/add-ons/Screen\ Savers
done

# install MIME database
mimeDBSource=$sourceDir/src/data/beos_mime
mimeDBDest=$targetDir/home/config/settings/beos_mime

echo "Deleting old MIME database..."

$rm -rf $mimeDBDest
$mkdir -p $mimeDBDest

echo "Installing MIME database..."

for inSuperFile in $mimeDBSource/*.super; do
	superType=$(basename $inSuperFile .super)
	outSuperDir=$mimeDBDest/$superType

	# compile rdef to rsrc file and the rsrc file to attributes
	tmpFile=/tmp/mimedb$$.rsrc
	tmpFile2=/tmp/mimedb$$.mime
	$rc -o $tmpFile $inSuperFile
	mkdir -p $tmpFile2
	$resattr -O -o $tmpFile2 $tmpFile
	$cp -r ${sPrefix}$tmpFile2 $outSuperDir
	rm -rf $tmpFile $tmpFile2

	# iterate through the sub types
	for inSubFile in $mimeDBSource/$superType/*; do
		# check, if the type exists
		if test -f $inSubFile && grep META:TYPE $inSubFile &> /dev/null; then
			subType=$(basename $inSubFile)
			outSubFile=$outSuperDir/$subType

			# compile rdef to rsrc file and the rsrc file to attributes
			tmpFile=/tmp/mimedb$$.rsrc
			tmpFile2=/tmp/mimedb$$.mime
			$rc -o $tmpFile $inSubFile
			$resattr -O -o $tmpFile2 $tmpFile
			$cp ${sPrefix}$tmpFile2 $outSubFile
			rm -f $tmpFile $tmpFile2
		fi
	done
done


# source the include file, if any
if [ "$includeFile" != "" ]; then
	. $includeFile
fi

cd $previousDir
sync

if [ ! $alreadyMounted ]; then
	echo Unmounting ...
	$fsShellCommand sync
	$fsShellCommand quit
fi

if [ $archive ]; then
	zip haiku.zip $imagePath
fi