#!/bin/bash

set -e

START_DIR_DONT_CHANGE=`pwd`
SELF=$(basename "$0")
# required config items
RELENG_XORRISO=""
RELENG_GENISOIMAGE=/usr/bin/genisoimage
RELENG_MKISOFS=/usr/bin/mkisofs

# config file
CONFFILE=${HOME}/.libburnia-releng/${SELF}.conf
GEN_DATA_DIR=releng_generated_data/${SELF}

UPPER=40
KEEP=0

#####################################################################
print_help() {
 cat << HLP
Usage:
 $0 -help

# using a config file ${CONFFILE}
 $0 -rc [-keep]

# without using a config file
 $0 xorriso_cmd

# cleanup test generated data directory and exit
 $0 -cleanup
HLP
}

#####################################################################
# cleanup
if [ "${1}" == "-cleanup" ]; then
 cd "${START_DIR_DONT_CHANGE}" || exit 2
 if [ -d "${GEN_DATA_DIR}" ]; then
    rm -rf ${GEN_DATA_DIR}
    printf "${SELF}: removed %s\n" ${GEN_DATA_DIR}
 else
    printf "${SELF}: ${GEN_DATA_DIR} does not exist.\n"
 fi
 exit 0
fi

# help
if [ ! "${1}" ]; then print_help && exit 3; fi
# config file
if [ "${1}" == "-rc" ]; then
 if [ -e ${CONFFILE} ]; then
  . ${CONFFILE}
  printf "${SELF}: Using config file %s\n" ${CONFFILE}
 else
  echo -e "\n${SELF}: Config file ${CONFFILE} not found.\n" && exit 4
 fi
 if [ "${2}" == "-keep" ]; then KEEP=1; fi
# command line args
elif [ "${1}" ]; then
 RELENG_XORRISO="${1}"
 if [ "${2}" == "-keep" ]; then KEEP=1; fi
# the rest
else
 print_help && exit 5
fi

# data dir
if [ -d "${GEN_DATA_DIR}" ]; then
 printf "\n${SELF}: directory %s exists!" ${GEN_DATA_DIR}
 printf "\n${SELF}: use '${SELF} -cleanup' to remove.\n"
 exit 6
else
 mkdir "${GEN_DATA_DIR}"
fi

if [ "${RELENG_XORRISO}" == "" ]; then
 echo -e "\n${SELF}: xorriso_cmd is required\n"
 exit 7
fi

if [ ! -x "${RELENG_XORRISO}" ]; then
 printf "${SELF}: Not found or not an executable: $RELENG_XORRISO\n"
 exit 8
fi

# all must be set at this point
printf "${SELF}: Config items:"
printf "\n\txorriso_cmd=${RELENG_XORRISO}\n"

# xorriso version details, incl. underlying libraries
"${RELENG_XORRISO}" -version
if ! "${RELENG_XORRISO}" -version | grep libjte >/dev/null 2>&1; then
 printf "\n${SELF}: JTE not supported with this xorriso build. Install jigit and rebuild."
 printf "\n${SELF}: See http://www.einval.com/~steve/software/JTE/\n"
 exit 9
fi

################################################
printf "${SELF}: Generating sample tree\n"
for ((i1=0; i1 < ${UPPER}/4; i1++))
do
  for ((i2=0; i2 < ${UPPER}/2; i2++))
  do
    for ((i3=0; i3 < ${UPPER}; i3++))
    do
    mkdir -p ${GEN_DATA_DIR}/DirOne$i1/DirTwo$i2/DirThree$i3
    touch    ${GEN_DATA_DIR}/DirOne$i1/DirTwo$i2/DirThree$i3/FileOne
    done
  done
done

# Disk cache might play dirty games, so re-run these several times?
if [ -x ${RELENG_XORRISO} ]; then
  printf "${RELENG_XORRISO}\n"
  time ${RELENG_XORRISO} -as mkisofs -quiet -print-size ${GEN_DATA_DIR}
fi

if [ -x ${RELENG_GENISOIMAGE} ]; then
  printf "${RELENG_GENISOIMAGE}\n"
  time ${RELENG_GENISOIMAGE}         -quiet -print-size ${GEN_DATA_DIR}
fi

if [ -x ${RELENG_MKISOFS} ]; then
  printf "${RELENG_MKISOFS}\n" 
  time ${RELENG_MKISOFS}             -quiet -print-size ${GEN_DATA_DIR}
fi

if [ ${KEEP} -eq 0 ]; then
 cd ${START_DIR_DONT_CHANGE} || exit 10
 rm -rf ${GEN_DATA_DIR}
fi

exit 0