set -eu

cat <<'END'
# File generated by confninja.sh; DO NOT EDIT!

ninja_required_version = 1.11

rule cargo
  command = cd $extractrs_dir && cargo build $rust_mode && cp $rs_bin ../bin
  description = CARGO $out

rule scss
  command = sass --load-path=node_modules/ $scss_mode $in $out
  description = SCSS $out

rule bundle
  command = esbuild --log-level=warning --bundle $bundle_mode --outfile=$out $in
  description = BUNDLE $out

rule instexec
  command = install $in $out
  description = INSTEXEC $out

rule instdata
  command = install -m0644 $in $out
  description = INSTDATA $out

rule perltest
  command = prove -l t/
  description = PERLTEST

rule perlman
  command = pod2man $in >$out
  description = POD2MAN $out

rule configure
  command = ./configure $confargs
  description = CONFIGURE
  generator = 1

build test: perltest extract
END

# CONFIGURE

echo "confargs = $CONFARGS"
echo "build build.ninja lib/JWebmail/Config.pm: configure configure confninja.sh lib/JWebmail/Config.pm.in"

case "$JWM_MODE" in
    (development)
        echo 'bundle_mode = --sourcemap'
        echo 'rs_bin = target/debug/jwebmail-extract'
        ;;
    (production)
        echo 'bundle_mode = --minify'
        echo 'sass_mode = --style=compress --no-source-maps'
        echo 'rust_mode = --profile release-small --target x86_64-unknown-linux-musl -q'
        echo 'rs_bin = target/x86_64-unknown-linux-musl/release-small/jwebmail-extract'
        ;;
esac

# EXTRACTRS

case "$MAILDIR_EXTRACTOR_NAME" in
    (extractrs)
        echo "extractrs_dir = $JWM_EXTRACTRS_DIR"
        echo -n 'build bin/jwebmail-extract: cargo $extractrs_dir/Cargo.toml'
        FILES_RS='arguments.rs cmd.rs error.rs main.rs rfc822.rs cmd/count.rs cmd/folders.rs cmd/list.rs cmd/raw.rs'
        for f in $FILES_RS
        do
            echo -n " \$extractrs_dir/src/$f"
        done
        echo
        echo "build extract: phony bin/jwebmail-extract"
        ;;
    (extractpl) echo "build extract: phony script/extract.pl";;
    (extractpy) echo "build extract: phony script/extract.py";;
esac

# PUBLIC

FILES_JS='src/displayheaders.js src/rendermail.js'
if [ "$LOGIN_SCHEME" = cram_md5 ]
then FILES_JS="$FILES_JS src/login_cram.js"
fi

PUBLIC=
for f in $FILES_JS
do case "$JWM_MODE" in
    (development)
        echo "build public/$f | public/$f.map: bundle $f"
        PUBLIC="$PUBLIC public/$f public/$f.map"
        ;;
    (production)
        echo "build public/$f: bundle $f"
        PUBLIC="$PUBLIC public/$f"
        ;;
esac
done

for f in my_bulma
do case "$JWM_MODE" in
    (development)
        echo "build public/css/$f.css | public/css/$f.css.map: scss scss/$f.scss"
        PUBLIC="$PUBLIC public/css/$f.css public/css/$f.css.map"
        ;;
    (production)
        echo "build public/css/$f.css: scss scss/$f.scss"
        PUBLIC="$PUBLIC public/css/$f.css"
        ;;
esac
done

echo "build public: phony $PUBLIC"

# INSTALL

install_file () { echo "build \$destdir/$2/$4: $1 $3/$4"; ALL_INST="$ALL_INST \$destdir/$2/$4"; }

echo "destdir = $DESTDIR"

echo "prefix = $PREFIX"
echo "bindir = \$prefix/bin"
echo "scriptdir = \$prefix/bin"
echo "perldir = $PERL_PATH"
if [ "$PREFIX" = usr ]
then echo "confdir = \$prefix/../etc"
else echo "confdir = \$prefix/etc"
fi
echo "datadir = \$prefix/share/jwebmail"
echo "docdir = \$prefix/share/doc/jwebmail"
echo "man3dir = \$prefix/share/man/man3"

ALL_INST=

case "$MAILDIR_EXTRACTOR_NAME" in
    (extractrs) install_file instexec \$bindir bin jwebmail-extract;;
    (extractpy) install_file instexec \$scriptdir script extract.py;;
    (extractpl) install_file instexec \$scriptdir script extract.pl;;
esac

