#!/bin/bash

#############################################################################
## 
##   Logtalk macOS installer package auxiliary script
##   Last updated on November 1, 2023
## 
##   This file is part of Logtalk <https://logtalk.org/>  
##   SPDX-FileCopyrightText: 1998-2023 Paulo Moura <pmoura@logtalk.org>
##   SPDX-License-Identifier: Apache-2.0
##   
##   Licensed under the Apache License, Version 2.0 (the "License");
##   you may not use this file except in compliance with the License.
##   You may obtain a copy of the License at
##   
##       http://www.apache.org/licenses/LICENSE-2.0
##   
##   Unless required by applicable law or agreed to in writing, software
##   distributed under the License is distributed on an "AS IS" BASIS,
##   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
##   See the License for the specific language governing permissions and
##   limitations under the License.
## 
#############################################################################

echo ""
echo "Checking for the Logtalk environment variable definitions..."
echo ""

if grep "LOGTALKHOME=/opt/local/share/logtalk" /etc/zprofile >/dev/null 2>&1; then
	echo "Logtalk environment variables default values already set for zsh!"
else
	/bin/cp -fp /etc/zprofile /etc/zprofile.lgtsaved
	echo '' >> /etc/zprofile
	echo '# Logtalk environment variables default values' >> /etc/zprofile
	echo 'LOGTALKHOME=/opt/local/share/logtalk' >> /etc/zprofile
	echo 'LOGTALKUSER=$HOME/logtalk' >> /etc/zprofile
	echo 'export LOGTALKHOME LOGTALKUSER' >> /etc/zprofile
	echo 'PATH=$PATH:$LOGTALKHOME/tools/diagrams' >> /etc/zprofile
	echo 'PATH=$PATH:$LOGTALKHOME/tools/lgtdoc/xml' >> /etc/zprofile
	echo 'PATH=$PATH:$LOGTALKHOME/scripts' >> /etc/zprofile
	echo 'PATH=$PATH:$LOGTALKHOME/integration' >> /etc/zprofile
	echo 'MANPATH=$MANPATH:$LOGTALKHOME/man' >> /etc/zprofile
	echo 'INFOPATH=$INFOPATH:$LOGTALKHOME/docs:$LOGTALKHOME/manuals' >> /etc/zprofile
	echo 'export PATH MANPATH INFOPATH' >> /etc/zprofile
	
fi

if grep "LOGTALKHOME=/opt/local/share/logtalk" /etc/profile >/dev/null 2>&1; then
	echo "Logtalk environment variables default values already set for bash!"
else
	/bin/cp -fp /etc/profile /etc/profile.lgtsaved
	echo '' >> /etc/profile
	echo '# Logtalk environment variables default values' >> /etc/profile
	echo 'LOGTALKHOME=/opt/local/share/logtalk' >> /etc/profile
	echo 'LOGTALKUSER=$HOME/logtalk' >> /etc/profile
	echo 'export LOGTALKHOME LOGTALKUSER' >> /etc/profile
	echo 'PATH=$PATH:$LOGTALKHOME/tools/diagrams' >> /etc/profile
	echo 'PATH=$PATH:$LOGTALKHOME/tools/lgtdoc/xml' >> /etc/profile
	echo 'PATH=$PATH:$LOGTALKHOME/scripts' >> /etc/profile
	echo 'PATH=$PATH:$LOGTALKHOME/integration' >> /etc/profile
	echo 'MANPATH=$MANPATH:$LOGTALKHOME/man' >> /etc/profile
	echo 'INFOPATH=$INFOPATH:$LOGTALKHOME/docs:$LOGTALKHOME/manuals' >> /etc/profile
	echo 'export PATH MANPATH INFOPATH' >> /etc/profile
fi

if grep "setenv LOGTALKHOME /opt/local/share/logtalk" /etc/csh.cshrc >/dev/null 2>&1; then
	echo "Logtalk environment variables default values already set for csh!"
else
	/bin/cp -fp /etc/csh.cshrc /etc/csh.cshrc.lgtsaved
	echo '' >> /etc/csh.cshrc
	echo '# Logtalk environment variables default values' >> /etc/csh.cshrc
	echo "setenv LOGTALKHOME /opt/local/share/logtalk" >> /etc/csh.cshrc
	echo 'setenv LOGTALKUSER $HOME/logtalk' >> /etc/csh.cshrc
	echo 'setenv PATH "${PATH}":"${LOGTALKHOME}"/tools/diagrams' >> /etc/csh.cshrc
	echo 'setenv PATH "${PATH}":"${LOGTALKHOME}"/tools/lgtdoc/xml' >> /etc/csh.cshrc
	echo 'setenv PATH "${PATH}":"${LOGTALKHOME}"/scripts' >> /etc/csh.cshrc
	echo 'setenv PATH "${PATH}":"${LOGTALKHOME}"/integration' >> /etc/csh.cshrc
	echo 'setenv MANPATH "${MANPATH}":"${LOGTALKHOME}"/man' >> /etc/csh.cshrc
	echo 'setenv INFOPATH ${INFOPATH}:"${LOGTALKHOME}"/docs:"${LOGTALKHOME}"/manuals' >> /etc/csh.cshrc
fi

echo ""
echo "Creating a link to the Logtalk installation folder in the /Applications folder..."
/bin/ln -sf /opt/local/share/logtalk /Applications/Logtalk
/usr/bin/open /Applications/Logtalk/
/usr/bin/open /Applications/Logtalk/scripts/macos/command_files
/usr/bin/open /Applications/Logtalk/README.md

echo ""
echo "You have successfully installed Logtalk!"
echo ""
