#!/bin/sh
########################################################
# Shell script to create content bundles               #
# Author: Abhishek Singh <abhishek.singh@olenepal.org> #
########################################################

set -e
set -u

# Function to show help
showhelp() {
	echo "---------- HELP ----------"
	echo "Usage Syntax: ./makecontent.sh <path_to_output_directory>"
	echo "Note: Please substitude <path_to_output_directory> with actual path on the filesystem. The path should be asolute."
	echo "The script will automatically create a \"content\" directory inside the output directory."
	echo "All the other arguments to the script than the first argument will not be used."
	echo "--------------------------"
}	

# Function to make link changes
makeLinkChange() {
	#echo "Modifying Links in "$1
	sed -i -e "s/\/css/\/fez\/css/" $1
	sed -i -e "s/\/images/\/fez\/images/" $1
	sed -i -e "s/href=\"\/\"/href=\"http:\/\/schoolserver\/fez\/\"/" $1
	#sed -i -e "s/<a class=\"white_link\" style=\"text-decoration: none;\" href=\"/\"/<a class=\"white_link\" style=\"text-decoration: none;\" href=\"http://schoolserver\/fez\/\"/"
	sed -i -e "s/\/feedback\/feedback.php/\/fez\/feedback\/feedback.php/" $1
	sed -i -e "s/{\$rel_url}/\/fez\//" $1
	sed -i -e "s/www.pustakalaya.org/schoolserver\/fez\//" $1
	sed -i -e "s/\/help.php/http:\/\/schoolserver\/fez\/help.php/" $1
	sed -i -e "s/\/adv_search/\/fez\/adv_search/" $1
}

# Function to set the root of / and /images relative to /fez
setRootToFez() {
	if [ -f $1 ]
		then
		sed -i -e 's/href=\"\/\"/href=\"\/fez\/\"/' $1
		sed -i -e 's/src=\"\/images/src=\"\/fez\/images/' $1
	fi
}

