#!/bin/bash

HOST=$(cat /etc/hostname)
BOOT_SCRIPT=/usr/local/bin/syncloud-boot.sh
BOOT_LOG=/var/log/syncloud_boot.log
SYSTEM_BOOT_SCRIPT=/etc/rc.local

apt-get -y install ntp ntpdate udisks-glue

adduser www-data plugdev

# tell beagle flasher to skip /data dir
if [[ ${HOST} = "arm" ]]; then
    BEAGLE_FLASHER=/opt/scripts/tools/beaglebone-black-eMMC-flasher.sh
    sed -i '/syncloud/d' ${BEAGLE_FLASHER}
    sed -i '/^copy_rootfs$/i umount /data || true # syncloud' ${BEAGLE_FLASHER}
fi

#Removing old and new boot scripts
sed -i '/syncloud/d' ${SYSTEM_BOOT_SCRIPT}

# add boot script to rc.local
sed -i "/# By default this script does nothing./a "${BOOT_SCRIPT}" > "${BOOT_LOG}" 2>&1 " ${SYSTEM_BOOT_SCRIPT}

#run boot script
${BOOT_SCRIPT}