#!/bin/sh
#
# Configure script for mythplugins
#

###
# functions from ffmpeg's configure

log(){
    echo "$@" >> $logfile
}

log_file(){
    log BEGIN $1
    pr -n -t $1 >> $logfile
    log END $1
}

echolog(){
    log "$@"
    echo "$@"
}

set_all(){
    value=$1
    shift
    for var in $*; do
        eval $var=$value
    done
}

enable(){
    set_all yes $*
}

disable(){
    set_all no $*
}

enabled(){
    test "${1#!}" = "$1" && op== || op=!=
    eval test "x\$${1#!}" $op "xyes"
}

disabled(){
    test "${1#!}" = "$1" && op== || op=!=
    eval test "x\$${1#!}" $op "xno"
}

enabled_all(){
    for opt; do
        enabled $opt || return 1
    done
}

disabled_all(){
    for opt; do
        disabled $opt || return 1
    done
}

enabled_any(){
    for opt; do
        enabled $opt && return 0
    done
}

disabled_any(){
    for opt; do
        disabled $opt && return 0
    done
    return 1
}

is_in(){
    value=$1
    shift
    for var in $*; do
        [ $var = $value ] && return 0
    done
    return 1
}

check_cmd(){
    log "$@"
    "$@" >> $logfile 2>&1
}

die_unknown(){
    echo "Unknown option \"$1\"."
    echo "See $0 --help for available options."
    exit 1
}

die(){
    echolog "$@"
    cat <<EOF

If you think configure made a mistake, make sure that you are using the latest
version of MythTV from git.  If the latest version fails, report the problem to the
mythtv-dev@mythtv.org mailing list or IRC #mythtv on irc.libera.chat
EOF
cat <<EOF
Include the log file "$logfile" produced by configure as this will help
solving the problem.
EOF
    exit 1
}

append(){
    var=$1
    shift
    eval "$var=\"\$$var $*\""
}

prepend(){
    var=$1
    shift
    eval "$var=\"$* \$$var\""
}

check_cmd(){
    log "$@"
    "$@" >> $logfile 2>&1
}

filter_cflags=echo
filter_cppflags=echo
filter_cxxflags=echo
filter_cxxppflags=echo

check_cc(){
    log check_cc "$@"
    cat > $TMPC
    log_file $TMPC
    check_cmd $CC $CPPFLAGS $CFLAGS "$@" -c -o $TMPO $TMPC
}

check_cpp(){
    log check_cpp "$@"
    cat > $TMPC
    log_file $TMPC
    check_cmd $CC $CPPFLAGS $CFLAGS "$@" -E -o $TMPO $TMPC
}

check_ld(){
    log check_ld "$@"
    flags=''
    libs=''
    for f; do
        test "${f}" = "${f#-l}" && flags="$flags $f" || libs="$libs $f"
    done
    check_cc $($filter_cflags $flags) || return
    check_cmd $LD $LDFLAGS $flags -o $TMPE $TMPO $libs $EXTRALIBS
}

check_header(){
    log check_header "$@"
    header=$1
    shift
    check_cpp "$@" <<EOF
#include <$header>
int x;
EOF
}

check_func(){
    log check_func "$@"
    func=$1
    shift
    check_ld "$@" <<EOF
extern int $func();
int main(void){ $func(); }
EOF
}

check_func_headers(){
    log check_func_headers "$@"
    headers=$1
    func=$2
    shift 2
    incs=""
    for hdr in $headers; do
        incs="$incs
#include <$hdr>"
    done
    check_ld "$@" <<EOF
$incs
int main(int argc, char **argv){
    return (long) $func;
}
EOF
}

check_lib(){
    log check_lib "$@"
    header="$1"
    func="$2"
    shift 2
    check_header $header && check_func $func "$@"
}

check_lib2(){
    log check_lib2 "$@"
    headers="$1"
    func="$2"
    shift 2
    check_func_headers "$headers" $func "$@"
}

