
#!/bin/sh

# Copyright (c) Members of the EGEE Collaboration. 2004 
# See http://eu-egee.org/partners/ for details on the copyright holders
# For license conditions see the license file or http://eu-egee.org/license.html

# glite-security-utils_installer v. 1.1.1
#
# The glite-security-utils_installer installs the gLite Security Utilities
#
# Usage: glite-security-utils_installer [-u|-v|--help]
#        -u          uninstall
#        -v          print version
#        --help      print script usage info
# Return codes: 0 - Ok
#               1 - if a file could not be downloaded

###############################################################################

#Parse the RPMLIST to strip out the RPMS that are already installed
function parseRPMList()
{
        newRPMLIST=""
        localRPMLIST=`rpm -qa`
        for i in $RPMLIST
        do
                g=`echo $i | sed -e 's/\.i386\.rpm//g'`
                g=`echo $g | sed -e 's/\.noarch\.rpm//g'`
                if [ -z "`echo $localRPMLIST | grep $g`" ]; then
                        newRPMLIST="${newRPMLIST} $i"
                else
                        echo "$i is already installed. It will be skipped."
                fi
        done
                                                                                                                                                             
        RPMLIST=$newRPMLIST
}

function parseUserRPMList()
{
        newRPMLIST=""
        localRPMLIST=`rpm -qa --dbpath $basedir/.rpm`
        for i in $RPMLIST
        do
                g=`echo $i | sed -e 's/\.i386\.rpm//g'`
                g=`echo $g | sed -e 's/\.noarch\.rpm//g'`
                if [ -z "`echo $localRPMLIST | grep $g`" ]; then
                        newRPMLIST="${newRPMLIST} $i"
                else
                        echo "$i is already installed. It will be skipped."
                fi
        done
                                                                                                                                                             
        RPMLIST=$newRPMLIST
}

#Parse the SCRIPTLIST to execute all scripts
function parseScriptList()
{
        for i in $SCRIPTLIST
        do
        		if [ "$INSTALL" = "true" ]; then
        			if [ "${basedir}" != "" ]; then
        				$i --basedir $basedir
        			else
	                	$i
	                fi
	            else
	            	$i -u
	            fi
        done
}

