#!/bin/bash
  
  clear
  
  echo -e
  echo -en "\E[32;1m Cuting image vendor extra header... \E[32;0m"
  echo -e
  
  ls STOCK/*.img
  
  echo -e
  echo -en "\033[1m Input image <name>: \033[0m"
  tput sgr0
  
  read INPUT
  
  echo -e
  
  #UTF8 may slow down grep, switch to C
  
  export LANG=C

  #Look for the ext4 magic and calculate its position
  
  magic=`grep -aobP -m1 '\x3A\xFF' STOCK/system.* | head -1 | cut -d":" -f1`
  
  offset=$(($magic))

  echo -en "\033[1m  Rename original $INPUT \033[0m"
  
  mv -v STOCK/$INPUT STOCK/$INPUT.orig
  
  echo -e
  echo -e
  
  dd if=STOCK/$INPUT.orig of=STOCK/$INPUT ibs=$offset skip=1
  
  dd if=STOCK/$INPUT.orig of=STOCK/IVEH_$INPUT ibs=$offset count=1
  
  echo
  echo -en "\033[1m Finished \033[0m"
  echo
  tput sgr0
  echo
  echo "Press Enter to continue"
  read enterKey
