# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
umask 044

if [ "$TZ" == "" ]; then
	export TZ="America/Regina"
fi

if [ "$BASH" == "" ]; then
	if [ -x /bin/bash ]; then
		export BASH="/bin/bash"
	elif [ -x /usr/bin/bash ]; then
		export BASH="/usr/bin/bash"
	else
		HAS_BASH=`which bash`
		if [ "$HAS_BASH" != "" ]; then
			export BASH="bash"
		fi
		unset HAS_BASH
	fi
fi

# we need names without the spaces
C_PROGRAMS_EXISTS=`ls -1 $HOME | grep c-programs`
if [ "$C_PROGRAMS_EXISTS" == "" ]; then
	ln -s /mnt/c/Program\ Files $HOME/c-programs
fi
unset C_PROGRAMS_EXISTS

C_PROGRAMS_X86_EXISTS=`ls -1 $HOME | grep c-programs-x86`
if [ "$C_PROGRAMS_X86_EXISTS" == "" ]; then
	ln -s /mnt/c/Program\ Files\ \(x86\) $HOME/c-programs-x86
fi
unset C_PROGRAMS_EXISTS

# only add $HOME/bin to the PATH if it exists
if [ -d "$HOME/bin" ]; then
	HOME_BIN_IN_PATH=`echo $PATH | grep "$HOME/bin"`
	if [ "$HOME_BIN_IN_PATH" == "" ]; then
		export PATH="$HOME/bin:$PATH"
	fi
fi

# Check if cmd.com is available
if [ "$CMD" == "" ]; then
	CMD_AVAILABLE=`which cmd.exe`
	if [ "$CMD_AVAILABLE" != "" ]; then
		export CMD="cmd.exe"
		alias cmd="$CMD"
	fi
	unset CMD_AVAILABLE
fi
	
# Check if Windows PowerShell is available
if [ "$POWERSHELL" == "" ]; then
	POWERSHELL_AVAILABLE=`which powershell.exe`
	if [ "$POWERSHELL_AVAILABLE" != "" ]; then
		export POWERSHELL="powershell.exe"
	fi
	unset POWERSHELL_AVAILABLE
fi

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
		. "$HOME/.bashrc"
    fi
fi

# Make sure we have access to a version of git, preferring the Windows version
if [ "$GIT" == "" ]; then
	GIT_AVAILABLE=`which git.exe`
	if [ "$GIT_AVAILABLE" != "" ]; then
		export GIT="git.exe"
		alias git="$GIT"
	else
		GIT_AVAILABLE=`which git`
		if [ "$GIT_AVAILABLE" == "" ]; then
			echo "SUDO apt-get install git..."
			sudo apt-get install git
		fi
		GIT_AVAILABLE=`which git`
		if ["$GIT_AVAILABLE" == "" ]; then
			echo "ERROR Installation of git failed"
			unalias git
		else
			export GIT="git"
			unalias git
		fi
	fi
	unset GIT_AVAILABLE
fi

# Force Windows Git implementation
export GIT_EDITOR="$GVIM"

# $GIT config --global core.editor "$GVIM"
$GIT config --global core.editor "vim"
$GIT config --global user.name "Your Name"
$GIT config --global user.email "your@email.address"
$GIT config --global user.signingkey XXXXXXXX
$GIT config --global user.gpgsign true

# If ant is installed locally, use that version
ANT_IS_LOCAL=`which ant | grep usr/bin`
if [ "$ANT_IS_LOCAL" != "" ]; then
	export ANT="ant"
	unset ANT_HOME
	unset ANT_BINDIR
	unset ANT_LIB
elif [ -d /mnt/d/msscodefactory/bin/apache-ant-1.10.1 ]; then
	export ANT_BINDIR=/mnt/d/msscodefactory/bin/apache-ant-1.10.1/bin
	export ANT_HOME="D:/msscodefactory/bin/apache-ant-1.10.1"
	export ANT="ant"
	export ANT_LIB="$ANT_HOME/lib"
else
	ANT_AVAILABLE=`which ant`
	if [ "$ANT_AVAILABLE" == "" ]; then
		echo "SUDO apt-get install ant"
		sudo apt-get install ant
	fi
	ANT_AVAILABLE=`which ant`
	if [ "$ANT_AVAILABLE" == "" ]; then
		echo "ERROR: Installation of ant failed"
		unset ANT
	else
		export ANT="ant"
	fi
	unset ANT_AVAILABLE
fi
unset ANT_IS_LOCAL

# Use the WSL OpenJDK 9 JRE and JDK for Java
export JAVAC="javac"
export JAVA="java"
unset JAVAEXE
unset JAVACEXE

# For the Windows Oracle JRE and JDK, use the following
#export JAVA="java.exe"
#export JAVAC="javac.exe"
#export JAVAEXE="java.exe"
#export JAVACEXE="javac.exe"

