#!/usr/bin/env bash
unset variant_desc
variant_bin="paulstretch"
USE_NTK=false

[ "$1" == "-ntk" ] &&
  USE_NTK=true

build_flags=( -lz -lpthread -lmad -DHAVE_JACK -DENABLE_RESAMPLING \
  $( pkg-config --cflags --libs jack audiofile fftw3f ogg vorbis vorbisenc vorbisfile \
    portaudio-2.0 mxml samplerate ) )

if ( ${USE_NTK} ) then
  toolkit="ntk"
  fluid_bin="ntk-fluid"
  build_flags+=( $( pkg-config --cflags --libs ntk ) )
  variant_bin+="-${toolkit}"
else
  toolkit="fltk"
  fluid_bin="fluid"
  build_flags+=( $( fltk-config --cflags --ldflags ) )
fi

${fluid_bin} -c GUI.fl 
${fluid_bin} -c FreeEditUI.fl

out_dir="usr/bin"
rm -f ${out_dir}/${variant_bin}
mkdir -p ${out_dir}
echo "build_flags(${toolkit}) ${build_flags[@]}"
g++ GUI.cxx FreeEditUI.cxx *.cpp Input/*.cpp Output/*.cpp ${build_flags[@]} \
  -o ${out_dir}/${variant_bin}

(${USE_NTK}) &&
  variant_desc=" (non-NTK)"

out_dir="usr/share/applications"
mkdir -p ${out_dir}
cat <<EOF > ${out_dir}/${variant_bin}.desktop
[Desktop Entry]
Name=PaulStretch${variant_desc}
Comment=Paul's Extreme Sound Stretch
Exec=${variant_bin}
Icon=${variant_bin}.png
Terminal=false
Type=Application
Categories=Audio;AudioVideo;
EOF

out_dir="usr/share/icons/hicolor/256x256/apps"
mkdir -p ${out_dir}
cp -f debian/paulstretch.png ${out_dir}/${variant_bin}.png
