#!/usr/bin/env bash

###############################################################################
# PatchView
#
# Splits an ASCII STL into individual patch STL files and opens them
# automatically in ParaView.
###############################################################################

set -e

###############################################################################
# Library directory
###############################################################################

LIB_DIR="$HOME/.meshDictCreator/lib"

if [[ ! -d "$LIB_DIR" ]]; then
    echo
    echo "PatchView library not found."
    echo
    echo "Please reinstall meshDictCreator."
    exit 1
fi

###############################################################################
# Arguments
###############################################################################

stl="$1"

if [[ -z "$stl" ]]; then
    echo "Usage:"
    echo "    patchview path/to/model.stl"
    exit 2
fi

###############################################################################
# Windows working directory
###############################################################################

WDIR_WIN='C:\pvtmp'
WDIR_WSL='/mnt/c/pvtmp'

mkdir -p "$WDIR_WSL/patchview" || {
    echo "Cannot create $WDIR_WSL"
    exit 1
}

###############################################################################
# Input / Output
###############################################################################

in="$(readlink -f "$stl")"

if [[ ! -f "$in" ]]; then
    echo
    echo "Input STL not found:"
    echo "$stl"
    exit 1
fi

base_dir="$(dirname "$in")"
out_dir="$base_dir/patchview"

mkdir -p "$out_dir"

###############################################################################
# Split STL
###############################################################################

python3 "$LIB_DIR/patchview_split.py" "$in" "$out_dir" || {
    echo
    echo "STL splitting failed."
    exit 1
}

###############################################################################
# Copy split STL files to Windows
###############################################################################

rm -f "$WDIR_WSL/patchview/"*.stl

cp "$out_dir"/*.stl "$WDIR_WSL/patchview/" || {
    echo
    echo "Failed to copy STL files."
    exit 1
}

###############################################################################
# ParaView executables
###############################################################################

PS="/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe"

PVBIN='C:\Program Files\ParaView 5.13.0\bin'

PVPY="$PVBIN\pvpython.exe"
PVEX="$PVBIN\paraview.exe"

###############################################################################
# Windows paths
###############################################################################

BUILD_SCRIPT_WIN="$(wslpath -w "$LIB_DIR/patchview_build.py")"

SRC_WIN="$WDIR_WIN\patchview"

STATE_WIN="$WDIR_WIN\patchview_state.pvsm"

###############################################################################
# Build ParaView state
###############################################################################

"$PS" -NoLogo -NoProfile -Command \
"Start-Process -FilePath '$PVPY' -ArgumentList @('$BUILD_SCRIPT_WIN','$SRC_WIN','$STATE_WIN') -WorkingDirectory '$WDIR_WIN' -Wait"

###############################################################################
# Launch ParaView
###############################################################################

"$PS" -NoLogo -NoProfile -Command \
"Start-Process -FilePath '$PVEX' -ArgumentList @('--state=$STATE_WIN') -WorkingDirectory '$WDIR_WIN'"

exit 0