#!/bin/sh

#
# Dasygenis Master Portmaster Script
# http://arch.ece.uowm.gr
# This script aids in installing massive number
# of packages. 
# Usage
# (a)$0 installed_port 
# 	e.g. ./portmaster_run.sh freetype'*'
# (b)$0 origin_port
#	e.g. ./portmaster_run.sh devel/cmake
#
# also, you can grab the current list
# portmaster --list-origins > /root/currentlist
# and you can reinstall them
#	./portmaster_run.sh `cat /root/currentlist`
#
#
# License: Creative Common 3.0 with attribution
#
# July, 2013
version="018"
#
# to update all packages 
# $0 `pkg version | grep '<' | cut -f 1 -d" "`

echo "Dasygenis Portmaster Script"
echo "FreeBSD Administration"
echo "Version: $version"
echo "..........................."

#check if we have portmaster
which portmaster >/dev/null 2>&1 

if [ $? -ne 0 ] ; then
	echo "Install portmaster with:"
	echo cd /usr/ports/ports-mgmt/portmaster/ \&\& make install
	exit 2
fi


export FORCE_PKG_REGISTER

#B5 temporary directories should be enabled as RW
mount -u -w /tmp > /dev/null 2>&1
mount -u -w /disk2 > /dev/null 2>&1
mount -u -w /var >  /dev/null 2>&1
mount -u -w /glftpd/disk1/  >  /dev/null 2>&1

# If Dasygenis lock/unlock script exist
# do execute it.
if [ -f /scripts/unlock_usr.sh ] ; then
	/scripts/unlock_usr.sh > /dev/null 2>&1
fi

#
# If we have the auxilarry function script
# do load it, and then read the machine specific
# config file. otherwise, just set a required variable
#
if [ -f /scripts/backup/functions.sh ] ; then
	. /scripts/backup/functions.sh
	load_configuration_file
else
	tempdir=/tmp
fi


day_of_month=`/bin/date +%d`
counter=`expr $day_of_month % 5` 
#
# We check every 5 days the dependencies...
#
if [ "${counter}" -eq "0" ] ; then
	portmaster -y --check-depends
	portmaster -y --check-port-dbdir 
	portmaster -y --check-port-dbdir 
	portmaster -y --clean-distfiles
fi

if [ $# -ge 1 ] ; then
	#we have some parameters
	toinstall="$@"
else
	#grab from stdin
	read toinstall
fi


PKG_TMPDIR=${tempdir}
export PKG_TMPDIR
if [ ! -d "${PKG_TMPDIR}" ] ; then
	echo "Temporary directory ${PKG_TMPDIR} does not exist"
	exit 5
fi




#
# If this machine uses portsnap, update /usr/ports
# prior to installing
#
if [ -d /var/db/portsnap ] ; then 
# Lets put a check here...if we have portsnap today, do not re-portsnap
#
 portsnap_checkfile=/tmp/.portsnapcheck

 portsnap_bypass=0
 # for 24 hours we bypassing portsnap
 ans=""
 if [ -f /tmp/.portsnapcheck ] ; then
 	ans=`find /tmp/.portsnapcheck -mtime 1`    > /dev/null 2>&1  
 fi

 portsnap_bypass=0
 if [ ! -z "$ans" ] ; then
	echo "+++++ Portsnap Executed already, bypassing portsnap fetch."
	portsnap_bypass=1
 else
	# Portsnap has not being executed. Lets touch the file for next time
	portsnap fetch update 
	touch /tmp/.portsnapcheck
 fi

fi

echo "============================================================"

if [ -f /usr/local/bin/pkg ]; then
	rm /var/cache/pkg/* > /dev/null 2>&1
	#pkg update -f
fi

echo Working on: $toinstall
/usr/bin/logger Executing $0 $toinstall

#rem: if fail the user should issue: make all-depends-list  on the corresponding port

# -D no cleaning of distfile
# -v verbose
# -y yes
# -g build packages
# -P use packages if available
# -G prevent make config
## Warning --update-if-newer causes errors
/bin/csh -c "( /usr/bin/nice -n +20 /usr/local/sbin/portmaster -G --no-confirm --packages-local  -v -y -D -g --delete-build-only  -P --local-packagedir=/usr/ports/packages/All '$toinstall'  ||  /usr/bin/nice -n +20 /usr/local/sbin/portmaster '$toinstall' )"
ret=$?

if [ "$portsnap_bypass" == 0 ] ; then
#lets clean old..Distfiles and Packages
yes | /usr/bin/nice -n +20  /usr/local/sbin/portmaster --clean-distfiles
yes | /usr/bin/nice -n +20  /usr/local/sbin/portmaster --clean-packages

echo "View Other Packages that need update (portmaster -L | grep New)  y/Y 5 secs remaining?"
count=0
read -t 1 yesno
while [ -z "$yesno" ] && [ $count -lt 5 ] ;
do
echo -n "."
read -t 1 yesno 
count=`expr $count + 1`
done
echo " "

#return status code back to shell
exit $ret

#
# User requested a printout...Give it to him
#
if [ "${yesno}" = "y" -o "${yesno}" = "Y" ] ; then
	portmaster -L | grep New
	
	#We are interactive, so why now ask some questions for clean jobs
	portmaster -y -s
fi

else
#return status code back to shell
exit $ret

fi
#end if portmaster has been executed today (portmaster bypass)
