-# Create ISO image
-echo "* Creating ISO image"
-iso="$PLC_NAME-BootCD-$BOOTCD_VERSION.iso"
-mkisofs -o "$iso" \
- -R -allow-leading-dots -J -r \
- -b isolinux.bin -c boot.cat \
- -no-emul-boot -boot-load-size 4 -boot-info-table \
- $isofs
-
-# Create USB image
-echo "* Creating USB image"
-usb="$PLC_NAME-BootCD-$BOOTCD_VERSION.usb"
-
-# Leave 1 MB of free space on the VFAT filesystem
-mkfs.vfat -C "$usb" $(($(du -sk $isofs | awk '{ print $1; }') + 1024))
-
-# Mount it
-tmp=$(mktemp -d /tmp/bootcd.XXXXXX)
-mount -o loop "$usb" $tmp
-trap "umount $tmp; rm -rf $tmp" ERR INT
-
-# Populate it
-echo "* Populating USB image"
-(cd $isofs && find . | cpio -p -d -u $tmp/)
-
-# Use syslinux instead of isolinux to make the image bootable
-mv $tmp/isolinux.cfg $tmp/syslinux.cfg
-umount $tmp
-rmdir $tmp
-trap - ERR INT
-
-echo "* Making USB image bootable"
-$srcdir/syslinux/unix/syslinux "$usb"
-
-exit 0
+echo "done"
+
+# hand over to pl_sysinit
+echo "pl_rsysinit: handing over to pl_sysinit"
+/etc/init.d/pl_sysinit
+EOF
+ chmod +x etc/rc.d/init.d/pl_rsysinit
+
+ popd
+
+ # create the cramfs image
+ echo "* Creating cramfs image"
+ mkfs.cramfs $tmp/ ${BUILDTMP}/cramfs.img
+ cramfs_size=$(($(du -sk ${BUILDTMP}/cramfs.img | awk '{ print $1; }') + 1))
+ rm -rf $tmp
+ pop_cleanup
+}
+
+#################### Create ISO CRAMFS image
+function build_iso_cramfs() {
+ local iso="$1" ; shift
+ local custom="$1"
+
+ prepare_cramfs "$custom"
+ echo "* Creating ISO CRAMFS-based image"
+
+ local tmp="${BUILDTMP}/cramfs-iso"
+ mkdir -p "$tmp"
+ push_cleanup rm -rf $tmp
+ (cd $ISOFS && find . | grep -v "\.img$" | cpio -p -d -u $tmp/)
+ cat >$tmp/isolinux.cfg <<EOF
+${console_serial_line}
+DEFAULT kernel
+APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${kernel_args}
+DISPLAY pl_version
+PROMPT 0
+TIMEOUT 40
+EOF
+
+ cp ${BUILDTMP}/cramfs.img $tmp
+ mkisofs -o "$iso" \
+ $MKISOFS_OPTS \
+ $tmp
+
+ rm -fr "$tmp"
+ pop_cleanup
+}
+
+#################### Create USB CRAMFS based image
+function build_usb_cramfs() {
+ local usb="$1" ; shift
+ local custom="$1"
+
+ prepare_cramfs "$custom"
+ echo "* Creating USB CRAMFS based image"
+
+ let vfat_size=${cramfs_size}+$FREE_SPACE
+
+ # Make VFAT filesystem for USB
+ mkfs.vfat -C "$usb" $vfat_size
+
+ # Populate it
+ echo "* Populating USB with overlay images and cramfs"
+ mcopy -bsQ -i "$usb" $ISOFS/kernel $ISOFS/pl_version ::/
+ mcopy -bsQ -i "$usb" ${BUILDTMP}/cramfs.img ::/
+
+ # Use syslinux instead of isolinux to make the image bootable
+ tmp="${BUILDTMP}/syslinux.cfg"
+ cat >$tmp <<EOF
+${console_serial_line}
+DEFAULT kernel
+APPEND ramdisk_size=$cramfs_size initrd=cramfs.img root=/dev/ram0 ro ${kernel_args}
+DISPLAY pl_version
+PROMPT 0
+TIMEOUT 40
+EOF
+
+ mcopy -bsQ -i "$usb" "$tmp" ::/syslinux.cfg
+ rm -f "$tmp"
+
+ echo "* Making USB CRAMFS based image bootable"
+ syslinux "$usb"
+}
+
+#################### map on all types provided on the command-line and invoke one of the above functions
+function build_types () {
+
+ [ -z "$OUTPUT_BASE" ] && OUTPUT_BASE="$PLC_NAME-BootCD-$BOOTCD_VERSION"
+
+ # alter output filename to reflect serial settings
+ if [ -n "$IS_SERIAL" ] ; then
+ if [ "$CONSOLE_INFO" == "$SERIAL_CONSOLE" ] ; then
+ serial="-serial"
+ else
+ serial="-serial-$(echo $CONSOLE_INFO | sed -e 's,:,,g')"
+ fi
+ else
+ serial=""
+ fi
+
+ function type_to_name() {
+ echo $1 | sed '
+ s/usb$/.usb/;
+ s/usb_partition$/-partition.usb/;
+ s/iso$/.iso/;
+ s/usb_cramfs$/-cramfs.usb/;
+ s/iso_cramfs$/-cramfs.iso/;
+ '
+ }
+
+ for t in $TYPES; do
+ arg=$t
+
+ tname=`type_to_name $t`
+ # if -o is specified (as it has no default)
+ if [ -n "$OUTPUT_NAME" ] ; then
+ output=$OUTPUT_NAME
+ else
+ output="${OUTPUT_BASE}${serial}${tname}"
+ fi
+
+ echo "*** Dealing with type=$arg"
+ echo '*' build_$t "$output" "$CUSTOM_DIR"
+ [ -n "$DRY_RUN" ] || build_$t "$output" "$CUSTOM_DIR"
+ done
+}
+
+####################
+function main () {
+
+ parse_command_line "$@"
+
+ init_and_check
+
+ echo "* Building images for $FULL_VERSION_STRING"
+ # Do not tolerate errors
+ set -e
+ trap "do_cleanup" ERR INT EXIT
+
+ init_serial $CONSOLE_INFO
+ build_overlay
+ build_types
+
+ exit 0
+}
+
+####################
+main "$@"