#!/bin/sh

#
# Script to generate distribution builds of pLog
#
# (c) 27-08-2004, the pLog Team
#
#
# Requires two parameters: the name of the output package and the
# CVS tag label that will be used to fetch the contents of the build. Therefore,
# contents must be tagged _before_ running this script. 
# The same CVS label must be used for both the plog_devel and plog_docs module.
#

#
# server and repository path
#
SVNSERVER=http://devel.lifetype.net
SVNREPO=/svn/plog/plugins

#
# array with the files that we are going to remove before
# making the package... Wildcards, complete paths, whatever
# can be used here
#
TOREMOVE="unported/ bin/"
WORKFOLDER="/tmp/"

#sourceforge wants all unique filenames.  So
# we code them with a version number.  ugh.
VERSION="1.0"

#
# helper functions to print things in different colors
#
NOCOLOR='\e[0m'
message()
{
 echo -e "\033[01;32m$*$NOCOLOR"
}

warning()
{
 echo -e "\033[01;33m$*$NOCOLOR"
}

error()
{
 echo -e "\033[01;31m$*$NOCOLOR"
}

# check parameters
if [ $# -lt 1 ]; then
    echo "Makes a zip package of a given plugin"
    echo ""
	echo "Usage: build.sh template"
	echo ""
	echo "Use 'all' in order to generate a package of all the template sets"
	exit -1
fi

# get the name of the template that we're trying to build
TEMPLATENAME=$1

# set second parameter to anything if you don't want the files to
# be automatically uploaded
DONT_UPLOAD=$2

# a temporary working folder like any other...
TMPFOLDER=${WORKFOLDER}`date | md5`
CURFOLDER=`pwd`

# export the current folder
message "Exporting folder..."
svn export . $TMPFOLDER

# remove whatever we don't need
message "Removing unnecessary files...";
for i in ${TOREMOVE}
do
  message "  -- removing $i"
  rm -rf ${TMPFOLDER}/$i
done

# build the correct template package, or all of them if that's what we were requested to do...
cd $TMPFOLDER
message "Creating ZIP package..."
if [ $TEMPLATENAME == "all" ]; then
    TEMPLATENAME=_all_plugins
    zip -qr ${CURFOLDER}/${VERSION}_${TEMPLATENAME}.zip .
else
    zip -r ${CURFOLDER}/${VERSION}_${TEMPLATENAME}.zip ${TEMPLATENAME}/
fi

if [ "x$DONT_UPLOAD" == "x" ]; then
  # upload the package.  Linux ftp (at least my version,
  # doesn't have the -a command,
  # but I can use curl to do the same thing.
  if [ `uname` == "Linux" ]; then
    curl --upload-file ${CURFOLDER}/${VERSION}_${TEMPLATENAME}.zip --user anonymous:lifetype-uploads@jon.limedaley.com ftp://upload.sf.net/incoming/
  else
    ftp -a -u ftp://upload.sourceforge.net/incoming/${VERSION}_${TEMPLATENAME}.zip ${TEMPLATENAME}.zip
  fi
fi

message "Done!"

# cleanup
rm -rf $TMPFOLDER
