#!/bin/bash
##########################################################################################################
#
# This script updates classpath files based on a dependency flag stored in current_dependency_flag.txt.
#
# Every time a jar-file dependency is added or removed, the dependency flag is updated,
# and the .classpath file is updated by copying in the contents from .classpathmaster
# which contains all the original contents of .classpath.
# However, each line in .classpathmaster referencing a jar file is only copied over
# to .classpath if the corresponding bit in the dependency flag is set to 1.
#
# dependency flag bit 0x001 - copy lucene jar line from .classpathmaster into .classpath
# dependency flag bit 0x002 - copy commons-net jar line from .classpathmaster into .classpath
# dependency flag bit 0x004 - copy hamcrest jar line from .classpathmaster into .classpath
# dependency flag bit 0x008 - copy junit jar line from .classpathmaster into .classpath
# dependency flag bit 0x010 - copy osgi jar line from .classpathmaster into .classpath
# dependency flag bit 0x020 - copy bcel jar line from .classpathmaster into .classpath
# dependency flag bit 0x040 - copy commons-lang3 jar line from .classpathmaster into .classpath
# dependency flag bit 0x080 - copy jspeex jar line from .classpathmaster into .classpath
# dependency flag bit 0x100 - copy minio jar line from .classpathmaster into .classpath
#
# NOTE: .classpathlinks is updated with contents from .classpathlinksmaster but
#       uses the same logic as .classpath and .classpathmaster.
#
# NOTE: common.gradle and the local variable TOTAL_NUM_DEPENDENCIES need to be
#       updated if a dependency is added
#
# NOTE: if dependencies for LIBNXM change, also update strip-graphics.sh
#
# Author: Zachary Matthews
# Since:  NeXtMidas 4.1.4
#
##########################################################################################################

function update_classpath_file () {
  classpath_type=${1}
  case ${classpath_type} in
    main)  classpath_master_file=${NMROOT}/.classpathmaster; classpath_file=${NMROOT}/.classpath;;
    links) classpath_master_file=${NMROOT}/gradle/.classpathlinksmaster; classpath_file=${NMROOT}/gradle/.classpathlinks;;
    *)     echo "Unrecognized classpath type ${classpath_type}"; return;;
  esac

  head -n4 ${classpath_master_file} > ${classpath_file}

  bit=1
  TOTAL_NUM_DEPENDENCIES=9
  for i in {1..$TOTAL_NUM_DEPENDENCIES}
  do
    if [ ! $((${bit}&${depFlag})) -eq 0 ] ; then
      head -n$((${i}+4)) ${classpath_master_file} | tail -n1 >> ${classpath_file}
    fi
    bit=$((${bit}<<1))
  done

  tail -n2 ${classpath_master_file} >> ${classpath_file}
}

NMROOT_TMP=`realpath ${0}`
for i in {1..2}
do
  NMROOT_TMP=`dirname ${NMROOT_TMP}`
done
NMROOT=${NMROOT_TMP}

depFlagHex=`head -n1 ${NMROOT}/current_dependency_flag.txt` # original flag as hex
depFlag=`echo $((16#${depFlagHex:2}))` # hex to decimal

update_classpath_file main
update_classpath_file links
