#!/bin/sh
# disabling 'shellcheck' warnings about 'expr substr' which works fine on BusyBox
# shellcheck disable=SC2308

log_do() {
  printf '\n   . '
  printf "%s" "$1"
  printf '... '
  shift
  "$@" && printf 'Done' || printf "\n   ! Some errror happened. Errorcode %s" "$?"
}

model_type="$(cat /sys/firmware/devicetree/base/model)"
if [ "$(expr substr "$model_type" 1 6 )" != "PERCo-" ] ; then
	printf " ! Not PERCo system! Model is [%s]   Canceling upgrade\n" "$model_type"
	rm -f firmware/s30
	exit 1
fi

model_exact="$(expr substr "$model_type" 7 40)"
if [ "$model_exact" != "CR02.9" ] && [ "$model_exact" != "CR11.1" ] && [ "$model_exact" != "CT13.1" ] && [ "$model_exact" != "CTL14.1" ] && [ "$model_exact" != "CL15.1" ] && [ "$model_exact" != "CL15.3" ] && [ "$model_exact" != "CL15.7" ] && [ "$model_exact" != "STB01" ]; then
	printf " ! Not v3s system! Model is [%s]   Canceling upgrade\n" "$model_type"
	rm -f firmware/s30
	exit 1
fi

mkdir -p /mnt/boot ; mount -o ro /dev/mmcblk0p1 /mnt/boot
if [ ! -f /mnt/boot/version ] || [ "$(cat /mnt/boot/version)" != "2" ] ; then
	umount /mnt/boot
	printf " ! Wrong version, install previous update   Canceling upgrade\n"
	rm -f firmware/s30
	exit 1
fi

if [ -r firmware/systemupdate/rootfs.cpio.uboot ] ; then
  first_crc="$(crc32 /mnt/boot/rootfs.cpio.uboot | cut -d' ' -f1)"
  second_crc="$(crc32 firmware/systemupdate/rootfs.cpio.uboot | cut -d' ' -f1)"
  if [ ! "$first_crc" = "$second_crc" ] ; then
    mount -o remount,rw /mnt/boot
    cp -f firmware/systemupdate/rootfs.cpio.uboot /mnt/boot/rootfs.cpio.uboot
  fi
fi


model_small="$(echo "$model_exact" |  tr '[:upper:].' '[:lower:]-')"
if [ -r "firmware/systemupdate/sun8i-v3s-$model_small.dtb" ] ; then
  first_crc="$(crc32 "/mnt/boot/sun8i-v3s-$model_small.dtb" | cut -d' ' -f1)"
  second_crc="$(crc32 "firmware/systemupdate/sun8i-v3s-$model_small.dtb" | cut -d' ' -f1)"
  if [ ! "$first_crc" = "$second_crc" ] ; then
    mount -o remount,rw /mnt/boot
    cp -f "firmware/systemupdate/sun8i-v3s-$model_small.dtb" "/mnt/boot/sun8i-v3s-$model_small.dtb"
  fi
fi


umount /mnt/boot

if [ -d firmware/kernelupdate ] ; then
  printf ' . firmware kernelupdate found. Trying to do kernelupdate \n'
  if firmware/kernelupdate/update ; then
    printf ' . kernelupdate successfully finished.\n'
  else
    printf " ! kernelupdate ended with code %s --- exiting\n" "$?"
    rm -f firmware/s30
    exit 1
  fi
fi

if [ -d firmware/systemupdate ] ; then
  printf ' . firmware systemupdate found. Trying to do systemupdate \n'
  if firmware/systemupdate/update ; then
    printf ' . systemupdate successfully finished.\n'
  else
    printf " ! systemupdate ended with code %s --- exiting\n" "$?"
    rm -f firmware/s30
    exit 1
  fi
fi
printf ' . v -- Old data saving script started --\n'
log_do 'Copying database' cp s30/base.db* firmware
log_do 'Copying defaults' cp s30/default.lua firmware
if [ -f firmware/ssl.pem.old ] && cmp -s s30/ssl.pem firmware/ssl.pem.old ; then
    printf '\n   . SSL keys updated'
else
  log_do 'Copying SSL keys' cp s30/ssl.pem firmware
fi
rm -f firmware/ssl.pem.old
sync
log_do 'Request reboot' touch /reboot_me_please
printf '\n'
