#  Copyright (C) 2005-2009 BetaCONCEPT LP.
# 
#  This file is part of BetaCMS.
# 
#  BetaCMS is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
# 
#  BetaCMS 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 Lesser General Public License for more details.
 
#  You should have received a copy of the GNU Lesser General Public License
#  along with BetaCMS.  If not, see <http://www.gnu.org/licenses/>.
 
JBOSS_DIR=/opt/jboss-4.2.3.GA
REPOSITORY_DIR=/opt/BetaCMS_Repository
JBOSS_USER=jboss


#DO NOT ALTER THE FOLLOWING SETTINGS
#UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING
JBOSS_DEPLOY_DIR=$JBOSS_DIR/server/default/deploy
JBOSS_SERVER_CONF_DIR=$JBOSS_DIR/server/default/conf

#Values for these properties are provided automatically from 
#pom.xml during package building
JACKRABBIT_JCA_CURRENT_VERSION=${jackrabbit-jca-version}

echo "Provide repository identifier (Only Latin characters [A-Z, a-z] without spaces )"
read NEW_REPOSITORY_ID

#Test if input is provided
if [ ! -n "$NEW_REPOSITORY_ID" ]; then
  echo "You have to provide an non empty repository identifier"
  exit
fi

REGEX=[A-Z,a-z]+

if [[ ! $NEW_REPOSITORY_ID =~ $REGEX ]]; then
	echo "Repository identifier should contain only latin characters (A-Z, a-z) without spaces"
	exit
fi

echo "Provide a Greek label for repository $NEW_REPOSITORY_ID"
read NEW_REPOSITORY_NAME_EL

if [ ! -n "$NEW_REPOSITORY_NAME_EL" ]; then
	NEW_REPOSITORY_NAME_EL=$NEW_REPOSITORY_ID
fi

echo "Provide an English label for repository $NEW_REPOSITORY_ID"
read NEW_REPOSITORY_NAME_EN

if [ ! -n "$NEW_REPOSITORY_NAME_EN" ]; then
	NEW_REPOSITORY_NAME_EN=$NEW_REPOSITORY_ID
fi

serverAliasURL=localhost:8080

echo "PostgreSQL username (default postgres)" 
read postgresUsername

if [ ! -n "$postgresUsername" ]; then
	postgresUsername=postgres
fi

echo "PostgreSQL password" 
read postgresPassword

if [ ! -n "$postgresPassword" ]; then
	postgresPassword=postgres
fi

echo "PostgreSQL Server Host or IP (default localhost, without http:// prefix)"
read postgresHost

if [ ! -n "$postgresHost" ]; then
	postgresHost=localhost
fi

echo "PostgreSQL Server Port (default 5432)"
read postgresPort

if [ ! -n "$postgresPort" ]; then
	postgresPort=5432
fi

echo "Provide directory where PostgreSQL createdb script is located (default /usr/local/bin)"
read createdbDirectory

if [ ! -n "$createdbDirectory" ]; then
	createdbDirectory=/usr/local/bin
fi



echo "You have provided the following values"
echo "Repository Identifier				: $NEW_REPOSITORY_ID"
echo "Repository Greek Label			: $NEW_REPOSITORY_NAME_EL"
echo "Repository English Label			: $NEW_REPOSITORY_NAME_EN"
echo "PostgreSQL User name				: $postgresUsername"
echo "PostgreSQL Server Host or IP		: $postgresHost"
echo "PostgreSQL Server Port			: $postgresPort"
echo "PostgreSQL createdb script dir	: $createdbDirectory"

echo "Continue ? (Y=yes)"
read continueInstall

if [[ "Y" != "$continueInstall" ]]; then
	exit
fi

#Add default datasources for repository
sed s/REPOSITORY/$NEW_REPOSITORY_ID/g < ./datasources/betacmsDB-template-ds.xml > $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.xml

sed s/POSTGRESQLHOST/$postgresHost/g < $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.xml > $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.tmp.xml
mv $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.tmp.xml $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.xml

sed s/POSTGRESQLPORT/$postgresPort/g < $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.xml > $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.tmp.xml
mv $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.tmp.xml $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.xml

