#!/bin/sh

root=$(cd $(dirname $0); pwd)
qmldir=/opt/victronenergy/gui/qml

cp -f $root/OverviewJD.qml $qmldir
if ! grep -lq "OverviewJD sentinel" $qmldir/main.qml; then
  cp -f $qmldir/main.qml /tmp
  (cd /tmp ; patch >/dev/null 2>&1 ) <$root/main.qml.diff
  if [ $? -ne 0 ]; then
    echo "Patching main.qml failed"
  else
    echo "Updating main.qml"
    mv -f $qmldir/main.qml $qmldir/main.qml.orig
    mv /tmp/main.qml $qmldir/
  fi
else
  echo "main.qml already patched, skipping"
fi
# Create rc.local hooks for reinstallation
mkdir /data/rc.local.d /data/rcS.local.d >/dev/null 2>&1
cat >/data/rc.local.d/overviewjd <<EOF
#!/bin/sh
sh $root/install.sh
EOF

cat >/data/rc.local <<EOF
#!/bin/sh
for s in /data/rc.local.d/*; do
  \$s
done
EOF

cat >/data/rcS.local <<EOF
#!/bin/sh
for s in /data/rcS.local.d/*; do
  \$s
done
EOF

chmod 755 /data/rc.local.d/overviewjd /data/rc.local /data/rcS.local