# Toggle between the Windows and Linux JRE 9 implementations
if [ "$JAVA_HOME" == "" ]; then
	if [ "$JAVA" == "java.exe" ]; then
		JAVAEXE_FOUND=`which java.exe`
		if [ "$JAVAEXE_FOUND" != "" ]; then
			export JAVA_HOME="$HOME/c-programs/Java/jre-9.0.4"
		else
			export JAVA_HOME="$HOME/c-programs/Java/jre-9.0.4"
			JRE_IN_PATH=`echo $PATH | grep Java/jre-9.0.4/bin`
			if [ "$JRE_IN_PATH" == "" ]; then
				echo "Adding $JAVA_HOME/bin to PATH..."
				export PATH="$JAVA_HOME/bin:$PATH"
			fi
			unset JRE_IN_PATH
			JAVAEXE_FOUND=`which java.exe`
			if [ "$JAVAEXE_FOUND" == "" ]; then
				echo "ERROR java.exe not found at c:/Program Files/Java/jre-9.0.4/bin/java.exe"
				unset JAVAEXE
				unset JAVA_HOME
			fi
		fi
		if [ "$JAVAEXE" == "" ]; then
			if [ -f /usr/lib/jvm/java-9-openjdk-amd64/bin/java ]; then
				echo "Java found at /usr/lib/jvm/java-9-openjdk-amd64/bin..."
				export JAVA="java"
				export JAVA_HOME="/usr/lib/jvm/java-9-openjdk-amd64"
			else
				echo "SUDO Install openjdk-9-jre..."
				sudo apt-get install openjdk-9-jre
				if [ -f /usr/lib/jvm/java-9-openjdk-amd64/bin/java ]; then
					echo "Java now found at /usr/lib/jvm/java-9-openjdk-amd64/bin..."
					export JAVA="java"
					export JAVA_HOME="/usr/lib/jvm/java-9-openjdk-amd64"
				else
					echo "WARNING: Java has been updated and the scripts need to be refreshed -- JAVA_HOME not set"
				fi
			fi
		fi
		unset JAVAEXE_FOUND
	else
		JAVAEXE_FOUND=`which java.exe`
		if [ "$JAVAEXE_FOUND" != "" ]; then
			export JAVAEXE="java.exe"
		else
			unset JAVAEXE
		fi
		unset JAVAEXE_FOUND
		JAVACEXE_FOUND=`which javac.exe`
		if [ "$JAVACEXE_FOUND" != "" ]; then
			export JAVACEXE="javac.exe"
		else
			unset JAVACEXE
		fi
		if [ -f /usr/lib/jvm/java-9-openjdk-amd64/bin/java ]; then
			echo "Java found at /usr/lib/jvm/java-9-openjdk-amd64/bin..."
			export JAVA="java"
			export JAVA_HOME="/usr/lib/jvm/java-9-openjdk-amd64"
			JAVA_FOUND=`which java`
			if [ "$JAVA_FOUND" == "" ]; then
				echo "Adding $JAVA_HOME/bin to PATH..."
				export PATH="$JAVA_HOME/bin:$PATH"
			fi
		else
			echo "SUDO Install openjdk-9-jre..."
			sudo apt-get install openjdk-9-jre
			if [ -f /usr/lib/jvm/java-9-openjdk-amd64/bin/java ]; then
				echo "Java now found at /usr/lib/jvm/java-9-openjdk-amd64/bin..."
				export JAVA="java"
				export JAVA_HOME="/usr/lib/jvm/java-9-openjdk-amd64"
			else
				echo "WARNING: Java has been updated and the scripts need to be refreshed"
			fi
		fi
	fi
fi

if [ "$JAVA" != "java" ]; then
	if [ "$JAVA" != "" ]; then
		alias java="$JAVA"
	fi
fi

export JAVACMD="$JAVA_HOME/bin/$JAVA"

