#!/bin/bash

# This script creates a directory containing all files required for a installation on a device
# For now, is only for efikamx devices but easily could be adapted for other devices (and/or ABIs)
# To do that, just change the variables you need and remove/add 

VERSION="2.7.1-rc1"
DEVICE="efikamx"
KERNEL_VERSION_STRING="2.6.31.14.27-efikamx_20111026"
DESTDIR="crux-arm-$VERSION-$DEVICE"

[ -d $DESTDIR ] && rm -r $DESTDIR
mkdir -p $DESTDIR

# bootloader files
wget -c -O $DESTDIR/boot.scr http://crux-arm.nu/files/devices/$DEVICE/boot-ram.scr
wget -c -O $DESTDIR/uImage http://crux-arm.nu/files/devices/$DEVICE/uImage-$KERNEL_VERSION_STRING
wget -c -O $DESTDIR/uInitrd http://crux-arm.nu/files/devices/$DEVICE/uInitrd-$VERSION

# kernel files
mkdir -p $DESTDIR/crux/kernel
wget -c -P $DESTDIR/crux/kernel http://crux-arm.nu/files/distfiles/$DEVICE/linux-$KERNEL_VERSION_STRING.tar.bz2
wget -c -P $DESTDIR/crux/kernel http://crux-arm.nu/files/devices/$DEVICE/modules-$KERNEL_VERSION_STRING.tar.bz2
wget -c -P $DESTDIR/crux/kernel http://crux-arm.nu/files/devices/$DEVICE/linux-$KERNEL_VERSION_STRING.config

# download released packages
mkdir -p $DESTDIR/crux/core
PACKAGES="$(curl -s http://crux-arm.nu/pkg/${VERSION%%-*}/core/ | grep '.pkg.tar.' | cut -d'=' -f4 | cut -d'"' -f2)"
for pkg in ${PACKAGES[@]}; do
	wget -c -P $DESTDIR/crux/core "http://crux-arm.nu/pkg/${VERSION%%-*}/core/$pkg"
done

# this stuff is important in order to get the setup working fine
echo "$VERSION-$(date +'%Y%m%d')" > $DESTDIR/crux-media
wget -O $DESTDIR/crux/setup.dependencies "http://crux.nu/gitweb/?p=system/iso.git;a=blob_plain;f=iso/setup.dependencies"

# optional but is a nice to have per device and can differ between them (due to bootloader, configs, etc.)
mkdir -p $DESTDIR/$DEVICE
wget -c -P $DESTDIR/$DEVICE http://crux-arm.nu/files/devices/$DEVICE/securetty
wget -c -P $DESTDIR/$DEVICE http://crux-arm.nu/files/devices/$DEVICE/inittab
wget -c -P $DESTDIR/$DEVICE http://crux-arm.nu/files/devices/$DEVICE/10-imx.rules
wget -c -P $DESTDIR/$DEVICE http://crux-arm.nu/files/devices/$DEVICE/xorg.conf
wget -c -P $DESTDIR/$DEVICE http://crux-arm.nu/files/devices/$DEVICE/rc.modules
wget -c -P $DESTDIR/$DEVICE http://crux-arm.nu/files/devices/$DEVICE/fstab-sda
wget -c -P $DESTDIR/$DEVICE http://crux-arm.nu/files/devices/$DEVICE/fstab-mmcblk
wget -c -P $DESTDIR/$DEVICE http://crux-arm.nu/files/devices/$DEVICE/boot-ram.scr
wget -c -P $DESTDIR/$DEVICE http://crux-arm.nu/files/devices/$DEVICE/boot-ram.script
wget -c -P $DESTDIR/$DEVICE http://crux-arm.nu/files/devices/$DEVICE/boot-sda.scr
wget -c -P $DESTDIR/$DEVICE http://crux-arm.nu/files/devices/$DEVICE/boot-sda.script
wget -c -P $DESTDIR/$DEVICE http://crux-arm.nu/files/devices/$DEVICE/boot-mmcblk.scr
wget -c -P $DESTDIR/$DEVICE http://crux-arm.nu/files/devices/$DEVICE/boot-mmcblk.script
wget -c -P $DESTDIR/$DEVICE http://crux-arm.nu/files/devices/$DEVICE/prt-get.conf
wget -O $DESTDIR/$DEVICE/$DEVICE.rsync "http://crux-arm.nu/portdb/?getup=$DEVICE&type=rsync"

# End of file