#!/bin/bash

set -x

readonly DIRECTORY_ROOT="/home/johnlewis.ie/Chromebook-ROMs/"
declare DIRECTORY
readonly CBFS_TOOL=("$DIRECTORY_ROOT"utils/cbfstool)

update_rw_legacys () {
  DIRECTORY=("$DIRECTORY_ROOT"legacy-slots)
  local date
  date="$(date +%d%m%y)"
  cd "$DIRECTORY"
  rm_cbfs "$SLOT_TYPE"-template.cbfs payload
  add_payload "$SLOT_TYPE"-template.cbfs payload \
  "$DIRECTORY_ROOT"/seabios-payloads/bios.bin.elf."$FILE"
  cp "$SLOT_TYPE"-template.cbfs \
  "$SLOT_TYPE"-"$date".cbfs
  rm -f "$SLOT_TYPE"-latest.cbfs
  ln -s "$SLOT_TYPE"-"$date".cbfs \
  "$SLOT_TYPE"-latest.cbfs
  tar -cjvhf "$SLOT_TYPE"-latest.cbfs.tar.bz2 \
  "$SLOT_TYPE"-latest.cbfs
  md5sum * > legacy-slots.md5
}

update_full_roms () {
  local model
  local date
  date="$(date +%d%m%y)"
  cd "$DIRECTORY_ROOT"

  for model in ${FULL_ROM_MODELS}; do
    cd "$model" || err "
ERROR: Could not change into directory for $model have you created it?
"
    pwd
    rm_cbfs coreboot-"$model"-seabios-template.rom fallback/payload
    add_payload coreboot-"$model"-seabios-template.rom fallback/payload \
    "$DIRECTORY_ROOT"/seabios-payloads/bios.bin.elf
    cp coreboot-"$model"-seabios-template.rom \
    coreboot-"$model"-seabios-"$date".rom
    rm -f coreboot-"$model"-seabios-latest.rom
    ln -s coreboot-"$model"-seabios-"$date".rom \
    coreboot-"$model"-seabios-latest.rom
    tar -cjvhf coreboot-"$model"-seabios-latest.rom.tar.bz2 \
    coreboot-"$model"-seabios-latest.rom
    md5sum * > "$model".md5
    cd ..
  done
}

update_boot_stubs () {
  DIRECTORY=("$DIRECTORY_ROOT"shellballs)
  local model
  local cbfs_file
  cd "$DIRECTORY"

  for model in ${BOOT_STUB_MODELS}; do
#  case $model in
#  lumpy|parrot|stout|butterfly)
    cbfs_file="bios.cbfs.new"
#    ;;
#  *)
#    cbfs_file="bios.bin.new"
#    ;;
#  esac
    cd "$model" || err "
ERROR: Could not change into directory for $model have you created it?
"
    pwd
    cbfs_model=$(xxd "$cbfs_file" |grep -iom1 "$model" |tr '[:upper:]' '[:lower:]') 
    echo "$cbfs_model"
    if [[ "$cbfs_model" != "$model" ]]; then
    err "
ERROR: This CBFS file is not for $model and will brick someone's device, John you numpty!
"
    fi
    rm_cbfs "$cbfs_file" fallback/payload
    add_payload "$cbfs_file" fallback/payload "$DIRECTORY_ROOT"/seabios-payloads/bios.bin.elf.baytrail
#    case $model in
#    lumpy|parrot|stout|butterfly)
#      :
#      ;;
#    *)
#      dd bs=1M skip=7 if=bios.bin.new of=bios.cbfs.new
#      ;;
#    esac
    rm -vf bios.cbfs.new.tar.bz2 || err "
ERROR: Could not remove $model's old compressed BOOT_STUB file. Did you create
an initial one?
"
    tar -cjvf bios.cbfs.new.tar.bz2 bios.cbfs.new
    md5sum * > "$model".md5
    cd ..
  done
}

function rm_cbfs {
   "$CBFS_TOOL" "$1" remove -n "$2" || err "
ERROR: Could not remove $2 from $1 Is there something wrong with the CBFS file?
"
}

function add_payload {
  "$CBFS_TOOL" "$1" add-payload -n "$2" -f "$3" -c lzma || err "
ERROR: Could not add payload, is there something wrong with cbfstool?
"
}

err() {
  echo "$@" >&2
  exit 1
}

ctrl_c() {
  echo "

INFO: Script cancelled by user, exiting ...
"
  exit 0
}