install_file instexec \$scriptdir script jwebmail
install_file instexec \$scriptdir script testauthenticator.pl

while read f
do install_file instdata \$perldir lib "$f"
done <<'END'
JWebmail/Controller/Webmail.pm
JWebmail/I18N/de.pm
JWebmail/I18N/en.pm
JWebmail/Model/ReadMails/QMailAuthuser.pm
JWebmail/Model/ReadMails/Role.pm
JWebmail/Model/WriteMails.pm
JWebmail/Plugin/I18N2/INI.pm
JWebmail/Plugin/I18N2/Maketext.pm
JWebmail/Plugin/I18N2/Role.pm
JWebmail/Plugin/I18N2.pm
JWebmail/Plugin/INIConfig.pm
JWebmail/Plugin/Paginate.pm
JWebmail/Plugin/ServerSideSessionData.pm
JWebmail/Plugin/TOMLConfig.pm
JWebmail/View/RenderMail.pm
JWebmail/View/Webmail.pm
JWebmail/Config.pm
JWebmail/I18N.pm
JWebmail.pm
END

while read f
do
    manname="$(echo -n "${f%.pm}"| sed 's!/!$:$:!g').3perl"
    echo "build \$destdir/\$man3dir/$manname: perlman lib/$f"
    ALL_INST="$ALL_INST \$destdir/\$man3dir/$manname"
done <<'END'
JWebmail/Controller/Webmail.pm
JWebmail/I18N/de.pm
JWebmail/Model/ReadMails/QMailAuthuser.pm
JWebmail/Model/ReadMails/Role.pm
JWebmail/Model/WriteMails.pm
JWebmail/Plugin/I18N2/Role.pm
JWebmail/Plugin/I18N2.pm
JWebmail/Plugin/INIConfig.pm
JWebmail/Plugin/Paginate.pm
JWebmail/Plugin/ServerSideSessionData.pm
JWebmail/Plugin/TOMLConfig.pm
JWebmail/View/RenderMail.pm
JWebmail/View/Webmail.pm
JWebmail/Config.pm
JWebmail/I18N.pm
JWebmail.pm
END
#JWebmail/I18N/en.pm
#JWebmail/Plugin/I18N2/Maketext.pm
#JWebmail/Plugin/I18N2/INI.pm

install_file instdata \$datadir . public/css/my_bulma.css
[ "$JWM_MODE" = development ] && install_file instdata \$datadir . public/css/my_bulma.css.map
install_file instdata \$datadir . public/src/displayheaders.js
[ "$JWM_MODE" = development ] && install_file instdata \$datadir . public/src/displayheaders.js.map
install_file instdata \$datadir . public/src/rendermail.js
[ "$JWM_MODE" = development ] && install_file instdata \$datadir . public/src/rendermail.js.map

if [ "$LOGIN_SCHEME" = cram_md5 ]
then
    install_file instdata \$datadir . public/src/login_cram.js
    [ "$JWM_MODE" = development ] && install_file instdata \$datadir . public/src/login_cram.js.map
fi

while read f
do install_file instdata \$datadir . "$f"
done <<'END'
templates/displayheaders/_bot_nav.html.ep
templates/displayheaders/_folders.html.ep
templates/displayheaders/_main_table.html.ep
templates/displayheaders/_pagination1.html.ep
templates/displayheaders/_pagination2.html.ep
templates/displayheaders/_pagination3.html.ep
templates/displayheaders/_top_nav.html.ep
templates/layouts/mainlayout.html.ep
templates/webmail/about.html.ep
templates/webmail/displayheaders.html.ep
templates/webmail/login.html.ep
templates/webmail/readmail.html.ep
templates/webmail/writemail.html.ep
templates/exception_.html.ep
templates/not_found.html.ep
END

install_file instdata \$confdir . jwebmail.development.toml
install_file instdata \$confdir . jwebmail.production.toml

for f in CHANGES.md LICENSE README.md
do install_file instdata \$docdir . "$f"
done

if [ "$JWM_INIT" = systemd ]
then
    case "$PREFIX" in
        (usr)       install_file instdata etc/systemd/system install jwebmail.service;;
        (usr/local) install_file instdata usr/local/lib/systemd/system install jwebmail.service;;
        (*)         echo 'unknown prefix' >&2; exit 1;;
    esac
    install_file instdata etc/sysusers.d install jwebmail-sysusers.conf
fi

echo "build install: phony $ALL_INST"

# DEFAULT

echo "default extract public"