#!/bin/sh

# Script for splash screens on i.MX platforms
#
# This is needed because we write a single generic image into unit in the
# factory. When CPU card is placed into a assembled unit, it will detect the
# screen resolution and program the appropriate splash image.
#
# Custom splash screens are stored in /media/factorydata/splash-images.
# Production splash screens are stored in /usr/share/splash-images. The set of
# splash screens for each supported resolution are named
# <image_width>x<image_height>.<extension>. The splash screen for Polaris uses
# a legacy naming convention, Polaris<image_width>x<image_height>.<extension>.
# A cache of the sha1sums for all images is stored along with the set of images
# sorted in reverse numerical order by image width, by image height in a file
# called checksums.sha1. This allows the largest resolution, valid splash
# screen to be selected without complicated math. The checksums.sha1 file is
# created at install time, or before, using the following command:
#
# sha1sum * | sort -nr -tx -k2 | sort -s -nr -t' ' -k2 > checksums.sha1
#
# Any changes here should also consider logic in combinedsplashinstaller update.sh
#
# set -x

source /lib/functions/navico_env.sh
navico_env_init

factorydata_directory="/media/factorydata"

if [ "$PROCESSOR_FAMILY" = "mx28" ] && \
   ! grep -wq "$factorydata_directory" /etc/fstab; then
    factorydata_directory=/etc/NOS
fi

# If the custom splash files directory exists then use it
for images_directory in \
  $factorydata_directory/splash-images \
  /usr/share/splash-images \
  ''
do
  if [ -z "${images_directory}" ]; then
    echo "Can't find images directory"
    exit 1
  elif [ -d "${images_directory}" ]; then
    break
  fi
done

logo_mtd="$(grep -i '"logo"' /proc/mtd|cut -d: -f1)"
if [ -z "${logo_mtd}" ]; then
  echo "Can't find logo partition"
  exit 1
fi

checksums_file="${images_directory}"/checksums.sha1
if [ ! -e "$checksums_file" ]; then
  echo "Can't find checksums file"
  exit 1
fi

# cat /sys/class/graphics/fb0/mode:
# U:800x600p-51
#   ^^^x^^^ = display resolution
display_resolution="$(sed -e 's/^[^0-9]*\([0-9]*x[0-9]*\).*$/\1/' < /sys/class/graphics/fb${FRAMEBUFFER##*[^0-9]}/mode)"
display_width="${display_resolution%x*}"
display_height="${display_resolution#*x}"

# Are we dealing with Polaris, or
# is it a Cummins branded Penn or Teller?
config_filename=/etc/NOS/Config.ini
filename_prefix=
if [ -r "$config_filename" ] &&
    ls "${images_directory}"/Cummins* >/dev/null 2>&1 &&
    grep -iq '^MercuryUIBranding=Cummins\>' "$config_filename"
then
    filename_prefix=Cummins
fi

# Search from largest to smallest available resolution, stopping at the first
# one that's less than or equal to the display resolution

checksums_fifo=/tmp/checksums_fifo
if rm -f $checksums_fifo && mkfifo $checksums_fifo; then
  grep " ${filename_prefix}[[:digit:]]" "$checksums_file" >$checksums_fifo &
  while read line_sha1sum line_filename; do
    image_resolution="${line_filename#$filename_prefix}"
    image_resolution="${image_resolution%%.*}"
    image_width="${image_resolution%x*}"
    image_height="${image_resolution#*x}"
    if [ "$image_width" -le "$display_width" -a \
	 "$image_height" -le "$display_height" ]; then
      image_sha1="$line_sha1sum"
      image_filename="$line_filename"
      image_length=$(stat -c %s "${images_directory}/${image_filename}")
      break
    fi
  done <$checksums_fifo
  rm -f $checksums_fifo
else
  echo "Can't read checksums file"
  exit 1
fi

if [ "${image_filename##*.}" = gz ]; then
    logo_mtd_sys_path="/sys/class/mtd/${logo_mtd}"
    logo_mtd_sz="$(cat ${logo_mtd_sys_path}/size)"
    logo_mtd_erase_sz="$(cat ${logo_mtd_sys_path}/erasesize)"
    sha1_offset=$((logo_mtd_sz - logo_mtd_erase_sz - 40))
else
    sha1_offset=$image_length
fi

if [ "$(dd if=/dev/"${logo_mtd}" bs=1 count=40 skip=$sha1_offset 2>/dev/null)" = "$image_sha1" ]; then
  echo "$logo_mtd is up-to-date with: $image_filename"
else
  echo "$logo_mtd is updating with: $image_filename"
  cp "${images_directory}/${image_filename}" /tmp

  # Flip the image if necessary
  # TODO: HFP-1725 support 90 degree rotations.
  if [ $(cut -f4 /proc/navico_platform/disp_intf) = 180 ]; then
      if [ "${image_filename##*.}" = gz ]; then
          gunzip /tmp/"${image_filename}"
          uncompr_image_filename="${image_filename%%.gz}"
          mv /tmp/"${uncompr_image_filename}" /tmp/unflipped-"${uncompr_image_filename}"
          flip_bmp -i /tmp/unflipped-"${uncompr_image_filename}" -o /tmp/"${uncompr_image_filename}"
          gzip /tmp/"${uncompr_image_filename}"
      else
          mv /tmp/"${image_filename}" /tmp/unflipped-"${image_filename}"
          flip_bmp -i /tmp/unflipped-"${image_filename}" -o /tmp/"${image_filename}"
      fi
  fi

  # Append the unmodified image sha1 into the partition
  printf "%s" "$image_sha1" | dd of=/tmp/"${image_filename}" bs=1 count=40 seek=$sha1_offset >/dev/null 2>&1
  flash_eraseall /dev/"${logo_mtd}" >/dev/null 2>&1
  nandwrite -p /dev/"${logo_mtd}" /tmp/"${image_filename}" >/dev/null 2>&1
  rm /tmp/"${image_filename}"
  echo "done"
fi
