#!/bin/bash

#############################################################################
## 
##   Logtalk macOS installer package auxiliary script
##   Last updated on December 25, 2020
## 
##   This file is part of Logtalk <https://logtalk.org/>  
##   Copyright 1998-2021 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
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
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
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 ""
