#!/bin/bash

basepath="/ifs/data/log/"
tarset=`ls $basepath/IsilonLogs*.tgz | sort |tail -n 1 | xargs basename`
tardate=`echo $tarset | sed -e 's/IsilonLogs-\(.*\)-\([0-9]*-[0-9]*\).tgz/\2/'`
clustername=`echo $tarset | sed -e 's/.*IsilonLogs-\(.*\)-\([0-9]*-[0-9]*\).tgz/\1/'`
filelist=`tar -tf $basepath/$tarset | grep $clustername`
mkdir -p $basepath/$tardate/
chmod 600 $basepath/$tardate/
cd $basepath/$tardate/

for f in $filelist; do
	tar -zxf $basepath/$tarset --include $f
	node=`echo $f | sed -e 's/.*\/\(.*\)\/.*/\1/'`
	if [ -f $f ]; then
		file=`tar -tf $f`
		tar -xf $f -C $node
		rm -f $f
		if [ -f $node/$file ]; then
			mv $node/$file $node-$file
		fi
	fi
	if ! [ "$(ls -A $node)" ]; then
		rmdir $node
 	fi
done
