#!/usr/bin/env bash

################################################################################
# copyright Bjarne D Mathiesen
#           Korsør ; Danmark ; Europa
#           macintosh .at. mathiesen .dot. info
# date      04/07-2007
# revised   02/12-2007  implemented automatic patching of Portfiles
#           28/12-2009  fixed the download link
#                       modified source/build directory
#                       removed sudo from commands
#           18/06-2011  added default values for the parameters
#                       updated path values for XCode4
#           05/03-2017  added update to actions
#           31/01-2018  added checkpaths & fixpaths
#                       updated default values for the parameters
#           02/03-2021  removed  fixpaths
#                       reworked setpaths
#                       added fixCLT
#
# this script is released under the OSS GPL v3 license
# the author welcomes feedback and improvements
# 
(   cd  $( dirname ${0} )

source ./portDefaults

usage() {
cat <<EOT

purpose : to automate the whole MacPorts install & update process
\${1} : action                 [ update (default) , install , setpaths , checkpaths , select , fixCLT ]
\${2} : macports base version  ( default ${version} ) only for install

!!! the script has to be run with root privileges !!!

EOT
}

declare action=${1:-"update"}
[ ! -z ${2} ] && declare version=${2}

case ${action} in
('-h'|'--help')
usage
exit
esac

if [[ ${EUID} -ne 0 ]]
then
cat <<EOT

+------------------------------------------------+
| this script has to be run with root privileges I
+------------------------------------------------+
please try again with "sudo ${0}"

EOT
usage
exit
fi

case ${action} in

########################
# https://trac.macports.org/wiki/ProblemHotlist#reinstall-clt
('fixCLT')
touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
softwareupdate -ia
rm /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress

;;
########################
#
('select')
while read -u 9 name selected options
do
    options=( $( echo ${options} ) )
    port select --set "${name}" ${options[@]: -2:1}
done 9< <( port -q select --summary )
port select --summary

;;
########################
# update installed ports
('update')
port -dN selfupdate
port outdated
port -pN reclaim
port clean --work outdated
port -cuNp upgrade outdated
port -pN reclaim
port -q installed requested > requested.ports

;;
########################
# setup the system paths
('setpaths')

mkdir -p  /etc/paths.d
cp    -np /etc/paths /etc/paths.orig
mv    -n  /etc/paths /etc/paths.d/999macosx
touch     /etc/paths

echo "${prefix}/bin"        >  /etc/paths
echo "${prefix}/sbin"       >> /etc/paths

echo "/Applications/Xcode.app/Contents/Developer/usr/bin"   >  /etc/paths.d/888developer

mkdir -p  /etc/manpaths.d
cp    -np /etc/manpaths /etc/manpaths.orig
mv    -n  /etc/manpaths /etc/manpaths.d/999macosx
touch     /etc/manpaths

echo "${prefix}/share/man"  > /etc/manpaths.d/000macports
#echo "/Developer/usr/share/man"     >  /etc/manpaths.d/888developer
#echo "/Developer/usr/X11/share/man" >> /etc/manpaths.d/888developer

;;
########################
# check the system paths
('checkpaths')
/usr/libexec/path_helper

;;
##################
# install macports
('install')

if [ ! -e MacPorts-${version}.tar.gz ]
then
    curl -L -O --url "https://github.com/macports/macports-base/releases/download/v${version}/MacPorts-${version}.tar.gz"
fi

rm  -rf  ./MacPorts-${version}
tar -zxf   MacPorts-${version}.tar.gz 2>/dev/null

cd MacPorts-${version}
CC=/usr/bin/cc ./configure \
     --prefix=/opt/local \
     --with-install-user=root \
     --with-install-group=admin \
     --with-directory-mode=0755 \
     --enable-readline \
&& make SELFUPDATING=1 \
&& make install SELFUPDATING=1

# update MacPorts itself
${prefix}/bin/port -dN selfupdate

# let's get bash, zsh & nano
${prefix}/bin/port -N install bash  && echo "${prefix}/bin/bash" >> /etc/shells
${prefix}/bin/port -N install zsh   && echo "${prefix}/bin/zsh"  >> /etc/shells
${prefix}/bin/port -N install nano

# cleanup
cd ..
rm  -rf  ./MacPorts-${version}

;;
# default
(*)
usage
esac

) ; wait

