#!/bin/sh

. common.sh

# Clean up
set +e
umount ${ZIPSPEAK_DOSFS_IMAGE} 2>/dev/null
set -e
rm -rf ${ZIPSPEAK_BUILD_ROOT}

# Set up the build directory
mkdir ${ZIPSPEAK_BUILD_ROOT}

# Initialize the DOS file system
dd if=/dev/zero of=${ZIPSPEAK_DOSFS_IMAGE} bs=1024k count=128
mkdosfs ${ZIPSPEAK_DOSFS_IMAGE}
mkdir ${ZIPSPEAK_DOSFS_ROOT}
mount -o loop -t msdos ${ZIPSPEAK_DOSFS_IMAGE} ${ZIPSPEAK_DOSFS_ROOT}

# Unpack ZipSlack
cd ${ZIPSPEAK_DOSFS_ROOT}
unzip ${ZIPSPEAK_SOURCE_DIR}/zipslack.zip >/dev/null

# Remount DOS file system as UMSDOS
cd ${ZIPSPEAK_ROOT}
umount ${ZIPSPEAK_DOSFS_IMAGE}
mount -o loop -t umsdos ${ZIPSPEAK_DOSFS_IMAGE} ${ZIPSPEAK_DOSFS_ROOT}

# Unpack the kernel
cd ${ZIPSPEAK_BUILD_ROOT}
tar zxf ${ZIPSPEAK_SOURCE_DIR}/lx${SLACK_KERNEL_VERSION}.tgz
tar zxf ${ZIPSPEAK_SOURCE_DIR}/linuxinc.tgz
mv usr/src/linux-${KERNEL_VERSION} ${KERNEL_ROOT}
rm -rf usr install

# Unpack Speakup
tar zxf ${ZIPSPEAK_SOURCE_DIR}/speakup-${SPEAKUP_VERSION}.tar.gz
mv speakup-0.09 speakup-0.09a

# Patch the kernel
cd ${KERNEL_ROOT}
set +e
patch -p1 -s < ../speakup-${SPEAKUP_VERSION}/speakup-${SPEAKUP_VERSION}-v2.2-patch
set -e
zcat ${ZIPSPEAK_SOURCE_DIR}/2.2.16-ac-errata.diff.gz | patch -p1 -s

# Make other directories that the build process needs
mkdir -p ${ZIPSPEAK_DIST_DIR} ${ZIPSPEAK_PACKAGE_DIR}
