#!/usr/bin/env bash
read -r -d '' usage <<'EOF'
go.sh

Purpose:
Build and push the various kinds of reference.wolfram.com documentation.
Not for use in RE build systems.
This is just a handy tool for your local test builds.

Usage:
./go <args>

Arguments:
  -d: delete and recreate the local build directories before building

======================================================================
what to build (mutually exclusive but that's not implemented yet)

  -s: "s"ource notebooks -> in-product notebooks
      ant -buildfile MakeMathematicaNotebooks.xml -propertyfile ./Properties/User/billw-MakeMathematicaNotebooks.properties

  -i: "i"n-product notebooks -> "w"ebpages
      ant -buildfile MakeMathematicaHTML.xml -propertyfile ./Properties/User/billw-MakeMathematicaHTML.properties

  -p: source notebooks -> webpages (typically used for "p"ackage documentation)
      ant -buildfile html.xml -propertyfile ./Properties/User/billw-html.properties

  -n: SourceInformation notebooks -> webpages (deployed to language/"n"otes)
      ant -buildfile SourceInformationNotes.xml -propertyfile ./Properties/User/billw-SourceInformation.properties

  -f: "f"eatureset notebooks -> webpages
      ant -buildfile FeaturesetPages.xml -propertyfile ./Properties/User/billw-FeaturesetPages.properties

  -g: set debu"g" to True

======================================================================
post-processing

  -l: run c"l"oudconvert to produce cloudplatform pages

======================================================================
where to put the output

  -c: copy: after building, copy to webdev4.  If applicable, copy all of
      language and cloudplatform if available.  
  -x: like -c, but copies featureset pages to the test location
      at https://www.devel.wolfram.com/test/core to avoid replacing
      officially-built files with my experiments.
  -z: like -c, but copies featureset pages to the test location
      at https://www.devel.wolfram.com/test/featureset2 to avoid replacing
      officially-built files with my experiments.

  -b: copy to webdev4:{language,cloudplatform}

  -t: copy to webtst4:{language,cloudplatform}

  -e: e for ExcelEntities: after building, push to an excel-specific dir using /Users/billw/wri/docs/bin/push-exceltest.sh

  -w: w for wsm: after building, copy output to a local WSM product for testing

EOF

delete="false"          # -d

source2nb="false"       # -s
ip2webpages="false"     # -i
source2webpages="false" # -p
notes2webpages="false"  # -n
featureset="false"      # -f
debug="false"           # -g

makecloud="false"       # -l (ell)

copy="false"            # -c
copytest="false"        # -x
copytest2="false"        # -z
copywebdev4="false"     # -b
copywebtst4="false"     # -t
copyexcel="false"       # -e
copywsm="false"         # -w

while getopts 'dsipnfglcbtewhxz' flag
do
    case "${flag}" in
        d) export delete="true";;
           #
        s) export source2nb="true";;
        i) export ip2webpages="true";;
        p) export source2webpages="true";;
        n) export notes2webpages="true";;
        f) export featureset="true";;
        g) export debug="true";;
           #
        l) export makecloud="true";;
           #
        c) export copy="true";;
        x) export copytest="true";;
        z) export copytest2="true";;
        b) export copywebdev4="true";;
        t) export copywebtst4="true";;
        e) export copyexcel="true";;
        w) export copywsm="true";;
           #
        h) echo "$usage" >&2
           exit 1 ;;
        *) echo "$usage" >&2
           exit 1 ;;
    esac
done

# ----------------------------------------------------------------------

if [ "$delete" == "true" ]; then
    if [ "$featureset" == "true" ]; then
        echo "removing previous featureset build products..." && rm -rf ./featuresetpages/ && mkdir -p ./featuresetpages/logs
    else
        echo "clean.sh: removing previous build products" && ./clean.sh
    fi
fi

# ----------------------------------------------------------------------

# ,----[ ~/bin/mdate ]
# | #!/Applications/Mathematica.app/Contents/MacOS/WolframScript -script
# | (* 2021-12-12T17.45.03 *)
# | Print[DateString[{"Year", "-", "Month", "-", "Day", "T", "Hour", ".", "Minute", ".", "Second"}]]
# `----

if [ "$source2nb" == "true" ]; then
    ant -buildfile ./MakeMathematicaNotebooks.xml -propertyfile ./Properties/User/billw-MakeMathematicaNotebooks.properties 2>&1 | tee /tmp/build-$(mdate).log
fi

if [ "$ip2webpages" == "true" ]; then
    ant -buildfile ./MakeMathematicaHTML.xml -propertyfile ./Properties/User/billw-MakeMathematicaHTML.properties 2>&1 | tee /tmp/build-$(mdate).log
fi

