#!/bin/bash

if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
  echo "Configuration Options:"
  echo "  -h, --help        Display this help and exit"
  echo "  --no_flac         Exclude FLAC decoder"
  echo "  --foxenflac       Use Foxen FLAC decoder (makes library LGPLv2.1+)"
  echo "  --static          Build static library (default is shared)"
  echo "  --test            Build faun_test program (modifies library)"
  echo "  --prefix <dir>    Set install directory root"
  exit
fi

FLAC=1
STATIC=0
TEST=0
PREFIX=/usr/local

while [ "$1" != "" ]; do
  case $1 in
    --no_flac)
      FLAC=0 ;;
    --foxenflac)
      FLAC=2 ;;
    --static)
      STATIC=1 ;;
    --test)
      TEST=1 ;;
    --prefix)
      shift
      PREFIX=$1 ;;
    *)
      echo "Invalid option $1"
      exit 1
      ;;
  esac
  shift
done

logic() {
  [ $1 = 0 ] && echo false || echo true
}

echo "Generating make.config & project.config"
printf "flac: ${FLAC}\nstatic: $(logic ${STATIC})\nftest: $(logic ${TEST})\n" >project.config
printf "FLAC=${FLAC}\nSTATIC=${STATIC}\nFTEST=${TEST}\nDESTDIR=${PREFIX}\n" >make.config
echo "Now type make (or copr) to build."
