#!/bin/sh
#
# Script:
# Without web server, run PHP script and view output and errors in web browser
#
# by Apollia - http://apollia.com/
#
# License: GNU Affero GPL 3.0
# https://www.gnu.org/licenses
#
#
# This script outputs three temporary HTML files - one with only STDOUT,
# only with only STDERR, and one with both STDOUT and STDERR.
#
#
#------------------------------------------------------------------------------



#export SAXONC_HOME="/root/apbin/APSK-GLMP-GTK/Saxon-C/v1.0.2-HEC/" 
# 16:16:03 01/28/2017.  A line which isn't needed if you don't have Saxon/C.
#
# Had to do that to make Saxon/C work on my Lighthouse 64 Puppy Linux 6.02 Beta 2 system.
#
# 
# https://saxonica.plan.io/issues/2907
#
# http://www.saxonica.com/saxon-c/index.xml

html_with_only_stdout="/tmp/temp-php-output---only-stdout.html"
html_with_only_stderr="/tmp/temp-php-output---only-stderr.html"
html_with_both_stdout_and_stderr="/tmp/temp-php-output---both-stdout-and-stderr.html"

web_browser_filepath="palemoon"


top_line="Arg 1: $1<p>\n\n"
# 01:07:05 02/04/2017.  Puts Argument 1 as the top line in the output HTML files.
# (Argument 1 should be a filepath to a PHP file.)

echo -e "$top_line" > "$html_with_only_stdout"
echo -e "$top_line" > "$html_with_only_stderr"
echo -e "$top_line" > "$html_with_both_stdout_and_stderr"
# 01:00:36 02/04/2017.  Overwrites whatever was already in those files.



# The following line is a slightly modified version of code from:
# http://unix.stackexchange.com/a/54974
# "How to redirect stderr,out to different files and also display in terminal?"
# http://unix.stackexchange.com/questions/6430/how-to-redirect-stderr-out-to-different-files-and-also-display-in-terminal

((php-cgi "$1" | tee -a "$html_with_only_stdout") 3>&1 1>&2 2>&3 | tee -a "$html_with_only_stderr") &>> "$html_with_both_stdout_and_stderr"


"$web_browser_filepath" "file://$html_with_both_stdout_and_stderr"

"$web_browser_filepath" "file://$html_with_only_stderr"

"$web_browser_filepath" "file://$html_with_only_stdout"