# Check if the argument to output directory is provided
if [ $# -eq 0 ] ; then
	echo "You need to provide an argument to the script."
	showhelp
	exit 1
fi

# Check if the first argument is "help"
if [ $1 == "help" ] ; then
	showhelp
else
	if [ -d $1 ] ; then
		if [ $1 == "." ]
		then
			curloc=$(pwd)
		else			
			curloc=$1
		fi
		echo "The output directory is set to "$curloc"/content/."
	else
		echo "The output directory does not exist. The script will automatically create it."
	fi
fi

########################################################
# Definition part goes here                            #
# You can modify the variables here                    #
# It is suggested NOT TO MODIFY other parts of script  #
########################################################

# Define the current working directory
work_DIR=/tmp/content/
# Define the output directory where all the content will be placed. It comes as an argument to the script
output_DIR=$curloc/content/
# Get the current working directory
crD=$(pwd)


########################################################
# Creation of necessary directory                      #
########################################################

# Remove the working directory if it already exists
echo "Removing the working directory."
if [ -d $work_DIR ] ; then
	rm -Rf $work_DIR
fi

# Remove the output directory if it already exists
echo "Removing the output directory."
if [ -d $output_DIR ] ; then
	rm -Rf $output_DIR
fi

# Create the working directory
echo "Creating the working directory."
mkdir $work_DIR


########################################################
# Obtaining content from backup server and hg repos    #
########################################################

# Changes Directory to working directory so that all the files get downloaded into that specific directory
cd $work_DIR

# Downloads the fedora data from the Pustakalaya Server (through scp)
echo "Copying fedora data to $work_DIR."
scp -r -q root@192.168.5.10:/backup/pustakalaya/daily.0/fedora/var/opt/fedora/data .

# Downloads the external content for fez from the Pustakalaya Server (through scp)
echo "Copying fez's external content to $work_DIR."
scp -r -q root@192.168.5.10:/backup/pustakalaya/daily.0/fez/var/www/fez/external-content .

# Modify links in Learn English Kids to point to proper resource URIs
cd external-content/static/LEKids/
sed -i -e 's/\/images/\/fez\/images/' css/pustakalaya.css
sed -i -e 's/href=\"\/\"/href=\"\/fez\/\"/' index.html
sed -i -e 's/\/images\/name_np.png/\/fez\/images\/name_np.png/' index.html
cd $work_DIR

# Modify links in ELearning for Kids to point to proper resource URIs
indexList=$(find external-content/static/ELKids/ -name "index.html")
for index in $indexList
do
	setRootToFez $index
done
#CSS Fix for ELKids
sed -i -e 's/\/images/\/fez\/images/' external-content/static/ELKids/css/pustakalaya.css


#Change the video files with that on the repository to match the scenario of NEXS
rsync --quiet -avz $crD/videos/ external-content/static/videos/

# Clones the fez repository which contains the frontend for fedora (through hg clone)
echo "Copying fez's content to $work_DIR."
hg clone -q http://hg.olenepal.org/fez/

# Clones the sabdakos which is integrated to fez (through hg clone)
echo "Obtaining sabdakos through mercurial and putting it in $work_DIR."
hg clone -q http://hg.olenepal.org/sabdakos/

# Downloads all the database dumps for fedora, fez, sabdakos (through scp)
echo "Copying database dumps to $work_DIR."
scp -r -q -p root@192.168.5.10:/backup/pustakalaya/daily.0/DBdump/root/DBdump/ .

# Renames the fedora data directory to "nexc-fedora" to match to content update script's requirement
mv data nexc-fedora

# Removes all the database dumps that are old than one day (i.e. keeps only the most current db dump)
echo "Manipulating database dumps."
cd DBdump

find . -name "*.sql.gz" -mtime +0 -exec rm -f {} \;

# Removes all the other database dumps except for fedora, fez and sabdakos
find . -name "balwiki*.sql.gz" -exec rm -f {} \;
find . -name "endict*.sql.gz" -exec rm -f {} \;

# Extracts the gz archives to get *.sql files
fdsqlgz=$(ls fedora*.sql.gz)
gunzip $fdsqlgz
fezsqlgz=$(ls fez*.sql.gz)
gunzip $fezsqlgz
sabdakossqlgz=$(ls sabdakos*.sql.gz)
gunzip $sabdakossqlgz
#vidsqlgz=$(ls video_data*.sql.gz)
#gunzip $vidsqlgz
extdatasqlgz=$(ls ext_data*.sql.gz)
gunzip $extdatasqlgz

# Renames the sql files to appropriate names
fdsql=$(ls fedora*.sql)
mv $fdsql nexc-fedora.sql
fezsql=$(ls fez*.sql)
mv $fezsql nexc-fez.sql
sabdakossql=$(ls sabdakos*.sql)
mv $sabdakossql nexc-sabdakos.sql
#vidsql=$(ls video_data*.sql)
#mv $vidsql nexc-video-data.sql
extdatasql=$(ls ext_data*.sql)
mv $extdatasql nexc-ext-data.sql

# Convert the databse dumps to latin1 because the NEXS still has collation and character sets in latin1
#sed -i -e 's/utf8/latin1/g' nexc-fedora.sql
#sed -i -e 's/utf8/latin1/g' nexc-fez.sql

# Copies all those sql files to their parent directory
cp nexc-fedora.sql ../
cp nexc-fez.sql ../
cp nexc-sabdakos.sql ../
#cp nexc-video-data.sql ../
cp nexc-ext-data.sql ../
cd ..

########################################################
# Creating archives for contents                       #
########################################################

# Create tar.bz2 archive for fedora-data
echo "Creating archive for fedora-data(nexc-fedora.tar.bz2)"
tar -cjf nexc-fedora.tar.bz2 nexc-fedora/

# Creates tar.bz2 archive for fez
echo "Creating archive for fez (nexc-fez.tar.bz2)." 
cd fez
hg archive --prefix fez --type tbz2 nexc-fez.tar.bz2
cp nexc-fez.tar.bz2 ../
cd ..
#tar -cjf nexc-fez.tar.bz2 fez/

# Creates tar.bz2 archive for sabdakos
echo "Creating archive for sabdakos (nexc-sabdakos.tar.bz2)."
cd sabdakos
hg archive --prefix sabdakos --type tbz2 nexc-sabdakos.tar.bz2
cp nexc-sabdakos.tar.bz2 ../
cd ..

# Creates tar.bz2 archive for external-content
echo "Creating archive for external content (nexc-ext-content.tar.bz2)."
mv external-content nexc-external-content
tar -cjf nexc-ext-content.tar.bz2 nexc-external-content/


########################################################
# Generating checksum files                            #
########################################################

# Create a sha1 checksum file for fedora-data(nexc-fedora)
echo "Generating checksum file for fedora-data"
sha1sum nexc-fedora.tar.bz2 > nexc-fedora.tar.bz2.sha1

# Creates a sha1 checksum file for fez
echo "Generating checksum file for fez codebase."
sha1sum nexc-fez.tar.bz2 > nexc-fez.tar.bz2.sha1

# Creates a sha1 checksum file for sabdakos
echo "Generating checksum file for sabdakos codebase."
sha1sum nexc-sabdakos.tar.bz2 > nexc-sabdakos.tar.bz2.sha1

# Create a sha1 checksum file for external content
echo "Generating checksum file for external content."
sha1sum nexc-ext-content.tar.bz2 > nexc-ext-content.tar.bz2.sha1

# Creates a sha1 checksum file for fedora database dump
echo "Generating checksum file for fedora sql dump."
sha1sum nexc-fedora.sql > nexc-fedora.sql.sha1

# Creates a sha1 checksum file for fez database dump
echo "Generating checksum file for fez sql dump."
sha1sum nexc-fez.sql > nexc-fez.sql.sha1

# Creates a sha1 checksum file for sabdakos database dump
echo "Generating checksum file for sabdakos sql dump."
sha1sum nexc-sabdakos.sql > nexc-sabdakos.sql.sha1

# Create a sha1 checksum file for video database dump
echo "Generating checksum file for extra data sql dump."
sha1sum nexc-ext-data.sql > nexc-ext-data.sql.sha1

########################################################
# Copying the processed content to output directory    #
########################################################

# Create the output directory
echo "Creating output directory (if it does not exist)."
mkdir -p $output_DIR

# Copies the fedora data (nexc-fedora) to the output directory
echo "Copying codebase to $output_DIR."
cp nexc*.bz2 $output_DIR
echo "Copying database dumps to $output_DIR."
cp nexc*.sql $output_DIR
echo "Copying checksum files to $output_DIR."
cp nexc*.sha1 $output_DIR

echo "Creation of content complete."
exit 0