#Downloads and install the module RPMS
function install()
{

	INSTALL=true
	
	version
	echo
	echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	echo x Please wait, downloading the gLite Security Utilities... x
	echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	echo

	mkdir -p glite-security-utils
	cd glite-security-utils

	# Download CAs	
	wget -r --level=1 -nd -nv -N http://glite.web.cern.ch/glite/packages/externals/bin/rhel30/RPMS/CA_Certs
	if [ ! "$?" == "0" ]
	then
		echo 
		echo ERROR: One or more CA certificates could not be downloaded!
		exit 1
	fi
	RPMLIST="$RPMLIST `cat ca_list`"


	# Download global dependencies	
	
		
		
wget -N -nv http://glite.web.cern.ch/glite/packages/externals/bin/rhel30/RPMS/ca_policy_eugridpma-1.0-1.noarch.rpm
if [ ! -f "ca_policy_eugridpma-1.0-1.noarch.rpm" ]
then
	echo 
	echo ERROR: ca_policy_eugridpma-1.0-1.noarch.rpm could not be downloaded!
	exit 1
fi
RPMLIST="$RPMLIST ca_policy_eugridpma-1.0-1.noarch.rpm"
			

        
wget -N -nv http://glite.web.cern.ch/glite/packages/externals/bin/rhel30/RPMS/edg-mkgridmap-2.5.1-1_sl3.noarch.rpm
if [ ! -f "edg-mkgridmap-2.5.1-1_sl3.noarch.rpm" ]
then
	echo 
	echo ERROR: edg-mkgridmap-2.5.1-1_sl3.noarch.rpm could not be downloaded!
	exit 1
fi
RPMLIST="$RPMLIST edg-mkgridmap-2.5.1-1_sl3.noarch.rpm"
			
        
wget -N -nv http://glite.web.cern.ch/glite/packages/externals/bin/rhel30/RPMS/glite-security-mkgridmap-2.4.2-1.noarch.rpm
if [ ! -f "glite-security-mkgridmap-2.4.2-1.noarch.rpm" ]
then
	echo 
	echo ERROR: glite-security-mkgridmap-2.4.2-1.noarch.rpm could not be downloaded!
	exit 1
fi
RPMLIST="$RPMLIST glite-security-mkgridmap-2.4.2-1.noarch.rpm"
			
        
wget -N -nv http://glite.web.cern.ch/glite/packages/externals/bin/rhel30/RPMS/glite-security-mkgridmap-conf-2.4.2-1.noarch.rpm
if [ ! -f "glite-security-mkgridmap-conf-2.4.2-1.noarch.rpm" ]
then
	echo 
	echo ERROR: glite-security-mkgridmap-conf-2.4.2-1.noarch.rpm could not be downloaded!
	exit 1
fi
RPMLIST="$RPMLIST glite-security-mkgridmap-conf-2.4.2-1.noarch.rpm"
			
        
wget -N -nv http://glite.web.cern.ch/glite/packages/externals/bin/rhel30/RPMS/fetch-crl-2.0-1.noarch.rpm
if [ ! -f "fetch-crl-2.0-1.noarch.rpm" ]
then
	echo 
	echo ERROR: fetch-crl-2.0-1.noarch.rpm could not be downloaded!
	exit 1
fi
RPMLIST="$RPMLIST fetch-crl-2.0-1.noarch.rpm"
			
        
wget -N -nv http://glite.web.cern.ch/glite/packages/externals/bin/rhel30/RPMS/perl-IO-Socket-SSL-0.94-0.dag.rhel3.noarch.rpm
if [ ! -f "perl-IO-Socket-SSL-0.94-0.dag.rhel3.noarch.rpm" ]
then
	echo 
	echo ERROR: perl-IO-Socket-SSL-0.94-0.dag.rhel3.noarch.rpm could not be downloaded!
	exit 1
fi
RPMLIST="$RPMLIST perl-IO-Socket-SSL-0.94-0.dag.rhel3.noarch.rpm"
			
        
wget -N -nv http://glite.web.cern.ch/glite/packages/externals/bin/rhel30/RPMS/perl-Net-LDAP-0.2701-1.dag.rhel3.noarch.rpm
if [ ! -f "perl-Net-LDAP-0.2701-1.dag.rhel3.noarch.rpm" ]
then
	echo 
	echo ERROR: perl-Net-LDAP-0.2701-1.dag.rhel3.noarch.rpm could not be downloaded!
	exit 1
fi
RPMLIST="$RPMLIST perl-Net-LDAP-0.2701-1.dag.rhel3.noarch.rpm"
			
		
wget -N -nv http://glite.web.cern.ch/glite/packages/externals/bin/rhel30/RPMS/perl-Crypt-SSLeay-0.51-4.i386.rpm
if [ ! -f "perl-Crypt-SSLeay-0.51-4.i386.rpm" ]
then
	echo 
	echo ERROR: perl-Crypt-SSLeay-0.51-4.i386.rpm could not be downloaded!
	exit 1
fi
RPMLIST="$RPMLIST perl-Crypt-SSLeay-0.51-4.i386.rpm"
			 	
        
wget -N -nv http://glite.web.cern.ch/glite/packages/externals/bin/rhel30/RPMS/perl-Net-SSLeay-1.23-0.dag.rhel3.i386.rpm
if [ ! -f "perl-Net-SSLeay-1.23-0.dag.rhel3.i386.rpm" ]
then
	echo 
	echo ERROR: perl-Net-SSLeay-1.23-0.dag.rhel3.i386.rpm could not be downloaded!
	exit 1
fi
RPMLIST="$RPMLIST perl-Net-SSLeay-1.23-0.dag.rhel3.i386.rpm"
			
        
wget -N -nv http://glite.web.cern.ch/glite/packages/externals/bin/rhel30/RPMS/perl-TermReadKey-2.20-12.i386.rpm
if [ ! -f "perl-TermReadKey-2.20-12.i386.rpm" ]
then
	echo 
	echo ERROR: perl-TermReadKey-2.20-12.i386.rpm could not be downloaded!
	exit 1
fi
RPMLIST="$RPMLIST perl-TermReadKey-2.20-12.i386.rpm"
			
        
wget -N -nv http://glite.web.cern.ch/glite/packages/externals/bin/rhel30/RPMS/perl-Convert-ASN1-0.18-0.rhel3.dag.noarch.rpm
if [ ! -f "perl-Convert-ASN1-0.18-0.rhel3.dag.noarch.rpm" ]
then
	echo 
	echo ERROR: perl-Convert-ASN1-0.18-0.rhel3.dag.noarch.rpm could not be downloaded!
	exit 1
fi
RPMLIST="$RPMLIST perl-Convert-ASN1-0.18-0.rhel3.dag.noarch.rpm"
			
	

	# Download glite-security-utils-service scripts from repository
		
	cd ..
			
	cd glite-security-utils
		

	# Download glite-security-utils-service dependencies RPMS from repository
		

	# Download glite-security-utils-service RPMS from repository
		
				
wget -N -nv http://glite.web.cern.ch/glite/packages/R1.4/R20050916/bin/rhel30/noarch/RPMS/glite-config-1.4.11-1.noarch.rpm
if [ ! -f "glite-config-1.4.11-1.noarch.rpm" ]
then
	echo 
	echo ERROR: glite-config-1.4.11-1.noarch.rpm could not be downloaded!
	exit 1
fi
RPMLIST="$RPMLIST glite-config-1.4.11-1.noarch.rpm"
			
				
wget -N -nv http://glite.web.cern.ch/glite/packages/R1.4/R20050916/bin/rhel30/noarch/RPMS/glite-security-utils-config-1.1.1-1.noarch.rpm
if [ ! -f "glite-security-utils-config-1.1.1-1.noarch.rpm" ]
then
	echo 
	echo ERROR: glite-security-utils-config-1.1.1-1.noarch.rpm could not be downloaded!
	exit 1
fi
RPMLIST="$RPMLIST glite-security-utils-config-1.1.1-1.noarch.rpm"
			
			
	# Install all RPMS
	echo
	echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	echo x Please wait, installing the gLite Security Utilities... x
	echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	echo
	cd ..
	parseScriptList
	cd glite-security-utils
	parseRPMList
	if [ ! -z "$RPMLIST" ]; then
		if [ `id -u` == 0 ]; then		
			rpm -Uvh $RPMLIST
			rpm_return=$?
		else
			echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
			echo x User installation
			echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
			if [ !  -d $basedir/.rpm ]; then
				mkdir -p $basedir/.rpm
				#cp -R /var/lib/rpm/* $basedir/.rpm
				rpm --initdb --dbpath $basedir/.rpm
				
				touch $basedir/.rpm/packages.sort
			fi
			rpm -qa | sort > $basedir/.rpm/packages.sort~
			diff  $basedir/.rpm/packages.sort $basedir/.rpm/packages.sort~ > /dev/null
			if [ $? != 0 ]; then
					#######################################
					#	Create system.rpm file
					#######################################
					echo Installer is inspecting your installation. It might take a while
					# creation of the system.rpm file
                    if [ -f $HOME/.rpmmacros ]; then
                    	mv $HOME/.rpmmacros $HOME/.rpmmacros~
                    fi
                    echo %_topdir        $basedir/.rpm/rpmbuild >> $HOME/.rpmmacros
                    mkdir -p $basedir/.rpm/rpmbuild/SPECS
                    mkdir -p $basedir/.rpm/rpmbuild/SOURCES
                    mkdir -p $basedir/.rpm/rpmbuild/BUILD
                    mkdir -p $basedir/.rpm/rpmbuild/RPMS
                    touch $basedir/.rpm/rpmbuild/SOURCES/system.tar.gz
					
					echo Verification of installed packages
					dot=0
					for package in `rpm -qa`
					do
						let dot++
						echo -n "."
						if [ $dot -ge 80 ]; then dot=0; echo ""; fi
	        			rpm -q --provides $package >> /tmp/provides
					done
					datestamp=`date +%Y%m%d%H%M`
					grep -v ">" /tmp/provides | sed -e 's/.*\/\(.*\)/\1/g' | sort | uniq > /tmp/provides.uniq
					split --lines 20 /tmp/provides.uniq /tmp/system
					echo
					echo Building of provides information for local rpm database
					dot=0
					for provides in `ls -1 /tmp/system*`
					do
						let dot++
						echo -n "#"
						if [ $dot -ge 80 ]; then dot=0; echo ""; fi
						name=`echo $provides | sed -e 's/\/tmp\///g'`
						#echo $name
						###############
						#       Create .spec file
						##############
						echo "Summary:Provides root installed symbols" > system.spec
						echo "Name:$name" >> system.spec
						echo "Version:0.1.0" >> system.spec
						echo "Release:$datestamp" >> system.spec
						echo "Copyright:Open Source EGEE License" >> system.spec
						echo "Vendor:EU EGEE project" >> system.spec
						echo "Group:System/Application" >> system.spec
						echo "BuildArch:noarch" >> system.spec
						echo "BuildRoot:%{_builddir}/%{name}-%{version}" >> system.spec
						echo "Source:system.tar.gz" >> system.spec
						echo "Provides:/bin/sh" >> system.spec
						echo "Provides:/usr/bin/env" >> system.spec
						echo "Provides:/usr/bin/perl" >> system.spec
						echo "Provides:/bin/env" >> system.spec
						echo "Provides:/sbin/ldconfig" >> system.spec
						echo "Provides:/sbin/install-info" >> system.spec
						echo "Provides:/bin/bash" >> system.spec
						echo "Provides:/bin/csh" >> system.spec
	
				#		grep -v ">" /tmp/provides | sed -e 's/.*\/\(.*\)/\1/g' | sort | uniq > /tmp/provides.uniq
						sed -e 's/\(.*\)/Provides:\1/g' $provides >> system.spec
						
						echo "%define debug_package %{nil}" >> system.spec
	
						echo "%description" >> system.spec
						echo "gLite Security Utilities installation" >> system.spec
						#echo "%prep" >> system.spec
						#echo "%setup -c" >> system.spec
						#echo "%build" >> system.spec
						#echo "%install" >> system.spec
						echo "%clean" >> system.spec
						echo "%pre" >> system.spec
						echo "%post" >> system.spec
						echo "%preun" >> system.spec
						echo "%postun" >> system.spec
						echo "%files" >> system.spec
						echo "%changelog" >> system.spec
						echo >> system.spec
	
						rpmbuild -bb system.spec > /dev/null 2>/dev/null
					done
					rm /tmp/system*
					if [ -f $HOME/.rpmmacros~ ]; then
						mv $HOME/.rpmmacros~ $HOME/.rpmmacros
					fi
					
					######################################
					#	Update system.rpm file in the database
					######################################
					rpm -U --dbpath $basedir/.rpm $basedir/.rpm/rpmbuild/RPMS/noarch/system*-0.1.0-$datestamp.noarch.rpm
					echo
					echo system examined successfully
					######################################
					#	Update package.list file
					#######################################
					mv $basedir/.rpm/packages.sort~ $basedir/.rpm/packages.sort
					#########################################
					#	Cleanup of not needed files
					#########################################
					rm -rf $basedir/.rpm/rpmbuild
					rm -f /tmp/provides /tmp/provides.uniq
			fi 
			
			parseUserRPMList
			if [ ! -z "$RPMLIST" ]; then
				rpm -Uvh --relocate /opt/glite=$basedir\
					 --relocate /opt/globus=$basedir\
					 --relocate /opt/gpt=$basedir\
			 		 --relocate /opt/edg=$basedir\
			 		 --relocate /usr=$basedir/usr\
			 		 --relocate /etc=$basedir/etc\
			 		 --nopost --dbpath $basedir/.rpm $RPMLIST
				rpm_return=$?
			else
				echo All required RPMS are already installed
				rpm_return=0
			fi
			
			if [ $rpm_return != 0 ]; then
				break
			fi
			###############################################
			#	Fixed hardcoded absolute path in .la files
			###############################################
			for la in `find $basedir -name "*.la"`
			do
				echo -n "."
    			REPL=`echo ${la} | sed -e 's/\(.*\)\/.*$/\1/g'`
    			sed -i~ -e "s|^libdir=.*|libdir=${REPL}|g"  ${la} 
			done
			echo
		fi
	else
		echo All required RPMS are already installed
		rpm_return=0
		fi
		if [ "$rpm_return" == "0" ]; then
			pbasedir=${basedir:-"/opt/glite"}
			echo
			echo Done!
			echo
			echo Before starting the Security Utilities, please create or update the
			echo configuration file $pbasedir/etc/config/glite-security-utils.cfg.xml
			echo and run the configuration script
			echo $pbasedir/etc/config/scripts/glite-security-utils-config.py --configure. A template
			echo is provided in $pbasedir/etc/config/templates/glite-security-utils.cfg.xml
		else
			echo
			echo
			echo An error occurred while installing the Security Utilities RPMS.
			echo Most likely one or more of the RPMS to be installed require
			echo additional dependencies or are older than already installed packages.
			echo Please refer to the rpm error message above for more details.
		fi
		echo
		echo Done!
		echo
		echo For more information refer to the gLite Installation and User Guides or to the gLite web site \(http:\/\/www.glite.org\)
		echo Please report problems and comments to the gLite Team at glite-bugs@cern.ch
	
		cd ..
		

}