#CXX related tests
check_header_cxx(){
    log check_header_cxx "$@"
    header=$1
    shift
    check_cxx "$@" <<EOF
#include <$header>
int x;
EOF
}

add_ldflags(){
    append ELDFLAGS "$@"
}

add_cxxflags(){
    append CXXFLAGS "$@"
}

add_ecxxflags(){
    append ECXXFLAGS "$@"
}

check_cxx(){
    log check_cxx "$@"
    cat > $TMPCXX
    log_file $TMPCXX
    check_cmd $CXX $CXXPPFLAGS $ECXXFLAGS "$@" -c -o $TMPO $TMPCXX
}

check_ld_cxx(){
    log check_ld_cxx "$@"
    flags=''
    libs=''
    for f; do
        test "${f}" = "${f#-l}" && flags="$flags $f" || libs="$libs $f"
    done
    check_cxx $($filter_cxxflags $flags) || return
    check_cmd $CXX $LDFLAGS $flags -o $TMPE $TMPO $libs
}

check_func_cxx(){
    log check_func_cxx "$@"
    func=$1
    header=$2
    shift 2
    disable $(echo $func | sed -e 's/:://g')
    check_ld_cxx "$@" <<EOF
#include <$header>
int main(void){ $func(); }
EOF
}

check_class_cxx(){
    log check_class_cxx "$@"
    class=$1
    header=$2
    shift 2
    check_ld_cxx "$@" <<EOF
#include <$header>
int main(void){ $class; }
EOF
}

check_lib_cxx(){
    log check_lib_cxx "$@"
    header="$1"
    func="$2"
    shift 2
    check_header_cxx $header && check_func_cxx $func $header "$@"
}

check_lib_cxx2(){
    log check_lib_cxx2 "$@"
    header="$1"
    func="$2"
    shift 2
    check_header_cxx $header && check_class_cxx $func $header "$@"
}

cp_if_changed(){
    cmp -s "$1" "$2" && { test "$quiet" != "yes" && echo "$2 is unchanged"; } && return
    mkdir -p "$(dirname $2)"
    cp -f "$1" "$2"
    echo "Created $2"
}

#
#    default parameters for all plugins
#

PLUGIN_LIST="
archive
browser
game
music
netvision
news
weather
zoneminder
zmserver
"

DEPRECATED_PLUGIN_LIST="
flix
"

CONFIG_LIST="
opengl
exif
newexif
dcraw
cdio
musicbrainz
"

DEPEND_LIST="
vorbis
flac
mp3lame
"

enable $PLUGIN_LIST $CONFIG_LIST $DEPEND_LIST
disable $DEPRECATED_PLUGIN_LIST

prefix="/usr/local"
libdir_name="lib"
sysroot=""
mythroot=""
ln_s="ln -sf"

logfile="config.ep"

cc="gcc"
python="python3"

targetos=`uname -s`

if test x"$1" = x"-h" -o x"$1" = x"--help" ; then
cat << EOF

Usage:   configure [options]
Options: [defaults in brackets after descriptions]
NB:      all --enable-* options can be reversed with --disable-*

Generic options:
  --prefix=PREFIX          MythTV install location PREFIX [$prefix]
  --sysroot=SYSROOT        General sysroot location SYSROOT [$sysroot]
  --mythroot=SYSROOT       MythTV specific sysroot location [$mythroot]
  --python=PATH            Force a specific python executable to use [$python]
  --libdir-name=LIBNAME    install/look for libs in PREFIX/LIBNAME [$libdir_name]
  --help                   print this message
  --enable-all             Enable all options
  --previous               use previous configure parameters if possible
  --extra-ldflags=ELDFLAGS add ELDFLAGS to LDFLAGS [$LDFLAGS]

MythArchive related options:
  --enable-mytharchive     build the mytharchive plugin [$archive]

MythBrowser related options:
  --enable-mythbrowser     build the mythbrowser plugin [$browser]