sed s/POSTGRESQLUSER/$postgresUsername/g < $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.xml > $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.tmp.xml
mv $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.tmp.xml $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.xml

sed s/POSTGRESQLPASSWORD/$postgresPassword/g < $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.xml > $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.tmp.xml
mv $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.tmp.xml $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.xml


echo "Created file $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.xml"

sed s/REPOSITORY/$NEW_REPOSITORY_ID/g <./datasources/jackrabbit-template-ds.xml > $JBOSS_DEPLOY_DIR/jackrabbit-$NEW_REPOSITORY_ID-ds.xml
sed s/JACKRABBIT_JCA_CURRENT_VERSION/$JACKRABBIT_JCA_CURRENT_VERSION/g < $JBOSS_DEPLOY_DIR/jackrabbit-$NEW_REPOSITORY_ID-ds.xml > $JBOSS_DEPLOY_DIR/jackrabbit-$NEW_REPOSITORY_ID-ds.tmp.xml
mv $JBOSS_DEPLOY_DIR/jackrabbit-$NEW_REPOSITORY_ID-ds.tmp.xml $JBOSS_DEPLOY_DIR/jackrabbit-$NEW_REPOSITORY_ID-ds.xml
echo "Created file $JBOSS_DEPLOY_DIR/jackrabbit-$NEW_REPOSITORY_ID-ds.xml"

#Create root directory for ALL repositories 
if [ ! -d $REPOSITORY_DIR ]; then mkdir $REPOSITORY_DIR ; fi

#Create repository home directory and necessary files
if [ ! -d $REPOSITORY_DIR/$NEW_REPOSITORY_ID ]; then 
	mkdir $REPOSITORY_DIR/$NEW_REPOSITORY_ID ; 
	echo "Created directory $REPOSITORY_DIR/$NEW_REPOSITORY_ID"
else
	echo "Directory $REPOSITORY_DIR/$NEW_REPOSITORY_ID already exists"
fi

if [ ! -d $REPOSITORY_DIR/$NEW_REPOSITORY_ID/repository ]; then mkdir $REPOSITORY_DIR/$NEW_REPOSITORY_ID/repository ; fi

if [ ! -f $REPOSITORY_DIR/$NEW_REPOSITORY_ID/repository.xml ]; then 
	#Add betacms keyword to repository.xml configuration file and add it to beatcms repository home directory
	sed s/REPOSITORY/$NEW_REPOSITORY_ID/g < ./configuration/repository-template.xml > $REPOSITORY_DIR/$NEW_REPOSITORY_ID/repository.xml
	echo "Created file $REPOSITORY_DIR/$NEW_REPOSITORY_ID/repository.xml"
fi