if [ "$source2webpages" == "true" ]; then
    ant -buildfile html.xml -propertyfile ./Properties/User/billw-html.properties 2>&1 | tee /tmp/build-$(mdate).log
fi

if [ "$notes2webpages" == "true" ]; then
    ant -buildfile SourceInformationNotes.xml -propertyfile ./Properties/User/billw-SourceInformation.properties 2>&1 | tee /tmp/build-$(mdate).log
fi

if [ "$featureset" == "true" ]; then
    echo "running ant..."
    if [ "$debug" == "true" ]; then
        ant -buildfile FeaturesetPages.xml -propertyfile ./Properties/User/billw-FeaturesetPages.properties -Ddebug="True" 2>&1 | tee ./featuresetpages/logs/build-$(mdate).log
    else
        ant -buildfile FeaturesetPages.xml -propertyfile ./Properties/User/billw-FeaturesetPages.properties -Ddebug="False" 2>&1 | tee ./featuresetpages/logs/build-$(mdate).log
    fi
fi

# ----------------------------------------------------------------------

if [ "$makecloud" == "true" ]; then
    /Users/billw/wri/docs/bin/makecloud.sh
fi

# ----------------------------------------------------------------------

if [ "$copy" = "true" ]; then
    if [ "$featureset" == "true" ]; then
        echo "copying featureset output to my test location" && \
            rsync -av --exclude 'logs' --exclude '*.html' /Users/billw/git/DocumentationBuild/SystemFiles/ant/Build/featuresetpages/ webdev4:/www/sites/www.wolfram.com/htdocs/test/core && \
            echo "https://www.devel.wolfram.com/core/"
    else
        /Users/billw/wri/docs/bin/push-webdev4-test.sh
    fi
fi

if [ "$copytest" = "true" ]; then
    rsync -av /Users/billw/git/DocumentationBuild/SystemFiles/ant/Build/featuresetpages/ webdev4:/www/sites/www.wolfram.com/htdocs/test/core
    echo "https://www.devel.wolfram.com/test/core/"
fi

if [ "$copytest2" = "true" ]; then
    rsync -av /Users/billw/git/DocumentationBuild/SystemFiles/ant/Build/featuresetpages/ webdev4:/www/sites/www.wolfram.com/htdocs/test/core2
    echo "https://www.devel.wolfram.com/test/core2/"
fi

if [ "$copywebdev4" = "true" ]; then
    /Users/billw/wri/docs/bin/push-webdev4.sh
fi

if [ "$copywebtst4" = "true" ]; then
    /Users/billw/wri/docs/bin/push-webtst4.sh
fi

if [ "$copyexcel" = "true" ]; then
    /Users/billw/wri/docs/bin/push-exceltest.sh
fi

if [ "$copywsm" = "true" ]; then
    /Users/billw/git/documentationbuild/SystemFiles/ant/Build/wsm.sh
fi

# /Users/billw/git/documentationbuild/SystemFiles/ant/Build/wsm.sh:
# #!/usr/bin/env bash
#
# # copy wsm output files to a wsm installation for in-product testing
#
# echo "copying fresh output to /Users/billw/wri/Mathematica/SystemModeler-12.2-b7485.app"
#
# echo "updating /Users/billw/wri/Mathematica/SystemModeler-12.2-b7485.app/Contents/docs/English/GettingStarted"
# rsync -a --delete /Users/billw/wri/docs/www/language/GettingStarted/ /Users/billw/wri/Mathematica/SystemModeler-12.2-b7485.app/Contents/docs/English/GettingStarted
#
# echo "updating /Users/billw/wri/Mathematica/SystemModeler-12.2-b7485.app/Contents/docs/English/UserGuide"
# rsync -a --delete /Users/billw/wri/docs/www/language/UserGuide/ /Users/billw/wri/Mathematica/SystemModeler-12.2-b7485.app/Contents/docs/English/UserGuide
#
# echo "updating /Users/billw/wri/Mathematica/SystemModeler-12.2-b7485.app/Contents/docs/English/WhatsNew"
# rsync -a --delete /Users/billw/wri/docs/www/language/WhatsNew/ /Users/billw/wri/Mathematica/SystemModeler-12.2-b7485.app/Contents/docs/English/WhatsNew
#
# echo "updating /Users/billw/wri/Mathematica/SystemModeler-12.2-b7485.app/Contents/docs/English/Resources/wsm-assets"
# rsync -a --delete /Users/billw/wri/docs/www/language/html/wsm-assets/ /Users/billw/wri/Mathematica/SystemModeler-12.2-b7485.app/Contents/docs/English/Resources/wsm-assets
#
# echo "done"

# /Users/billw/Dropbox/bin/killmath9