#!/bin/bash
# ONLY TEMPORARY!
# Used for the time till ECP 4.0 works fine and the image can be used normally without any tweaks

PATH_OPKG_FOLDER="/opt/opkg-persistent"
PATH_OPKG_STATUS_FILE="$PATH_OPKG_FOLDER/status"
PATH_WEBUI_WORKDIR="/mnt/persistent-data/evtec-webui-data/"

# Change owner of /mnt/persistent-data/
ownerPersistentData=$(ls -l /mnt/ | grep persistent | cut -d' ' -f3)
if [ "$ownerPersistentData" != "xfc" ]; then
    chown -R xfc:xfc /mnt/persistent-data

    # Do not make kaputt my app webui! Please find together another way to do the things.
    if [ -d "$PATH_WEBUI_WORKDIR" ]; then
        chown -R www-data:www-data "$PATH_WEBUI_WORKDIR"
    fi
fi

mount -o remount,rw /

# Change owner of /opt/ECP/
ownerECP=$(ls -l /opt/ | grep ECP | cut -d' ' -f3)
if [ "$ownerECP" != "xfc" ]; then
    chown -R xfc:xfc /opt/ECP
fi

# Create ttyRFID file
ln -s /dev/ttymxc2 /dev/ttyRFID

# Create .ssh folder in /mnt/persistent-data
mkdir -p /mnt/persistent-data/.ssh

# Copy opkg file from rootfs into /var (when a package was installed manually)
if [ -f $PATH_OPKG_STATUS_FILE ]; then
    cp -f $PATH_OPKG_STATUS_FILE /var/lib/opkg/status
fi

# Start opkg update procedure
/bin/bash /opt/package-installer/opkg-install.sh

mount -o remount,ro /