#!/bin/sh

# Script for splash screens on i.MX51 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
#
# Script expects to find a file /usr/share/splash-images/image-list which contains info about navico
# splash screens. One line per bitmap, columns seperated by taps, columns are:
# 1 - resolution
# 2 - file name
# 3 - file size
# 4 - sha1sum of file
# 5 - true if this bitmaps is latest version, false otherwise
#
# Reuben Dowle

# set -x

SPLASH_FILES_DIR=/usr/share/splash-images
SPLASH_FILES_INFO=${SPLASH_FILES_DIR}/image-list
SPLASH_FILE_MTD=/dev/mtd1

if [ ! -e ${SPLASH_FILES_INFO} ]; then
  echo "No splash images found"
  return 0
fi

DISPLAYRES="$(cut -f2 /proc/navico_platform/disp_mode | cut -d _ -f 1)"

# If we have a blank flash, then we need to write a valid splash screen
if [ "$(dd if=${SPLASH_FILE_MTD} bs=2 count=1 2>/dev/null)" != "BM" ]; then
  UPDATESPLASH=true
  echo "Splash mtd does not contain a bitmap"
fi

# If the mtd has a bitmap programmed into it, Check current SHA1 sum against SHA1 sums 
# for our known splash images. If we match a known SHA1 sum and it is for the wrong 
# resolution OR the bitmap is out-of-date, then we need to update to the latest navico 
# bitmap for this resolution
if [ -z ${UPDATESPLASH} ]; then
  OLDIFS=$IFS
  IFS="	"
  while read line; do
    set -- $line
    CURRENTSHA=$(dd if=${SPLASH_FILE_MTD} bs=1 count=40 skip=$3 2>/dev/null)
    if [ "$CURRENTSHA" = "$4" ]; then
      echo "Splash mtd contains navico splash screen"
      NAVICOSPLASHSCREEN=true
      if [ "$5" = "true" ]; then
        SPLASH_RES=$(echo $line | cut -d " " -f 1)
        if [ "$1" != "${DISPLAYRES}" ]; then
          UPDATESPLASH=true
        fi
      else
        UPDATESPLASH=true
      fi
      break
    fi
  done < $SPLASH_FILES_INFO
  IFS=$OLDIFS
  
  if [ -z ${NAVICOSPLASHSCREEN} ]; then
    echo "Splash mtd contains custom splash image" 

     # A paranoid check for valid custom splash checksum. Not really needed, so it is disabled for now

#    # Read size from bitmap header
#    SPLASH_SIZE=$(dd if=/dev/mtd1 bs=1 count=4 skip=2 2>/dev/null | od -l | head -n1 | awk '{print $2}')
#    EXPECTEDSHA=$(head -c ${SPLASH_SIZE} ${SPLASH_FILE_MTD} | sha1sum | cut -f1 -d " ")
#    CURRENTSHA=$(dd if=${SPLASH_FILE_MTD} bs=1 count=40 skip=${SPLASH_SIZE} 2>/dev/null)
#    if [ "${EXPECTEDSHA}" != "${CURRENTSHA}" ]; then
#      echo "Splash checksum failed"
#      UPDATESPLASH=true
#    fi
  fi
fi

if [ "${UPDATESPLASH}" = "true" ]; then
  echo "Splash mtd needs update..."
  line=$(grep ${DISPLAYRES} ${SPLASH_FILES_INFO} | grep true)
  SPLASH_FILE=$(echo $line | cut -d " " -f 2)
  SPLASH_SIZE=$(echo $line | cut -d " " -f 3)
  EXPECTEDSHA=$(echo $line | cut -d " " -f 4)
  cp ${SPLASH_FILES_DIR}/${SPLASH_FILE} /tmp
  echo ${EXPECTEDSHA} >> /tmp/${SPLASH_FILE}
  flash_eraseall ${SPLASH_FILE_MTD} >/dev/null 2>&1
  nandwrite -p ${SPLASH_FILE_MTD} /tmp/${SPLASH_FILE} >/dev/null 2>&1
  echo "Splash mtd updated to ${SPLASH_FILE}"
fi

exit 0
