#!/bin/bash
GPGDIR=/etc/portage/gpg/
WORKDIR=/var/vhosts/portage.schorsch-tech.de/git/portage
BAREDIR=/var/vhosts/portage.schorsch-tech.de/git/portage.git

MAILADDR=georg@schorsch-tech.de

LANG=C

DATE=`date +%Y%m%d`

cd /tmp
rm /tmp/portage-latest.tar.xz* > /dev/null 2>&1
wget http://distfiles.gentoo.org/snapshots/portage-latest.tar.xz > /dev/null 2>&1
wget http://distfiles.gentoo.org/snapshots/portage-latest.tar.xz.gpgsig > /dev/null 
2>&1

gpgsig=`cat portage-latest.tar.xz.gpgsig`
result=`gpg --verify --homedir="$GPGDIR" portage-latest.tar.xz.gpgsig 
portage-latest.tar.xz 2>&1`
gpgresultcode="$?"

if [[ "$gpgresultcode" = "0" ]]
then
	cd "$WORKDIR"
	rm -Rf *
	tar -xJf /tmp/portage-latest.tar.xz portage
	mv portage/* .
	rm -Rf portage
	git add -A :/
	git commit -m"
Date: $DATE

File: portage-latest.tar.xz
Verify result:
$result

gpgsig:
$gpgsig
" 2>&1 > /dev/null
	git tag "$DATE" > /dev/null 2>&1

	git push origin > /dev/null 2>&1
	git push --tags > /dev/null 2>&1
else
	echo "$result" | mail -s "GPG Key of fetched portage tree not valid" 
$MAILADDR
	exit 1
fi

# sync the tree
emerge --sync > /dev/null 2>&1

# check for glsa
glsa-check -lq

# maintain packages and distfiles
emaint all  > /dev/null 2>&1
eclean-dist -q