# Toggle between the Windows and Linux JDK 9 implementations
if [ "$JDK_HOME" == "" ]; then
	if [ "$JAVAC" == "javac.exe" ]; then
		JAVACEXE_FOUND=`which javac.exe`
		if [ "$JAVACEXE_FOUND" == "" ]; then
			export JDK_HOME="$HOME/c-programs/Java/jdk-9.0.4"
			JDK_IN_PATH=`echo $PATH | grep Java/jdk-9.0.4/bin`
			if [ "$JDK_IN_PATH" == "" ]; then
				export PATH="$JDK_HOME/bin:$PATH"
			fi
			unset JDK_IN_PATH
			JAVACEXE_FOUND=`which javac.exe`
			if [ "$JAVACEXE_FOUND" == "" ]; then
				echo "ERROR Windows Oracle JDK 9 installation not found on C drive"
				unset JAVACEXE
				export JAVAC="javac"
			fi
		else
			export JDK_HOME="$HOME/c-programs/Java/jdk-9.0.4"
		fi
		unset JAVACEXE_FOUND
	fi
	if [ "$JAVAC" == "javac" ]; then
		if [ -f /usr/lib/jvm/java-9-openjdk-amd64/bin/javac ]; then
			echo "Javac found at /usr/lib/jvm/java-9-openjdk-amd64/bin..."
			export JDK_HOME="/usr/lib/jvm/java-9-openjdk-amd64"
		else
			echo "SUDO Install openjdk-9-jdk-headless..."
			sudo apt-get install openjdk-9-jdk-headless
			if [ -f /usr/lib/jvm/java-9-openjdk-amd64/bin/javac ]; then
				echo "Javac now found at /usr/lib/jvm/java-9-openjdk-amd64/bin..."
				export JDK_HOME="/usr/lib/jvm/java-9-openjdk-amd64"
			else
				echo "WARNING: Java has been updated and the scripts need to be refreshed"
			fi
		fi
	fi
fi

if [ "JAVAC" != "javac" ]; then
	if [ "$JAVAC" != "" ]; then
		alias javac="$JAVAC"
	fi
fi

export JAVACCMD="$JDK_HOME/bin/$JAVAC"

# Make sure the Windows SSH agent is running
if [ -f /mnt/d/msscodefactory/keys/run-your-email.address-putty-private-pageant.vbs ]; then
	if [ "$POWERSHELL" != "" ]; then
		PAGEANT_RUNNING=`tasklist.exe | grep pageant.exe`
		if [ "$PAGEANT_RUNNING" == "" ]; then
			$POWERSHELL "d:/msscodefactory/keys/run-your-email.address-putty-private-pageant.vbs"
		fi
		unset PAGEANT_RUNNING
	fi
fi

# Make sure the Linux ssh-agent is running
if [ "$SSH_AGENT_PID" == "" ]; then
	if [ -f $HOME/bin/ssh-agent-setpid.bash ]; then
		. $HOME/bin/ssh-agent-setpid.bash
	fi
fi

if [ "$SSH_AGENT_PID" != "" ]; then
	unset SSH_AGENT_STATUS
	SSH_AGENT_STATUS=`ps -ef | grep ssh-agent | grep -v grep`
	if [ "$SSH_AGENT_STATUS" == "" ]; then
		# ssh-agent -k
		unset SSH_AGENT_PID
		rm -f $HOME/bin/ssh-agent-setpid.bash
		nohup ssh-agent < /dev/null &> $HOME/bin/ssh-agent-setpid.bash 2>&1 &
		sleep 2
		. $HOME/bin/ssh-agent-setpid.bash
		SSH_AGENT_STATUS=`ps -ef | grep ssh-agent | grep -v grep`
		echo "SSH_AGENT_STATUS=\"$SSH_AGENT_STATUS\""
	else
		echo "SSH_AGENT_STATUS=\"$SSH_AGENT_STATUS\""
	fi
	unset SSH_AGENT_STATUS
else
	rm -f $HOME/bin/ssh-agent-setpid.bash
	nohup ssh-agent < /dev/null &> $HOME/bin/ssh-agent-setpid.bash 2>&1 &
	sleep 2
	. $HOME/bin/ssh-agent-setpid.bash
fi
echo "SSH_AGENT_PID=$SSH_AGENT_PID"

# Make sure the Linux SSH key for your@eamil.address is registered
if [ -f /mnt/d/msscodefactory/keys/your-email.address.ssh ]; then
	KEY_EXISTS=`ssh-add -L | grep your-email.address`
	# echo "KEY_EXISTS=\"$KEY_EXISTS\""
	if [ "$KEY_EXISTS" == "" ]; then
		ssh-add /mnt/d/msscodefactory/keys/your-email.address.ssh
	fi
	unset KEY_EXISTS
fi

# Make sure WordNet 3.0 with 3.1 data files is configured
if [ -f /mnt/d/msscodefactory/bin/WordNet-3.0/dict/noun.exc ]; then
	export WNHOME="/mnt/d/msscodefactory/bin/WordNet-3.0"
	WORDNET_IN_PATH=`echo $PATH | grep 'WordNet-3.0/bin'`
	if [ "$WORDNET_IN_PATH" == "" ]; then
		export PATH="$WORDNET_HOME/bin:$PATH"
	fi
	unset WORDNET_IN_PATH
fi

# Configure the project paths
if [ "$MSSCFHOME" == "" ]; then
	export MSSCFDRIVE="/."
	if [ -d "/mnt/d/msscodefactory" ]; then
		export MSSCFHOME="/mnt/d/msscodefactory"
	fi