MythGame related options:
  --enable-mythgame        build the mythgame plugin [$game]

MythMusic related options:
  --enable-mythmusic       build the mythmusic plugin [$music]
  --enable-cdio            enable cd playback [$cdio]
  --enable-musicbrainz     enable fetching cd metadata from musicbrainz [$musicbrainz]

MythNetvision related options:
  --enable-mythnetvision   build the mythnetvision plugin [$netvision]

MythNews related options:
  --enable-mythnews        build the mythnews plugin [$news]

MythWeather related options:
  --enable-mythweather     build the mythweather plugin [$weather]

MythZoneMinder related options:
  --enable-mythzoneminder  build the mythzoneminder plugin [$zoneminder]
  --enable-mythzmserver    build the mythzmserver  [$zmserver]

EOF
exit 0
fi
if [ -e config.log ] ; then
    PCONF=`cat config.log | tail -n 1 | grep configure`
    if test x"$PCONF" != x"" ; then
        PCONF=`echo $PCONF | sed 's/.\/configure//g'`
    fi
    for opt do
        if test x"$opt" != x"--prev" -a x"$opt" != x"--previous" ; then
            OTHOPT="$OTHOPT$opt"
        fi
        if test x"$opt" = x"--prev" -o x"$opt" = x"--previous" ; then
            if test x"$PCONF" != "--prev" ; then
                PREV="yes";
            fi
        fi
    done
    if test x"$PREV" = x"yes"; then
        echo "Options: $PCONF $OTHOPT"
        ./configure $PCONF $OTHOPT
        exit
    fi
fi


CMDLINE_SET="
    logfile
    python
    sysroot
    mythroot
"

CONFIGURATION_OPTS=""
for opt do
  CONFIGURATION_OPTS="$CONFIGURATION_OPTS ""$opt"
done

date >> config.log
echo "   $0$CONFIGURATION_OPTS" >> config.log

for opt do
  optval="${opt#*=}"
  case "$opt" in
  --targetos=*) targetos=`echo $opt | cut -d '=' -f 2`
  ;;
  --prefix=*) prefix=`echo $opt | cut -d '=' -f 2`
  ;;
  --libdir-name=*) libdir_name=`echo $opt | cut -d '=' -f 2`;
  ;;
  --enable-myth?*|--disable-myth?*)
  eval $(echo "$opt" | sed 's/--/action=/;s/-myth/ option=/;s/-/_/g')
  if is_in $option $PLUGIN_LIST; then
      $action $option
  elif is_in $option $DEPRECATED_PLUGIN_LIST; then
      $action $option
  else
      die_unknown $opt
  fi
  ;;
  --enable-new-exif)  enable exif newexif
  ;;
  --disable-new-exif) disable newexif
  ;;
  --enable-dcraw) dcraw="yes"
  ;;
  --disable-dcraw) dcraw="no"
  ;;
  --icc-profile=*) icc=`echo $opt | cut -d '=' -f 2`
  ;;
  --enable-all)
      enable $DEPRECATED_PLUGIN_LIST $PLUGIN_LIST $CONFIG_LIST
  ;;
  --disable-all)
      disable $DEPRECATED_PLUGIN_LIST $PLUGIN_LIST
  ;;
  --enable-?*|--disable-?*)
  eval $(echo "$opt" | sed 's/--/action=/;s/-/ option=/;s/-/_/g')
  if is_in $option $CONFIG_LIST; then
      $action $option
  else
      die_unknown $opt
  fi
  ;;
  --extra-ldflags=*)
      add_ldflags $optval
  ;;
  *)
  optname="${opt%%=*}"
  optname="${optname#--}"
  optname=$(echo "$optname" | sed 's/-/_/g')
  if is_in $optname $CMDLINE_SET; then
      eval $optname='$optval'
  fi
  esac
done

