#!/bin/sh

OSNAME=$(uname)
VER=`cat Version`

if [ "$OSNAME" = "AIX" ]
then
	#echo '.svn' > /tmp/xcat-excludes
    #tar -X /tmp/xcat-excludes -cf /opt/freeware/src/packages/SOURCES/xCAT-UI.tar xCAT-UI
    #gzip -f /opt/freeware/src/packages/SOURCES/xCAT-UI.tar
    #cd ./xCAT-UI
    #rm -f /opt/freeware/src/packages/SRPMS/xCAT-UI*rpm /opt/freeware/src/packages/RPMS/ppc/xCAT-UI*rpm
    #rpm -ba xCAT-UI.spec
    #rpm -Uvh /opt/freeware/src/packages/RPMS/ppc/xCAT-UI*rpm

    source=/opt/freeware/src/packages
	echo '.svn' > /tmp/xcat-excludes
    tar -X /tmp/xcat-excludes -cf $source/SOURCES/xCAT-UI-$VER.tar xCAT-UI
    gzip -f $source/SOURCES/xCAT-UI-$VER.tar
    rm -f $source/SRPMS/xCAT-UI*rpm $source/RPMS/noarch/xCAT-UI*rpm
    rpm -ba xCAT-UI/xCAT-UI.spec
    #rpm -Uvh /opt/freeware/src/packages/RPMS/ppc/xCAT-UI*rpm

else
	if [ -f /etc/redhat-release ]
	then
   		pkg="redhat"
	else
   		pkg="packages"
	fi
	
	# Minify Javascript files using Google Compiler
	JAVA='/opt/ibm/java-ppc64-60/jre/bin/java'
	JAR='/xcat2/build/tools/compiler.jar'
	UI='xCAT-UI/js'
	
	declare -a FILES
	IFS='
	'
	
	# Find all Javascript files
	FILES=(`find ${UI} -name '*.js'`)
	for i in ${FILES[*]}; do
		# Ignore Javascripts that are already minified
		if [[ ! $i =~ '.*\.min\.js$' ]]; then
			`${JAVA} -jar ${JAR} --warning_level=QUIET --js=$i --js_output_file=$i.min`
			
			# Remove old Javascript and replace it with minified version
			rm -rf $i
			mv $i.min $i
	    fi
	done

	set -x
	tar --exclude=.svn -czf /usr/src/$pkg/SOURCES/xCAT-UI-$VER.tar.gz xCAT-UI
	rm -f /usr/src/$pkg/SRPMS/xCAT-UI-$VER*rpm /usr/src/$pkg/RPMS/noarch/xCAT-UI-$VER*rpm
	rpmbuild -ta /usr/src/$pkg/SOURCES/xCAT-UI-$VER.tar.gz
	#rpm -Uvh /usr/src/$pkg/RPMS/noarch/xCAT-UI-$VER*rpm
fi
