#!/bin/sh

#
# Dasygenis Master Massive Portmaster Script
# http://arch.ece.uowm.gr
# Minas Dasygenis, July 2015
# This script aids in installing massive number
# of packages.
# Just execute it, and all packages will be updated
#
#
# License: Creative Common 3.0 with attribution
#
version="003"


execscript=/scripts/system/portmaster_run.sh

if [ ! -f "$execscript" ] ; then
	echo execscript $execscript absent, download it from arch.ece.uowm.gr. Aborting now..
	exit 8
fi


#check unknown packages
unknown=`pkg version | grep '?'| cut -f 1 -d'?'`

if [ ! -z "$unknown" ] ; then
	echo WARNING. Undefined packages. Remove with pkg delete -f to continue
	echo $unknown
	echo pkg delete -f $unknown
	/scripts/unlock_usr.sh > /dev/null 2>&1
	exit 4
fi

#Grab packages need update
updated=`pkg version | grep '<'| cut -f 1 -d'<'`
if [ -z "$updated" ] ; then
	echo "Nothing to be done, exiting"
	exit 4
fi

echo "The following packages will be updated"
echo $updated
sleep 3

#variable to hold all problematic ports
problemports=""




for i in $updated
do

#lets check if that port exist, because ot may have been updated along the way
doesitexist=`pkg version | grep '<'| cut -f 1 -d'<' | grep $i`
if [ -z "$doesitexist" ] ; then
	echo "------------------------------------------------------------"
	echo "------------------------------------------------------------"
	echo "------------------------------------------------------------"
	echo "----------------PORT $i has already been updated, skipping"
	echo "------------------------------------------------------------"
	echo "------------------------------------------------------------"
	echo "------------------------------------------------------------"
	continue
fi

echo "============================================================"
echo "============================================================"
echo "============================================================"
echo "===============Updating: $i"
echo "============================================================"
echo "============================================================"
echo "============================================================"
sleep 1
$execscript $i
if [ $? -ne 0 ] ; then
	echo "-Sleep30-Error occured, at port $i"
	problemports="$problemports:$i"
	sleep 30
fi

#end for every port to be updated
done

if  [ ! -z "$problemports" ] ; then
	echo "Some ports could not be updated: $problemports"
	exit 4
fi