if [ ! -d $REPOSITORY_DIR/$NEW_REPOSITORY_ID/repository/betacms_definition_schemas ]; then 
	mkdir $REPOSITORY_DIR/$NEW_REPOSITORY_ID/repository/betacms_definition_schemas 
	echo "Created directory $REPOSITORY_DIR/$NEW_REPOSITORY_ID/repository/betacms_definition_schemas "
	
	#Copy schemas
	cp ./schemas-default/*.xsd $REPOSITORY_DIR/$NEW_REPOSITORY_ID/repository/betacms_definition_schemas/.
	cp ./schemas-default/identityStore/*.xsd $REPOSITORY_DIR/$NEW_REPOSITORY_ID/repository/betacms_definition_schemas/.
	echo "Copied all XSDs to directory $REPOSITORY_DIR/$NEW_REPOSITORY_ID/repository/betacms_definition_schemas/"
else
	echo "Directory $REPOSITORY_DIR/$NEW_REPOSITORY_ID/repository/betacms_definition_schemas already exists. No XSD file is copied"

fi

#Update betacms-repositories-configuration.xml file to load newly created repository
SEARCH_PATTERN=id=\"$NEW_REPOSITORY_ID\"
REPOSITORY_ALREADY_CONFIGURED=`grep $SEARCH_PATTERN $JBOSS_SERVER_CONF_DIR/betacms-repositories-configuration.xml`

if [ ! -n "$REPOSITORY_ALREADY_CONFIGURED" ]; then
#Add repository id
REPOSITORY_CONFIGURATION_FILE_PATH=$NEW_REPOSITORY_ID-repository-configuration-entry.xml
sed s/REPOSITORY/$NEW_REPOSITORY_ID/g < ./configuration/betacms-repositories-configuration-entry-template.xml > $REPOSITORY_CONFIGURATION_FILE_PATH

#Ask for server alias url
sed s@HOST-URL-INCLUDING-HTTP@http://$serverAliasURL@g < $REPOSITORY_CONFIGURATION_FILE_PATH > $REPOSITORY_CONFIGURATION_FILE_PATH.tmp
mv $REPOSITORY_CONFIGURATION_FILE_PATH.tmp $REPOSITORY_CONFIGURATION_FILE_PATH

sed s/REP_EL_NAME/$NEW_REPOSITORY_NAME_EL/g < $REPOSITORY_CONFIGURATION_FILE_PATH > $REPOSITORY_CONFIGURATION_FILE_PATH.tmp
mv $REPOSITORY_CONFIGURATION_FILE_PATH.tmp $REPOSITORY_CONFIGURATION_FILE_PATH

sed s/REP_EN_NAME/$NEW_REPOSITORY_NAME_EN/g < $REPOSITORY_CONFIGURATION_FILE_PATH > $REPOSITORY_CONFIGURATION_FILE_PATH.tmp
mv $REPOSITORY_CONFIGURATION_FILE_PATH.tmp $REPOSITORY_CONFIGURATION_FILE_PATH

cat $JBOSS_SERVER_CONF_DIR/betacms-repositories-configuration.xml $REPOSITORY_CONFIGURATION_FILE_PATH > $JBOSS_SERVER_CONF_DIR/betacms-repositories-configuration.xml.tmp

#Append betacms-repositoryies-configuration.xml with new entry
sed '/<\/ns:betacmsRepositories>/ d' < $JBOSS_SERVER_CONF_DIR/betacms-repositories-configuration.xml.tmp > $JBOSS_SERVER_CONF_DIR/betacms-repositories-configuration.xml.tmp2
mv $JBOSS_SERVER_CONF_DIR/betacms-repositories-configuration.xml.tmp2 $JBOSS_SERVER_CONF_DIR/betacms-repositories-configuration.xml

echo "</ns:betacmsRepositories>" >> $JBOSS_SERVER_CONF_DIR/betacms-repositories-configuration.xml

rm -f $JBOSS_SERVER_CONF_DIR/betacms-repositories-configuration.xml.tmp
rm -f $REPOSITORY_CONFIGURATION_FILE_PATH

echo "Updated configuration file $JBOSS_SERVER_CONF_DIR/betacms-repositories-configuration.xml with entry for repository $NEW_REPOSITORY_ID"

else
	echo "Configuration entry for repository $NEW_REPOSITORY_ID already exists in  file $JBOSS_SERVER_CONF_DIR/betacms-repositories-configuration.xml"
fi

#Change user rights
chown -R $JBOSS_USER $REPOSITORY_DIR/$NEW_REPOSITORY_ID
chown -R $JBOSS_USER $JBOSS_DEPLOY_DIR/jackrabbit-$NEW_REPOSITORY_ID-ds.xml
chown -R $JBOSS_USER $JBOSS_DEPLOY_DIR/betacmsDB-$NEW_REPOSITORY_ID-ds.xml

echo ssh $postgresUsername@$postgresHost $createdbDirectory/createdb -E UNICODE $NEW_REPOSITORY_ID

ssh $postgresUsername@$postgresHost $createdbDirectory/createdb -E UNICODE $NEW_REPOSITORY_ID

echo "In case of database creation failure, unless database already exists,"
echo "you need to manually create a database in PostgreSQL using executing  the following command"
echo " createdb -E UNICODE $NEW_REPOSITORY_ID"
echo "You have to be connected to server $postgresHost as user $postgresUsername"

