#!/bin/sh

# phpPgAdmin upgrading and installing utility


echo
echo "***************************************************"
echo -e "*   \033[1;37mphpPgAdmin upgrading and installing utility\033[0m   *"
echo "***************************************************"
echo


if [ "$1" = "" ]; then
	INSTALL_VERSION=4.1.2
else
	INSTALL_VERSION=$1
fi


echo "Use which mirror to pull 'phpPgAdmin-$INSTALL_VERSION.tar.gz' from?"
echo -n "[http://jaist.dl.sourceforge.net/sourceforge/phppgadmin/] "
read MIRROR

if [ "${MIRROR}" = "" ]
then
	MIRROR=http://jaist.dl.sourceforge.net/sourceforge/phppgadmin/
fi



echo -n "Where should we install phpPgAdmin locally? [/var/www/html/phpPgAdmin] "
read INSTALL_DIRECTORY

if [ "${INSTALL_DIRECTORY}" = "" ]
then
	INSTALL_DIRECTORY=/var/www/html/phpPgAdmin
fi


MODE=1

until [ -e ${INSTALL_DIRECTORY} ]
do
	_save_inst_dir=$INSTALL_DIRECTORY

	echo ""
	echo "Can not locate a copy of phpPgAdmin in ${INSTALL_DIRECTORY} to upgrade.."
	echo "If you would like to install phpPgAdmin to specified directory, hit enter."
	echo "If you would like to specify a different directory to upgrade from, enter it now."
	read INSTALL_DIRECTORY

	if [ "${INSTALL_DIRECTORY}" = "" ]
	then
		MODE=2
		INSTALL_DIRECTORY=$_save_inst_dir
		break;
	fi

	unset _save_inst_dir
done


# if upgrading, backup old files
if [ $MODE = 1 ]
then
	BACKUP_DIRECTORY="$INSTALL_DIRECTORY-backup"

	until [ ! -d $BACKUP_DIRECTORY ]
	do
		echo
		echo "The backup location; $BACKUP_DIRECTORY already exists. Please specify a new location for your phpPgAdmin."
		echo -n "New Backup Location: "
		read BACKUP_DIRECTORY
	done

	mv $INSTALL_DIRECTORY $BACKUP_DIRECTORY

fi


mkdir ${INSTALL_DIRECTORY}
rm -fr ${INSTALL_DIRECTORY}/*

# download new tarball
cd ${INSTALL_DIRECTORY}
wget -q ${MIRROR}phpPgAdmin-${INSTALL_VERSION}.tar.gz

# did we get the file?
if [ ! -e "${INSTALL_DIRECTORY}/phpPgAdmin-${INSTALL_VERSION}.tar.gz" ]
then
	echo
	echo "Can not import ${MIRROR}phpPgAdmin-${INSTALL_VERSION}.tar.gz"
	echo "Check to make sure '${INSTALL_VERSION}' is a real version. Exiting..."
	echo
	exit 0;
fi

tar xzf phpPgAdmin-${INSTALL_VERSION}.tar.gz -C ${INSTALL_DIRECTORY}

# need to copy extracted version directory into install directory & rm tarball
mv -f ${INSTALL_DIRECTORY}/phpPgAdmin-${INSTALL_VERSION}/* ${INSTALL_DIRECTORY}
rm -f ${INSTALL_DIRECTORY}/phpPgAdmin-${INSTALL_VERSION}.tar.gz
rm -fr ${INSTALL_DIRECTORY}/phpPgAdmin-${INSTALL_VERSION}


# if upgrading...
if [ $MODE = 1 ]
then
	# restore old config.inc.php IF old one exists
	if [ -e ${BACKUP_DIRECTORY}/conf/config.inc.php ]
	then
		cp ${BACKUP_DIRECTORY}/conf/config.inc.php ${INSTALL_DIRECTORY}/conf
	else
		search="\$conf['servers'][0]['host'] = '';"
		replace="\$conf['servers'][0]['host'] = 'localhost';"
		/usr/bin/perl -pi -e "s/${search}/${replace}/" ${INSTALL_DIRECTORY}/conf/config.inc.php
		echo "confing.inc.php not found, using a new one.";
	fi

	echo
	echo "Done!"
	echo

	echo "If this is a fresh install, please add"
	echo
	echo -e "\033[1;37mAlias /phpPgAdmin /var/www/html/phpPgAdmin/\033[0m"
	echo -e "\033[1;37mAlias /phppgadmin /var/www/html/phpPgAdmin/\033[0m"
	echo
	echo "to your httpd.conf and restart Apache."
	echo

	echo "Now, check and make sure phpPgAdmin is working."
	echo "Hit 'y' to remove old version backups ($BACKUP_DIRECTORY)."
	echo "Hit 'n' to leave the backups."
	echo -n "(y/n) [y] "
	read bool_rmback

	case "$bool_rmback" in
		[nN])	;;
		*)	echo "Removing backups..."
			rm -fr $BACKUP_DIRECTORY
			;;
	esac

fi

echo ""
echo "Successfully installed phpPgAdmin ${INSTALL_VERSION} to ${INSTALL_DIRECTORY}!"
echo ""

exit 0;

