#!/bin/bash

# check variables, like all scripts herein do
WRS_SCRIPT_NAME=$(basename $0)

if [ -z "$WRS_BASE_DIR" ]; then
    echo "$0: Plesae set WRS_BASE_DIR" >& 2
    exit 1
fi
. ${WRS_BASE_DIR}/scripts/wrs_functions

wrs_check_vars WRS_OUTPUT_DIR WRS_DOWNLOAD_DIR CROSS_COMPILE

wrs_echo "--- Wrapping filesystem"

# This builds an initramfs thing. The cpio archive is made from
# a real directory, and we have no device table support. So let's
# extract a tar file with devices, instead.

DEVTAR="$WRS_BASE_DIR/../userspace/devices.tar.gz"

TMPFS=$(mktemp -d /tmp/rootfs.XXXXXX)
chmod a+rx $TMPFS

rootfs_vanilla="$WRS_OUTPUT_DIR/build/buildroot-$BRVER/output/target"
rootfs_override="$WRS_BASE_DIR/../userspace/rootfs_override"

TMPSCRIPT=$(mktemp /tmp/rootfs-script.XXXXXX)
ROOTFS_IMAGE_TGZ="$WRS_OUTPUT_DIR/images/wrs-image.tar.gz"
ROOTFS_USR_TGZ="$WRS_OUTPUT_DIR/images/wrs-usr.tar.gz"
ROOTFS_INITRAMFS="$WRS_OUTPUT_DIR/images/wrs-initramfs.gz"

cat > $TMPSCRIPT << EOF
mkdir -p $TMPFS/wr
cp -r $rootfs_vanilla/* $TMPFS
cp -r $WRS_OUTPUT_DIR/images/wr/* $TMPFS/wr
cp -r $WRS_OUTPUT_DIR/images/lib/* $TMPFS/lib
rm -f $TMPFS/etc/init.d/*
rm -f $TMPFS/THIS_IS_NOT_YOUR_ROOT_FILESYSTEM
# remove symlink from the buildroot
rm -rf $TMPFS/etc/dropbear
mkdir -p $TMPFS/etc/dropbear; chown -R root:root $TMPFS/etc/dropbear

cp -r $rootfs_override/* $TMPFS
# remove leftovers from on-going edits in rootfs_override
find $TMPFS -name '*~' | xargs rm -f

rm -rf $TMPFS/dev
(cd $TMPFS && tar xzf $DEVTAR)
(cd $TMPFS && ln -fs sbin/init .)

mkdir -p $TMPFS/root/.ssh
#cat $HOME/.ssh/id_?sa.pub >> $TMPFS/root/.ssh/authorized_keys
if [ -f $WRS_BASE_DIR/authorized_keys ]; then
    cat $WRS_BASE_DIR/authorized_keys >> $TMPFS/root/.ssh/authorized_keys
    chmod 600 $TMPFS/root/.ssh/authorized_keys
fi
chmod g-w $TMPFS/root $TMPFS/root/.ssh
chown -R root:root $TMPFS/root
chmod a+rx $TMPFS

##### now move stuff to usr (we need usr to be in flash, / remains initramfs)
# remove needless stuff
rm -rf $TMPFS/home $TMPFS/opt
sed -i '/^default/ d' $TMPFS/etc/passwd
# move /wr and /var to /usr/wr and /usr/var
mv $TMPFS/wr  $TMPFS/usr; ln -s usr/wr  $TMPFS
mv $TMPFS/var $TMPFS/usr; ln -s usr/var $TMPFS
# var/run links to ../tmp, so create /usr/tmp
ln -s ../tmp $TMPFS/usr/tmp
# var/run links to ../run, overwrite it to point to ../tmp
ln -sf ../tmp $TMPFS/var/run
# mv ubi commands to /sbin: they are needed for boot time
mv $TMPFS/usr/sbin/ubi* $TMPFS/sbin

#set permisions for monitrc
chmod 600 $TMPFS/etc/monitrc

# copy /etc to /usr/etc, where it can be edited (boot sequence copies back)
cp -a $TMPFS/etc $TMPFS/usr/etc

# copy non-blocking tee to /bin where it is used during boot
cp $TMPFS/wr/bin/nbtee $TMPFS/bin

(cd "$TMPFS" && tar cz . > $ROOTFS_IMAGE_TGZ)
(cd "$TMPFS/usr" && tar cz . > $ROOTFS_USR_TGZ)
# include /usr but nothing within usr
(cd "$TMPFS" &&  find . | grep -v '^./usr.' | \
      cpio -o -H newc | gzip > $ROOTFS_INITRAMFS)

EOF

fakeroot bash $WRS_SH_OPTIONS $TMPSCRIPT

#### Finally, create the overall tarball for installation

## Create a package for distribution. The package contains:
#  - at91bootstrap.bin
#  - barebox.bin
#  - zImage
#  - wrs-iniramfs.gz
#  - wrs-usr.tar.gz
# Actually, the pack is already built by "wrs_build_wraprootfs", so check
packlist="at91bootstrap.bin barebox.bin zImage wrs-initramfs.gz wrs-usr.tar.gz"
checksum_file="checksums.md5"

wrs_echo "--- Calculating checksums";
rm -f "$WRS_OUTPUT_DIR/images/$checksum_file"
for file in $packlist; do
      (cd "$WRS_OUTPUT_DIR/images" && md5sum "$file" >> "$checksum_file")
done

# Put $checksum_file first into the archive. In case the archive is damaged,
# the beginning of the archive has the biggest chance to be untuched
packlist="$checksum_file $packlist"
# Check if the current git repo correspond to a tag
version=$(cd $WRS_BASE_DIR; git describe --always --dirty)
# If prefix 'wr-switch-sw-' is not set in tagname, we force it to
# 'wr-switch-sw-unof-'
name=$(echo $version | sed  '/^wr-switch-sw-/! s/.*/wr-switch-sw-unof-\0/')
# add date and suffix
name="${name}-$(date +%Y%m%d)_binaries.tar"

wrs_echo "--- Packing into $name";
(cd "$WRS_OUTPUT_DIR/images" && tar cf wrs-firmware.tar  $packlist) || exit 1
(cd "$WRS_OUTPUT_DIR/images" && cp wrs-firmware.tar ../$name) || exit 1

# remove temporaries or keep them
if [ -z "$CONFIG_KEEP_ROOTFS" ]; then
    rm -rf $TMPFS
    rm -rf $TMPSCRIPT
else
    wrs_echo "Image FS is in $TMPFS/"
    wrs_echo "Gerenated by $TMPSCRIPT"
fi