check_py_lib(){
    log check_py_lib "$@"
    lib=${1}
    check_cmd $python <<EOF
import sys
try:
    from distutils.sysconfig import get_python_lib
    for mythroot in '${mythroot}'.split(':'):
        sys.path.append(get_python_lib(prefix=mythroot +'${prefix}'))
        sys.path.append(get_python_lib(prefix=mythroot +'${prefix}/local'))
    sys.path.append(get_python_lib(prefix='${sysroot}${prefix}'))
    sys.path.append(get_python_lib(prefix='${sysroot}${prefix}/local'))
except:
    # python 3.12+
    from sysconfig import get_path
    for mroot in '${mythroot}'.split(':'):
        sys.path.append(mroot + get_path('purelib'))
    vars = {'base': '${sysroot}${prefix}', 'prefix': '${prefix}'}
    sys.path.append(get_path('purelib', vars=vars, expand=True))
    vars = {'base': '${sysroot}${prefix}', 'prefix': '${prefix}/local'}
    sys.path.append(get_path('purelib', vars=vars, expand=True))
try:
    import $lib
except:
    sys.exit(1)
sys.exit(0)
EOF
}

# set temporary file name
: ${TMPDIR:=$TEMPDIR}
: ${TMPDIR:=$TMP}
: ${TMPDIR:=/tmp}

if ! check_cmd type mktemp; then
    # simple replacement for missing mktemp
    # NOT SAFE FOR GENERAL USE
    mktemp(){
        echo "${2%XXX*}.${HOSTNAME}.${UID}.$$"
    }
fi

tmpfile(){
    tmp=$(mktemp -u "${TMPDIR}/mythplugins_conf.XXXXXXXX")$2 &&
    (set -C; exec > $tmp) 2>/dev/null ||
    die "Unable to create temporary file in $TMPDIR."
    append TMPFILES $tmp
    eval $1=$tmp
}

trap 'rm -f -- $TMPFILES' EXIT
trap exit HUP INT TERM

tmpfile TMPC  .c
tmpfile TMPE  $EXESUF
tmpfile TMPH  .h
tmpfile TMPO  .o
tmpfile TMPS  .S
tmpfile TMPV  .ver
tmpfile TMPSH .sh
tmpfile TMPCXX .cpp
tmpfile TMPASM .asm
tmpfile TMPMAK .mak
tmpfile TMPPRO .pro

unset -f mktemp

# make sure we can execute files in $TMPDIR
cat > $TMPSH 2>> $logfile <<EOF
#! /bin/sh
EOF
chmod +x $TMPSH >> $logfile 2>&1
if ! $TMPSH >> $logfile 2>&1; then
    cat <<EOF
Unable to create and execute files in $TMPDIR.  Set the TMPDIR environment
variable to another directory and make sure that it is not mounted noexec.
EOF
    die "Sanity test failed."
fi

# bring in mythtv config
mythrootlist=$(echo "$mythroot" | tr ":" " ")
for mythrootitem in $mythrootlist; do
    if [ -e $mythrootitem$prefix/include/mythtv/mythconfig.mak ] ; then
        break;
    fi
done
if [ -e $mythrootitem$prefix/include/mythtv/mythconfig.mak ] ; then
    rm mythconfig.mak 2> /dev/null
    cp $mythrootitem$prefix/include/mythtv/mythconfig.mak mythconfig.mak
    sed -i -e "s,^SYSROOT\=.*,SYSROOT=$mythrootitem,g" mythconfig.mak
elif [ -e $prefix/include/mythtv/mythconfig.mak ] ; then
    rm mythconfig.mak 2> /dev/null
    ${ln_s} $prefix/include/mythtv/mythconfig.mak mythconfig.mak
else
    echo "ERROR: mythconfig.mak not found at $prefix/include/mythtv/mythconfig.mak"
    echo "Did you make AND install MythTV first?"
    echo "Are you using the correct prefix ($prefix)?"
    echo "Bailing out!!"
    exit
fi

