#!/bin/bash
#                 /home/www/.update.sh
# http://crystalfaeries.net/.update.sh
# celeste:crystalfaery 2015-04-20 19:41:29+00:00
# Update /media/mmcblk0p1 (VFAT SD Card mounted as /home/www-) from the current website /home/www
# This obamanation is the necessary consequence of using a native VFAT SD card (ewwwwwwwwww...!)
# ...which means no symlinks, wrong user, group, file permissions; have to work as root... etc.)
# (however, this hopefully means it can be mounted and copied on a WinDblOwS or MacOSuX system.)
# NOTE: vs non-VFAT version, we are controlled by files in /home/www versus /home/www- (on the SD Card)

if [ "$(/usr/bin/whoami)" == "root" ]
then
	#/etc/fstab:	/dev/disk/by-uuid/9016-4EF8 /home/www- vfat rw,nosuid,nodev,noatime,uid=1000,gid=1000,fmask=0002,dmask=0002,codepage=437,iocharset=utf8,shortname=mixed,showexec,utf8,flush,errors=remount-ro 0 0
	cd /home/www	|| exit -2							# source directory
	rsync -auvzH --exclude=.du.txt $(find . -maxdepth 1 -type f -print) /home/www-	# copy top level files
	cd /home/www-	|| exit -3							# target directory
	rm -rf				$( cat .exclude.txt )				# exclude implies delete
	rsync -auvzH --delete	--exclude-from=.exclude.txt	--exclude=.du.txt	\
	/home/www/{audio,clairvoyance,fae,htdig,icons,imgs,kahealani,posix,src,video}	.
#	NOTE: we did NOT include webalizer results which if shared may compromise privacy for visitors

	cp /dev/null	/tmp/du.$$.txt		# initiate log file
	cp /dev/null	/tmp/$$.exclude.txt	# initiate .exclude file
	for d in `sort -u < .exclude.txt`
	do
		echo $d >> /tmp/$$.exclude.txt	# build cleaned-up .exclude file
		du -s /home/www/$d | sed 's/\/home\/www\///g'	>> /tmp/du.$$.txt	# calculate exclusions
	done
	mv /tmp/$$.exclude.txt /home/www/.exclude.txt	# install cleaned-up .exclude.txt file
	df --full | head -n 1			>	.du.txt				# header
	df --full | grep /home/www-		>>	.du.txt				# usage
	echo ""					>>	.du.txt
	echo "	EXCLUDED:"			>>	.du.txt
	sort -rn < /tmp/du.$$.txt		>>	.du.txt
	echo ""					>>	.du.txt
	echo "	INCLUDED:"			>>	.du.txt
	dudir | sed 's/\/home\/www-\///'	>>	.du.txt
	vi						.du.txt /home/www/.exclude.txt /home/www/.update.sh
	for f in					.du.txt /home/www/.exclude.txt /home/www/.update.sh
	do
		rsync	-auvzH $f ~kahealani/txt/`hostname`.mmcblk0p1$(basename $f)	# log locally as TODO
	done
	rsync		-auvzH	/home/www/.update.sh	/usr/local/bin/vfat.update.sh
	exit	$?
else
	echo "$0 must be executed as root to write to VFAT /dev/mmcblk0p1 (/home/www-)"
	exit -1
fi


syntax highlighted by Code2HTML, v. 0.9.1