#!/bin/bash

#t_old="obs/OpenHPC_1.3.9_Factory"
t_old="v3.3.1.GA"
t_new="4.0.0"

f_all=pkg-ohpc.all
f_add=pkg-ohpc.chglog-add
f_del=pkg-ohpc.chglog-del
f_upd=pkg-ohpc.chglog-upd

f_dif=pkg-ohpc.dif
f_old=pkg-ohpc.old
f_new=pkg-ohpc.new

rm -f ${f_add} ${f_del} ${f_upd}
rm -f ${f_dif} ${f_old} ${f_new}



# updated to account for path rename (ks 7/30/15)
#git diff $t_old:docs/recipes/install/centos7.2/vanilla/data/manifest/${f_all}  -- ${f_all} > ${f_dif}

# updated to account for path rename (ks 3/23/17)
#git diff $t_old:docs/recipes/install/centos7.2/x86_64/warewulf/slurm/manifest/${f_all}  -- ${f_all} > ${f_dif}

# updated to account for path rename from 1.3 -> 2.0  (ks 6/05/20)
#git diff $t_old:docs/recipes/install/centos7/x86_64/warewulf/slurm/manifest/${f_all}  -- ${f_all} > ${f_dif}

# updated for RC1 -> 2.0 release (ks 10/5/20)
#git diff $t_old:docs/recipes/install/centos8/x86_64/warewulf/slurm/manifest/${f_all}  -- ${f_all} > ${f_dif}

# updated for 2.4-> 2.5 release (ks 5/29/22)
#git diff $t_old:docs/recipes/install/rocky9/x86_64/warewulf/slurm/manifest/${f_all}  -- ${f_all} > ${f_dif}
git diff $t_old:docs/recipes/install/rocky9/x86_64/warewulf/slurm/manifest/${f_all}  -- ${f_all} > ${f_dif}

# revert back to normal diff (ks 6/7/17)
#git diff ${t_old} ${t_new} ${f_all} >${f_dif}


if [ $? -ne 0 ]; then
   echo "ERROR: git diff failed!"
   exit
fi

grep '^-' ${f_dif} | cut -c 2- | cut -f1,2 -d" " | tail -n +2 | sort >${f_old}
grep '^+' ${f_dif} | cut -c 2- | cut -f1,2 -d" " | tail -n +2 | sort >${f_new}

echo "*** Old release: ${t_old}"
echo "*** New release: ${t_new}"
echo
echo "*** Packages Deleted:"
join -v 1 ${f_old} ${f_new} > ${f_del}
join -v 2 ${f_old} ${f_new} >  ${f_add}
join      ${f_old} ${f_new} > ${f_upd}

echo
echo "[Component Additions]"

while read entry; do
      name=`echo ${entry} | awk '{print $1}'`
      version=`echo ${entry} | awk '{print $2}'`

      printf "  * %-36s     (%s)\n" ${name} ${version}

done < ${f_add}

echo " "
echo "[Component Version Changes]"
while read entry; do
      name=`echo ${entry} | awk '{print $1}'`
      oldversion=`echo ${entry} | awk '{print $2}'`
      newversion=`echo ${entry} | awk '{print $3}'`

      if [ "x$oldversion" != "x$newversion" ];then
          printf "  * %-36s     (%s)\n" ${name} "${oldversion} -> ${newversion}"
      fi

done < ${f_upd}

oldversion=3.6
newversion=3.6

if [ "x$oldverson" == "x$newversion" ];then
   echo "we have a match"
   exit 0
fi

rm -f ${f_dif} ${f_old} ${f_new}