#Set build env variables according to mythconfig.mak
LD=$(cat mythconfig.mak | grep -e "^LD=")
LD=${LD#LD=}
CC=$(cat mythconfig.mak | grep -e "^CC=")
CC=${CC#CC=}
CFLAGS=$(cat mythconfig.mak | grep -e "^CFLAGS=")
CFLAGS=${CFLAGS#CFLAGS=}
CPPFLAGS=$(cat mythconfig.mak | grep -e "^CPPFLAGS=")
CPPFLAGS=${CPPFLAGS#CPPFLAGS=}
CXXFLAGS=$(cat mythconfig.mak | grep -e "^CXXFLAGS=")
CXXFLAGS=${CXXFLAGS#CXXFLAGS=}
LDFLAGS=$(cat mythconfig.mak | grep -e "^LDFLAGS=")
LDFLAGS="${LDFLAGS#LDFLAGS=} ${ELDFLAGS}"
CXX=$(cat mythconfig.mak | grep -e "^QMAKE_CXX=")
CXX=${CXX#QMAKE_CXX=}
ECXXFLAGS=$(cat mythconfig.mak | grep -e "^ECXXFLAGS=")
ECXXFLAGS=${ECXXFLAGS#ECXXFLAGS=}
CXXPPFLAGS=$(cat mythconfig.mak | grep -e "^CXXPPFLAGS=")
CXXPPFLAGS=${CXXPPFLAGS#CXXPPFLAGS=}
OPENGLV=$(cat mythconfig.mak | grep -e "^CONFIG_OPENGL_VIDEO=yes")
OPENGLES=$(cat mythconfig.mak | grep -e "^HAVE_GLES2_GL2_H=yes")
WEBKIT=$(cat mythconfig.mak | grep -e "^CONFIG_QTWEBKIT=yes")

EXTRALIBS=$(cat mythconfig.mak | grep -e "^EXTRALIBS=")
EXTRALIBS=${EXTRALIBS#EXTRALIBS=}

MYTHTV_VERSION_MAJMIN=$(cat mythconfig.mak | grep -e "^MYTHTV_VERSION_MAJMIN=")
MYTHTV_VERSION_MAJMIN=${MYTHTV_VERSION_MAJMIN#MYTHTV_VERSION_MAJMIN=}
MYTHTV_LIBVERSION=$(cat mythconfig.mak | grep -e "^MYTHTV_LIBVERSION=")
MYTHTV_LIBVERSION=${MYTHTV_LIBVERSION#MYTHTV_LIBVERSION=}

QT_MIN_VERSION=$(cat mythconfig.mak | grep -e "^QT_MIN_VERSION=")
QT_MIN_VERSION=${QT_MIN_VERSION#QT_MIN_VERSION=}
QT_MIN_VERSION_STR=$(cat mythconfig.mak | grep -e "^QT_MIN_VERSION_STR=")
QT_MIN_VERSION_STR=${QT_MIN_VERSION_STR#QT_MIN_VERSION_STR=}
if [ x"$QT_MIN_VERSION" = "x" ]; then
    die "Unknown minimum Qt version. Please make AND install MythTV first."
fi

version2num(){
    # accepts version as in 1.10.4 and turns it into 0x010A04 which can be compared as a number
    echo $1 | awk -F. '{ print ( ( $1 * 256 ) + $2 ) * 256 + $3; };'
}

# Use the qmake specified when building the mythtv directory.
qmake=$(cat mythconfig.mak | grep -e "^QMAKE=")
qmake=${qmake#QMAKE=}
qt_version_str=`$qmake -query QT_VERSION`
qt_version=$(version2num $qt_version_str)

# rely on what was discover during MythTV configure
if enabled opengl && test "$OPENGLV" != "CONFIG_OPENGL_VIDEO=yes"; then
    disable opengl
fi

if ! check_lib libexif/exif-data.h exif_loader_new -lexif ; then
    disable exif
fi

if enabled browser; then
    if test "$WEBKIT" != "CONFIG_QTWEBKIT=yes"; then
        disable browser
        echo "MythBrowser requires QtWebkit."
    fi
fi

if enabled netvision; then
    disable_netvision(){
        msg="$1"
        echo "MythNetvision requires the $msg"
        disable netvision
    }

    check_py_lib MythTV  || disable_netvision "MythTV Python bindings (MythTV)"
    check_py_lib pycurl  || disable_netvision "Python pycurl library (pycurl)"
    check_py_lib lxml    || disable_netvision "Python lxml library (lxml)"
    check_py_lib xml     || disable_netvision "Python XML library (xml)"
    check_py_lib urllib  || disable_netvision "Python URL library (urllib)"

    disabled netvision && echo "Disabling MythNetvision due to missing dependencies."
fi

if enabled weather; then
    check_pl_lib(){
        log check_pl_lib "$@"
        check_cmd perl -e "use ${1};"
    }

    disable_weather(){
        echo "MythWeather requires $1"
        disable weather
    }

    check_pl_lib "Date::Manip"  || disable_weather "Date::Manip"
    check_pl_lib "XML::Simple"  || disable_weather "XML::Simple"
    check_pl_lib "XML::XPath"  || disable_weather "XML::XPath"
    check_pl_lib "Image::Size"  || disable_weather "Image::Size"
    check_pl_lib "DateTime::Format::ISO8601" ||
        disable_weather "DateTime::Format::ISO8601"
    check_pl_lib "SOAP::Lite"  || disable_weather "SOAP::Lite"
    check_pl_lib "JSON"  || disable_weather "JSON"

    disabled weather && echo "Disabling MythWeather due to missing dependencies."
fi

# Test for dcraw utility
enabled dcraw && command -v dcraw 2>/dev/null > /dev/null || disable dcraw

if test x"$icc" != x ; then
    # Assume relative paths are for files in the standard icc directory.
    case "$icc" in
    /*)
    ;;
    *) icc=$sysroot/usr/share/color/icc/$icc
    ;;
    esac
    # Verify the presence of the file.
    if test ! -f "$icc" ; then
        icc=""
    fi
fi

libmusicbrainz_pc(){
    if $(pkg-config libmusicbrainz5cc --exists) ; then
        echo "libmusicbrainz5cc"
    else
        echo "libmusicbrainz5"
    fi
}

libcoverart_pc(){
    if $(pkg-config libcoverartcc --exists) ; then
        echo "libcoverartcc"
    else
        echo "libcoverart"
    fi
}

if enabled music ; then

    if ! check_lib vorbis/codec.h vorbis_info_init -lvorbis || ! check_lib vorbis/vorbisenc.h vorbis_encode_setup_vbr -lvorbisenc -lvorbis -logg ; then
        disable vorbis
        echo "MythMusic requires vorbis."
    fi

    if ! check_lib FLAC/all.h FLAC__stream_decoder_new -lFLAC ;then
        disable flac
        echo "MythMusic requires FLAC."
    fi

    if enabled cdio ; then
        if ! check_lib cdio/cdio.h cdio_open -lcdio ; then
            disable cdio
        # cd paranoia moved to sub-directory on latest debian based releases
        elif check_lib cdio/cdda.h cdio_cddap_open -lcdio_cdda && \
             check_lib cdio/paranoia.h cdio_paranoia_init -lcdio_paranoia ; then
            enable cdio; # nop
        elif check_lib cdio/paranoia/cdda.h cdio_cddap_open -lcdio_cdda && \
             check_lib cdio/paranoia/paranoia.h cdio_paranoia_init -lcdio_paranoia ; then
            enable cdio; # nop
        else
            disable cdio
        fi
    fi
    if enabled musicbrainz ; then
        if ! check_lib discid/discid.h discid_new -ldiscid ; then
            disable musicbrainz
            echo "MusicBrainz support requires libdiscid."
        elif ! check_lib_cxx2 musicbrainz5/Query.h MusicBrainz5::CQuery\(\"test\"\) $(pkg-config $(libmusicbrainz_pc) --cflags --libs); then
            disable musicbrainz
            echo "MusicBrainz support requires libmusicbrainz5."
        elif ! check_lib_cxx2 coverart/CoverArt.h CoverArtArchive::CCoverArt\(\"test\"\) $(pkg-config $(libcoverart_pc) --cflags --libs); then
            disable musicbrainz
            echo "MusicBrainz support requires libcoverart."
        else
            enable musicbrainz; # nop
        fi
    fi

    if ! check_lib lame/lame.h lame_init -lmp3lame ; then
        disable mp3lame
        echo "MythMusic requires the LAME mp3 encoder."
    fi

    if disabled vorbis || disabled flac || disabled mp3lame ;  then
        echo "Disabling MythMusic due to missing dependencies."
        disable music
    fi
fi

if enabled zoneminder ; then
    if ! check_header mysql/mysql.h ; then
        echo "Disabling MythZMServer. Requires mysql/mysql.h header"
        disable zmserver
    fi
fi

echo "Creating settings2.pro"
echo "MYTHTV_VERSION_MAJMIN: ${MYTHTV_VERSION_MAJMIN}"
echo "MYTHTV_LIBVERSION: ${MYTHTV_LIBVERSION}"
sed -e "s/@MYTHTV_VERSION_MAJMIN@/${MYTHTV_VERSION_MAJMIN}/g" \
    -e "s/@MYTHTV_LIBVERSION@/${MYTHTV_LIBVERSION}/g" \
    settings2.pro.in > $TMPPRO
cp_if_changed $TMPPRO settings2.pro

###########################################################
#                                                         #
#  Top level mythplugin.pro file creation                 #
#  (ie. which plugins to build)                           #
#                                                         #
###########################################################

echo ""
echo "Configuration settings: "
echo " "
echo "        Qt minimum     $QT_MIN_VERSION_STR"
echo "        Qt installed   `$qmake -query QT_VERSION`"
echo "        qmake          $(which $qmake)"
echo " "

echo "#" > ./config.pro
echo "#    Automatically generated by mythplugin configure" >> ./config.pro
echo "#    (manual modifications will be overwritten)" >> ./config.pro
echo "#" >> ./config.pro

if enabled archive ; then
  echo "        MythArchive    plugin will be built"
  echo "SUBDIRS += mytharchive" >> ./config.pro
else
  echo "        MythArchive    plugin will not be built"
fi

if enabled browser ; then
  echo "        MythBrowser    plugin will be built"
  echo "SUBDIRS += mythbrowser" >> ./config.pro
else
  echo "        MythBrowser    plugin will not be built"
fi

if enabled game ; then
  echo "        MythGame       plugin will be built"
  echo "SUBDIRS += mythgame" >> ./config.pro
else
  echo "        MythGame       plugin will not be built"
fi

if enabled music ; then
  echo "        MythMusic      plugin will be built"
  echo "SUBDIRS += mythmusic" >> ./config.pro
else
  echo "        MythMusic      plugin will not be built"
fi

if enabled netvision ; then
  echo "        MythNetvision  plugin will be built"
  echo "SUBDIRS += mythnetvision" >> ./config.pro
else
  echo "        MythNetvision  plugin will not be built"
fi

if enabled news ; then
  echo "        MythNews       plugin will be built"
  echo "SUBDIRS += mythnews" >> ./config.pro
else
  echo "        MythNews       plugin will not be built"
fi

if enabled weather ; then
  echo "        MythWeather    plugin will be built"
  echo "SUBDIRS += mythweather" >> ./config.pro
else
  echo "        MythWeather    plugin will not be built"
fi

if enabled zoneminder ; then
  echo "        MythZoneMinder plugin will be built"
  echo "SUBDIRS += mythzoneminder" >> ./config.pro
  echo "#" > ./mythzoneminder/mythzoneminder.pro
  echo "#    Automatically generated by configure - modify only under penalty of death" >> ./mythzoneminder/mythzoneminder.pro
  echo "#" >> ./mythzoneminder/mythzoneminder.pro
  echo "TEMPLATE = subdirs" >> ./mythzoneminder/mythzoneminder.pro
  echo "" >> ./mythzoneminder/mythzoneminder.pro
  echo "# Directories" >> ./mythzoneminder/mythzoneminder.pro
  if enabled zmserver ; then
    echo "SUBDIRS = mythzoneminder mythzmserver theme i18n" >> ./mythzoneminder/mythzoneminder.pro
  else
    echo "SUBDIRS = mythzoneminder theme i18n" >> ./mythzoneminder/mythzoneminder.pro
  fi
else
  echo "        MythZoneMinder plugin will not be built"
fi

###########################################################
#                                                         #
#  MythMusic related configuration options                #
#                                                         #
###########################################################

if enabled music ; then

    echo "/*" >  ./mythmusic/mythmusic/config.h
    echo "    Automatically generated by configure - do not modify" >> ./mythmusic/mythmusic/config.h
    echo "*/" >> ./mythmusic/mythmusic/config.h
    echo "/* NOLINTBEGIN(cppcoreguidelines-macro-usage) */" >> ./mythmusic/mythmusic/config.h

    echo "#" > ./mythmusic/mythmusic/config.pro
    echo "#    Automatically generated by configure - modify only under penalty of death" >> ./mythmusic/mythmusic/config.pro
    echo "#" >> ./mythmusic/mythmusic/config.pro

    if enabled cdio ; then
      echo "#define HAVE_CDIO 1" >> ./mythmusic/mythmusic/config.h
      echo "CONFIG += cdio" >> ./mythmusic/mythmusic/config.pro
      echo "        libcdio        support will be included in MythMusic"
      if enabled musicbrainz ; then
        echo "#define HAVE_MUSICBRAINZ 1" >> ./mythmusic/mythmusic/config.h
        echo "CONFIG += link_pkgconfig" >> ./mythmusic/mythmusic/config.pro
        echo "CONFIG += musicbrainz" >> ./mythmusic/mythmusic/config.pro
        echo "PKGCONFIG += libdiscid $(libmusicbrainz_pc) $(libcoverart_pc)" >> ./mythmusic/mythmusic/config.pro
        echo "        musicbrainz    support will be included in MythMusic"
      else
        echo "#undef  HAVE_MUSICBRAINZ" >> ./mythmusic/mythmusic/config.h
        echo "        musicbrainz    support will not be included in MythMusic"
      fi
    else
      echo "#undef  HAVE_CDIO" >> ./mythmusic/mythmusic/config.h
      echo "        libcdio        support will not be included in MythMusic"
    fi
    echo "/* NOLINTEND(cppcoreguidelines-macro-usage) */" >> ./mythmusic/mythmusic/config.h
fi

###########################################################
#                                                         #
#  MythZoneMinder related configuration options           #
#                                                         #
###########################################################

if enabled zmserver ; then
  echo "        mythzmserver   support will be included in MythZoneMinder"
else
  echo "        mythzmserver   support will not be included in MythZoneMinder"
fi

###########################################################
#                                                         #
#   An extra directory and Makefile for cleanup rules     #
#                                                         #
###########################################################

mkdir -p cleanup
cat << END_CLEANUP > cleanup/Makefile
check:
all:
clean:
distclean:
install:
qmake_all:
	\$(NONE)

# Hack to remove some empty directories that qmake doesn't create rules for
uninstall:
	-rmdir \$(INSTALL_ROOT)/$prefix/share/mythtv/*
	-rmdir \$(INSTALL_ROOT)/$prefix/share/mythtv

END_CLEANUP

echo >> ./config.pro
echo "# clean up for 'make uninstall'. This must be last" >> ./config.pro
echo "SUBDIRS += cleanup " >> ./config.pro

###########################################################

echo ""

${qmake} QMAKE=$(which ${qmake}) mythplugins.pro