###############################################################################
function uninstall()
{
	INSTALL=false
	
	version

	# Global dependencies	
	
		
		
RPMLIST="$RPMLIST ca_policy_eugridpma-1.0-1"
					

        
RPMLIST="$RPMLIST edg-mkgridmap-2.5.1-1_sl3"
					
        
RPMLIST="$RPMLIST glite-security-mkgridmap-2.4.2-1"
					
        
RPMLIST="$RPMLIST glite-security-mkgridmap-conf-2.4.2-1"
					
        
RPMLIST="$RPMLIST fetch-crl-2.0-1"
					
        
RPMLIST="$RPMLIST perl-IO-Socket-SSL-0.94-0.dag.rhel3"
					
        
RPMLIST="$RPMLIST perl-Net-LDAP-0.2701-1.dag.rhel3"
					
		
RPMLIST="$RPMLIST perl-Crypt-SSLeay-0.51-4"
					 	
        
RPMLIST="$RPMLIST perl-Net-SSLeay-1.23-0.dag.rhel3"
					
        
RPMLIST="$RPMLIST perl-TermReadKey-2.20-12"
					
        
RPMLIST="$RPMLIST perl-Convert-ASN1-0.18-0.rhel3.dag"
					
	

	# glite-security-utils-service scripts from repository
		

	# glite-security-utils-service dependencies RPMS from repository
		

	# glite-security-utils-service RPMS from repository
		
				
RPMLIST="$RPMLIST glite-config-1.4.11-1"
                                        
				
RPMLIST="$RPMLIST glite-security-utils-config-1.1.1-1"
                                        
			
		
	# Uninstall all RPMS
	echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	echo x  Please wait, uninstalling the gLite Security Utilities...   x
	echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	echo
	$SCRIPTLISTUn
	rpm -ev $RPMLIST
	if [ "$?" == "0" ]; then
		echo
		echo Done!
	else
		echo
		echo An error occurred while removing the Security Utilities RPMS.
		echo Most likely one or more of the RPMS to be removed have
		echo dependent packages.
		echo Please refer to the rpm error message above for more details.
	fi
}

