#!/bin/bash

###########################################################################
#
#	Shell program to upload a web site to SourceForge.net.
#
#	Copyright 2001-2002, William Shotts <bshotts@users.sourceforge.net>.
#
#	This program is free software; you can redistribute it and/or
#	modify it under the terms of the GNU General Public License as
#	published by the Free Software Foundation; either version 2 of the
#	License, or (at your option) any later version. 
#
#	This program is distributed in the hope that it will be useful, but
#	WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#	General Public License for more details.
#
#	This software is part of the LinuxCommand.org project, a site for
#	Linux education and advocacy devoted to helping users of legacy
#	operating systems migrate into the future.
#
#	You may contact the LinuxCommand.org project at:
#
#		http://www.linuxcommand.org
#
#	SourceForge.net is a trademark of VA Software, Inc.
#
#	Description:
#
#	This program uploads your project web site to SourceForge.net.  It
#	does this by creating a gzipped tar file (a "tarball") containing
#	all the files in your project's htdocs directory more recent than
#	the last time you uploaded your site.
#
#	For a description of the expected layout of your project
#	directories, please see the sf-tools.README file.
#
#	To configure this script for your project, set the PROJECT_NAME
#	constant (below) to the Unix name of your project.  Also set the
#	SF_USER constant with your SourceForge.net user name if it is not the
#	same as your user name on your local machine.
#
#	Usage:
#
#		sft_upload_website -h | --help | [-a]
#
#	Options:
#
#		-h, --help	Display this help message and exit.
#		-a              Upload all files, not just recent ones
#
#
#	Revisions:
#
#	01/23/2001	File created by lc_new_script v.2.0.6
#	01/28/2001	Added automatic installation of uploaded web
#			site tarball. (0.0.2)
#	01/28/2001	Slight fix to timestamp handling (0.0.3)
#	03/22/2001	Changed description (above) to reflect the
#			fact that this program now installs the
#			contents of the transmitted tarbal. (0.0.4)
#	03/28/2001	Added support for new SF directory naming scheme
#			(1.0.1)
#	05/12/2001	Fixed problem with archive expansion on remote
#			site. (1.0.2)
#	01/26/2002	Improved method of setting file permissions and
#			other minor cleanups (1.0.3)
#	03/07/2002	Various cosmetic updates (1.0.4)
#
#	$Id: sft_upload_website,v 1.13 2002/03/09 16:50:43 bshotts Exp $
#
###########################################################################


###########################################################################
#	Constants
###########################################################################

PROGNAME=$(basename $0)
VERSION="1.0.4"

PROJECT_NAME="your_project"	# Put your project name here!

SF_USER=${USER:-$LOGNAME}	# Replace "${USER:-$LOGNAME}" with your SF
				# shell account name if it is not the same
				# as your local user name.
				
PROJECT_ROOT=~/$PROJECT_NAME
HTDOCS="$PROJECT_ROOT/htdocs"

# Construct group directory based on new (3/28/2001) SF directory naming
# scheme.
SF_PROJECT_DIR="/home/groups/$(echo $PROJECT_NAME | awk '{ printf("%s/%s", substr($1, 1, 1), substr($1, 1, 2))}' )/$PROJECT_NAME"

TIME_STAMP=${PROJECT_ROOT}/${PROJECT_NAME}_last_upload
ARCHIVE=${PROJECT_NAME}_site.tar.gz


###########################################################################
#	Functions
###########################################################################


function clean_up
{

	#####	
	#	Function to remove temporary files and other housekeeping
	#	No arguments
	#####

	# If a full upload has been forced, put the old timestamp back.
	
	if [ -f $TIME_STAMP.tmp ]; then
		mv $TIME_STAMP.tmp $TIME_STAMP
	fi
}


function error_exit 
{
	#####	
	# 	Function for exit due to fatal program error
	# 	Accepts 1 argument
	#		string containing descriptive error message
	#####

	
	echo "${PROGNAME}: ${1:-"Unknown Error"}" >&2
	clean_up
	exit 1
}


function graceful_exit
{
	#####
	#	Function called for a graceful exit
	#	No arguments
	#####

	clean_up
	exit
}


function term_exit
{
	#####
	#	Function to perform exit if termination signal is trapped
	#	No arguments
	#####

	echo "${PROGNAME}: Terminated"
	clean_up
	exit
}