fi

if [ "$MSSCFHOME" != "" ]; then
	if [ "$MSSCFDOWNLOADS" == "" ]; then
		if [ -d "$MSSCFHOME/downloads" ]; then
			export MSSCFDOWNLOADS="$MSSCFHOME/downloads"
		fi
	fi
	if [ "$MSSCF28HOME" == "" ]; then
		if [ -d "$MSSCFHOME/net-sourceforge-MSSCodeFactory-2-8/java/bin" ]; then
			export MSSCF28HOME="$MSSCFHOME/net-sourceforge-MSSCodeFactory-2-8"
			export PATH="$MSSCF28HOME/java/bin:$PATH"
		fi
	else
		export PATH="$MSSCF28HOME/java/bin:$PATH"
	fi
	if [ "$MSSCF28HOME" != "" ]; then
		if [ "$MSSCF28CARTRIDGEDIR" == "" ]; then
			if [ -d "$MSSCFHOME/net-sourceforge-MSSCodeFactory-2-8/java/cartridge-2.8" ]; then
				export MSSCF28CARTRIDGEDIR="$MSSCF28HOME/java/cartridge-2.8"
			fi
		fi
	fi
	if [ "$MSSCF29HOME" == "" ]; then
		if [ -d "$MSSCFHOME/net-sourceforge-MSSCodeFactory-2-9/java/bin" ]; then
			export MSSCF29HOME="$MSSCFHOME/net-sourceforge-MSSCodeFactory-2-9"
			export PATH="$MSSCF29HOME/java/bin:$PATH"
		fi
	else
		export PATH="$MSSCF29HOME/java/bin:$PATH"
	fi
	if [ "$MSSCF29CARTRIDGEDIR" == "" ]; then
		if [ -d "$MSSCFHOME/net-sourceforge-MSSCodeFactory-Cartridge-2-9" ]; then
			export MSSCF29CARTRIDGEDIR="$MSSCFHOME/net-sourceforge-MSSCodeFactory-Cartridge-2-9"
		fi
	fi
	if [ "$MSSCFSECURITY210HOME" == "" ]; then
		if [ -d "$MSSCFHOME/net-sourceforge-MSSCodeFactory-CFSecurity-2-10/java/bin" ]; then
			export MSSCFSECURITY210HOME="$MSSCFHOME/net-sourceforge-MSSCodeFactory-CFSecurity-2-10"
			export PATH="$MSSCFSECURITY210HOME/java/bin:$PATH"
		fi
	else
		export PATH="$MSSCFSECURITY210HOME/java/bin:$PATH"
	fi
	if [ "$MSSCFINTERNET210HOME" == "" ]; then
		if [ -d "$MSSCFHOME/net-sourceforge-MSSCodeFactory-CFInternet-2-10/java/bin" ]; then
			export MSSCFINTERNET210HOME="$MSSCFHOME/net-sourceforge-MSSCodeFactory-CFInternet-2-10"
			export PATH="$MSSCFINTERNET210HOME/java/bin:$PATH"
		fi
	else
		export PATH="$MSSCFINTERNET210HOME/java/bin:$PATH"
	fi
	if [ "$MSSCFCRM210HOME" == "" ]; then
		if [ -d "$MSSCFHOME/net-sourceforge-MSSCodeFactory-CFCrm-2-10/java/bin" ]; then
			export MSSCFCRM210HOME="$MSSCFHOME/net-sourceforge-MSSCodeFactory-CFCrm-2-10"
			export PATH="$MSSCFCRM210HOME/java/bin:$PATH"
		fi
	else
		export PATH="$MSSCFCRM210HOME/java/bin:$PATH"
	fi
	if [ "$MSSCFDBTEST210HOME" == "" ]; then
		if [ -d "$MSSCFHOME/net-sourceforge-MSSCodeFactory-CFDbTest-2-10/java/bin" ]; then
			export MSSCFDBTEST210HOME="$MSSCFHOME/net-sourceforge-MSSCodeFactory-CFDbTest-2-10"
			export PATH="$MSSCFDBTEST210HOME/java/bin:$PATH"
		fi
	else
		export PATH="$MSSCFDBTEST210HOME/java/bin:$PATH"
	fi
	if [ "$MSSCFBAM210HOME" == "" ]; then
		if [ -d "$MSSCFHOME/net-sourceforge-MSSCodeFactory-CFBam-2-10/java/bin" ]; then
			export MSSCFBAM210HOME="$MSSCFHOME/net-sourceforge-MSSCodeFactory-CFBam-2-10"
			export PATH="$MSSCFBAM210HOME/java/bin:$PATH"
		fi
	else
		export PATH="$MSSCFBAM210HOME/java/bin:$PATH"
	fi
fi