###############################################################################
function usage()
{
	echo 
	echo Copyright \(c\) Members of the EGEE Collaboration. 2004 
	echo See http://eu-egee.org/partners/ for details on the copyright holders
	echo For license conditions see the license file or http://eu-egee.org/license.html
	echo 
	echo glite-security-utils_installer v. 1.1.1
	echo 
	echo The glite-security-utils_installer installs the gLite Security Utilities
	echo 
	echo Usage: glite-security-utils_installer \[-u\|-v\|--help\]
	echo -u          uninstall
	echo -v          print version
	echo --help      print script usage info
	echo --basedir	 set the base directory for the user installation
	echo			 it must be an absolute path
	echo 
	echo Return codes:
	echo 0 - Ok
	echo 1 - if a file could not be downloaded
	echo 
}

###############################################################################
function version
{
	echo 
	echo Copyright \(c\) Members of the EGEE Collaboration. 2004 
	echo See http://eu-egee.org/partners/ for details on the copyright holders
	echo For license conditions see the license file or http://eu-egee.org/license.html
	echo 
	echo glite-security-utils_installer v. 1.1.1
	echo 
}


RPMLIST=
SCRIPTLIST=
SCRIPTLISTUn=

###############################################################################
# Main

#while getopts uvh opt
#do
#	case $opt in
#		'u') uninstall
#		     exit 0	
#		     ;;
#		'v') version
#		     exit 0	
#		     ;;
#		'h') usage
#		     exit 0	
#		     ;;
#	esac
#done
TEMP=`getopt -o uvh --long uninstall,version,help,basedir: \
     -n 'glite-security-utils_installer.sh' -- "$@"`

if [ $? != 0 ] ; then echo "Terminating..." ; exit 1 ; fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

if [ `id -u` != 0 ]; then
	export basedir=`pwd`/glite
fi

while true ; do
        case "$1" in
                -u|--uninstall) uninstall
                				exit 0
                				;;
                -v|--version)	version
                				exit 0
                				;;
                -h|--help)		usage
                				exit 0
                				;;
                --basedir) 		export basedir=$2
                				if [ "${basedir:0:1}" != "/" ]; then
                					echo ERROR: basedir must be an absolute path
                					exit 1
                				fi
                				echo Setting base directory to $basedir
                				shift 2; break ;;
#                *) echo "Internal error!" ; exit 1 ;;
				*) break ;;
        esac
done

install

exit 0
	