function int_exit
{
	#####
	#	Function to perform exit if interrupt signal is trapped
	#	No arguments
	#####

	echo "${PROGNAME}: Aborted by user"
	clean_up
	exit
}


function usage
{
	#####
	#	Function to display usage message (does not exit)
	#	No arguments
	#####

	echo "Usage: ${PROGNAME} [-h | --help] [-a]"
}


function helptext
{
	#####
	#	Function to display help message for program
	#	No arguments
	#####
	
	local tab=$(echo -en "\t\t")
		
	cat <<- -EOF-

	${PROGNAME} ver. ${VERSION}	
	This is a program to upload a web site to SourceForge.net.
	
	$(usage)
	
	Options:
	
	-h, --help	Display this help message and exit.
	-a              Upload all files, not just recent ones
			
	
		
-EOF-
}	


###########################################################################
#	Program starts here
###########################################################################
	
##### Initialization And Setup #####

# Trap TERM, HUP, and INT signals and properly exit

trap term_exit TERM HUP
trap int_exit INT


##### Command Line Processing #####

if [ "$1" = "--help" -o "$1" = "-h" ]; then
	helptext
	graceful_exit
fi

# Some sanity checks

if [ "$SF_USER" = "" ]; then
	echo >&2 '
Your SourceForge.net user name (SF_USER) is empty.
On some systems you may need to substitute
$LOGNAME for $USER.'
	error_exit "Configuration error"
fi

if [ "$PROJECT_NAME" = "your_project" -o "$PROJECT_NAME" = "" ]; then
	echo >&2 "
PROJECT_NAME has not been set.  Please edit this
script and assign your project name to the
PROJECT_NAME constant."
	error_exit "Configuration error"
fi

if [ ! -d $PROJECT_ROOT ]; then
	echo >&2 "
Project directory does not exist.
Perhaps project name (PROJECT_NAME) is misspelled."
	error_exit "Configuration error"
fi

if [ ! -d "$HTDOCS" ]; then
	echo >&2 "
The htdocs directory ($HTDOCS) does not exist.
Please rename/move your htdocs directory."
	error_exit "Configuration error"
fi

cd $PROJECT_ROOT || error_exit "cannot cd to $PROJECT_ROOT"

all_files_flag=
while getopts ":ha" opt; do
	case $opt in
		a )	# upload all files
			all_files_flag="yes"
			;;

		h )	helptext
			graceful_exit
			;;

		* )	usage
			clean_up
			exit 1
	esac
done


##### Main Logic #####

echo -e "\n$PROGNAME version $VERSION\n"

# Fix permissions on all web site files to make them readable.
# This is in case you have a default umask set to something
# restrictive, like I do.

echo -n "Setting file permissions..."
find $HTDOCS -type f -not -perm 644 -exec chmod 644 {} \;
echo "Done."

# Temporarily rename timestamp if all_files_flag is set

if [ -n "$all_files_flag" ]; then
	if [ -f $TIME_STAMP ]; then
		echo "Uploading all files"
		rm -f $TIME_STAMP.tmp
		mv $TIME_STAMP $TIME_STAMP.tmp || error_exit "cannot rename time stamp"
	fi
fi

# Construct tarball with all files newer that last upload

echo -n "Creating website tarball..."

if [ -f $TIME_STAMP ]; then

	# If timestamp exists, only archive files newer than timestamp.
	
	tar czf $ARCHIVE --newer "$(date -r $TIME_STAMP)" htdocs || error_exit "cannot create archive file"
else
	tar czf $ARCHIVE htdocs || error_exit "cannot create archive file"
fi

echo "Done."


# Transfer tarball to SourceForge.net

echo "Starting file transfer to SourceForge.net..."
scp $ARCHIVE ${SF_USER}@shell1.sourceforge.net:${SF_PROJECT_DIR} || error_exit "error transfering file"
echo "Transfer complete."

# Install web site from tarball

echo -e "\nInstalling web site..."
ssh $SF_USER@shell1.sourceforge.net "cd $SF_PROJECT_DIR ; tar xzvf $ARCHIVE" || error_exit "error installing web site"
echo -e "\nUpdated web site installed.\n"

# If all went well, update timestamp

if [ -f $TIME_STAMP.tmp ]; then
	mv $TIME_STAMP.tmp $TIME_STAMP
fi
touch $TIME_STAMP

graceful_exit


syntax highlighted by Code2HTML, v. 0.9.1