#!/bin/sh

# -----------------------------------------------------------------------
#
# Copyright  2021 by Global Phasing Limited
#
#           All rights reserved.
#
#           This software is proprietary to and embodies the confidential
#           technology of Global Phasing Limited (GPhL). Possession, use,
#           duplication or dissemination of the software is authorised
#           only pursuant to a valid written licence from GPhL.
#
# Contact   buster-develop@GlobalPhasing.com
#
# -----------------------------------------------------------------------

md5_BUSTER_before=ea940ed8e3cca886a5b207e07439a4b3
md5_BUSTER_ibefore=cac4d4fdb74293f13c747c393aed9bba
md5_ALL_before=c0b4b0c5eb882cf68468501616b1efb6
md5_ALL_ibefore=75aaaaf432b45e4155bd0f502cd3a14d

datum="20210716"

error () {
  echo " "
  [ $# -eq 0 ] && echo " ERROR: see above" || echo " ERROR: $@"
  echo " "
  exit 1
}
get_md5sum () {
    local res
    local UNAME=`uname`
    if [ "X$UNAME" = "XDarwin" ]; then
        res=`/sbin/md5 $@ 2>/dev/null | awk '{print $4}'`
    else
        res=`md5sum $@ 2>/dev/null | awk '{print $1}'`
    fi
    [ $? -ne 0 ] && note "No MD5SUM" && res=0
    echo $res
}

get_cksum () {
    local res
    res=`cksum $@ 2>/dev/null | awk '{print $2}'`
    [ $? -ne 0 ] && note "No CKSUM" && res=0
    echo $res
}

[ "X`uname`" != "XDarwin" ] && error "this script should only be executed on a Mac OS system"

for t in ALL BUSTER
do
  [ -f GPhL_${t}_snapshot_${datum}.darwin.tar ] && break
done

[ ! -f GPhL_${t}_snapshot_${datum}.darwin.tar ] && error "file \"GPhL_${t}_snapshot_${datum}.darwin.tar\" not found"
[ ! -f GPhL_${t}_snapshot_${datum}_install.sh ] && error "file \"GPhL_${t}_snapshot_${datum}_install.sh\" not found"

eval "md5_before=\"\$md5_${t}_before\""
eval "md5_ibefore=\"\$md5_${t}_ibefore\""

printf " checks ... "
[ ! -f GPhL_${t}_snapshot_${datum}.darwin.tar ] && error "file \"GPhL_${t}_snapshot_${datum}.darwin.tar\" not found"
[ ! -f GPhL_${t}_snapshot_${datum}_install.sh ] && error "file \"GPhL_${t}_snapshot_${datum}_install.sh\" not found"

[ "X`get_md5sum GPhL_${t}_snapshot_${datum}.darwin.tar`" != "X$md5_before" ] && error "file \"GPhL_${t}_snapshot_${datum}.darwin.tar\" not as expected"
[ "X`get_md5sum GPhL_${t}_snapshot_${datum}_install.sh`" != "X$md5_ibefore" ] && error "file \"GPhL_${t}_snapshot_${datum}_install.sh\" not as expected"

type tar >/dev/null 2>&1
[ $? -ne 0 ] && error "command \"tar\" not found"
ls | grep -v "^__" | grep -v "GPhL_${t}_snapshot_${datum}_install.sh" | sort > __$$.1 || error "unable to list files in current directory (1)"

echo "done"

printf " unpacking ... "
tar -xf GPhL_${t}_snapshot_${datum}.darwin.tar || error
ls | grep -v "^__" | grep -v "GPhL_${t}_snapshot_${datum}_install.sh" | sort > __$$.2 || error "unable to list files in current directory (1)"
tar -xf all.darwin.tar || error
ls | grep -v "^__" | grep -v "GPhL_${t}_snapshot_${datum}_install.sh" | sort > __$$.3 || error "unable to list files in current directory (1)"
echo "done"

printf " fixing ... "
mv grade2.darwin.tar.gz grade2_darwin.tar.gz || error
sed "s/grade2\.\$hwsp/grade2_\$hwsp/g" install_autoBUSTER.sh > __$$.sh && mv __$$.sh install_autoBUSTER.sh || error
chmod 0750 install_autoBUSTER.sh || error
echo "done"

printf " packaging ... "
__fs=`diff __$$.2 __$$.3 | grep "^>" | awk '{print $2}' | sed "s/grade2\./grade2_/g"`
tar -cf all.darwin.tar $__fs || error
rm -fr $__fs

__fs=`diff __$$.1 __$$.2 | grep "^>" | awk '{print $2}'`
tar -cf GPhL_${t}_snapshot_${datum}.darwin.tar $__fs || error
rm -fr $__fs __$$.1 __$$.2 __$$.3
echo "done"

printf " final checks ... "
sed "s/mdsum_darwin_chk=.*/mdsum_darwin_chk=\"`get_md5sum GPhL_${t}_snapshot_${datum}.darwin.tar`\"/g" GPhL_${t}_snapshot_${datum}_install.sh | sed "s/cksum_darwin_chk=.*/cksum_darwin_chk=\"`get_cksum GPhL_${t}_snapshot_${datum}.darwin.tar`\"/g" > __$$.sh && mv __$$.sh GPhL_${t}_snapshot_${datum}_install.sh || error
chmod +x GPhL_${t}_snapshot_${datum}_install.sh || error
echo "done"

cat <<e

 You should now be able to install normally using the files:
e
ls -l GPhL_${t}_snapshot_${datum}.darwin.tar GPhL_${t}_snapshot_${datum}_install.sh
echo " "



