#!/bin/csh
#
#--------------------------------------------------------
# File: 			CB_install (ftp)      
# Version:		2.0
# Creation:    		2. Dezember 1992 (Rene Soiron, RWTH)
# Last Change: 	26. April 1994 (Rene Soiron, RWTH)
#--------------------------------------------------------
#
echo " "
echo "This is the ConceptBase 3.3 Installation Script"
echo "Version 2.0"
echo " "


set prolog = "no"
set ftp = "yes"
set CurrDir = `pwd`

#
# Test if gunzip is available
#
test -f `which gunzip` >>& /dev/null
set stat = $status
if ($stat != 0) then
	echo The script did not find the gunzip programm.
	echo Please enter the path where to find it 
	set GUNZIP = ($<)
	test -x $GUNZIP
	set stat = $status
	if ($stat != 0) then
		echo ERROR
		echo $GUNZIP not executable
		exit(-1)
	endif
else
	set GUNZIP = `which gunzip`
endif

@ anz_versuche = 3			
set datei = $CurrDir/cshrc.new	
					
set ftp_file = "cbase33.tar.gz"
set CBFile = "cbase33.tar"


test -f $ftp_file
set stat = $status
if ($stat != 0) then
	echo "In your directory exists no file named $ftp_file."
	echo "You can obtain it via ftp from the ftp-server of the"
	echo "RWTH Aachen."
	echo "ftp-server: ftp.informatik.rwth-aachen.de"
	echo "directory: pub/CB/CB_3.3 "
	exit (1)	
endif



set ok = "no"
@ anzahl = 1
echo " "
echo "Now you will be asked at most $anz_versuche times to enter "
echo "a valid, not empty directory name."
while (($ok == "no") && ($anzahl <= $anz_versuche))
	set ok = "yes"
	echo " "
	echo "Current directory : `pwd`"
	echo "Please enter the (relative or absolute) name of the"
	echo "Concept Base installation directory"

	set CBDir = ($<)
	if ($CBDir != "") then	
		test -d $CBDir
		set stat = $status
	else 
		set stat = -1
		set ok = "no"
	endif

	if (($stat != 0) && ($stat != -1)) then
		echo "WARNING: The directory $CBDir does not exist."
		echo -n "Should it be created ? (Y/N) "
		set answer = ($<)
		if (($answer == Y) || ($answer == y)) then
			mkdir $CBDir
		else
			set ok = no
		endif
	endif

	if ($ok != no) then
		test -s $CBDir/CB_Exe/KSV_Exe/runCBserver
		set stat = $status
		if ($stat  == 0) then
			echo "You should not overwrite the Version 3.1"
			echo "Please enter another installation directory"
			set ok = no
		endif
	endif
	@ anzahl = $anzahl + 1	
end

if (($anzahl > $anz_versuche) && ($ok == "no")) then
	echo "Installation of Concept-Base terminated"
	exit (1)
endif

pushd $CBDir >> /dev/null
set CBDir = `pwd`
popd >> /dev/null


/bin/cp $ftp_file $ftp_file.save
/bin/mv $ftp_file  $CBDir >>& /dev/null

cd $CBDir

echo "Uncompressing Concept Base..."
$GUNZIP $CBDir/$ftp_file
mv $CurrDir/$ftp_file.save $CurrDir/$ftp_file
echo "Installing ConceptBase..."
tar -xvf $CBDir/$CBFile
set stat = $status
if ($stat != 0) then
	echo "Something went wrong with the extraction of ConceptBase"
	echo "Installation of Concept Base failed."
	exit (1)
else 
	/bin/rm $CBDir/$CBFile 
endif


cd $CBDir
chmod -R 770 CB_Exe 
cd $CBDir/CB_Exe/KSV_Exe

echo ""
echo "IMPORTANT NOTICES"
echo "==================="
echo ""


 
cd $CurrDir
echo "setenv CB_HOME $CBDir" > $datei
if (`setenv|grep "CB_HOME"` != "")  then	
	echo -n "WARNING: In your Environment already exists"
	echo  " a Variable named CB_HOME."
	echo "The contents MUST be changed to $CBDir"
endif
source $datei

cd  $CBDir
chgrp -R cbase CB_Exe
set stat = $status
if ($stat != 0) then
	echo "If you don't want to install a group cbase then only the owner of the "
	echo "directory $CBDir can use ConceptBase"
endif

echo " "
echo "Before ConceptBase can be started, you need to decode it."
echo "Further Information can be found in the Installation Guide"
echo " "	


echo "The file $CurrDir/$ftp_file "
echo "is no longer needed."
echo "You can delete it or keep it as compressed copy of the "
echo "ConceptBase System."
echo ""

echo "This script created the file $datei."
echo "It contains the correct settings of the Environment-Variables"
echo "needed by ConceptBase."
echo "The contents of it should be copied into the ~/.cshrc file of" 
echo "every ConceptBase user."
echo ""
echo "Done."
echo " "

cd $CurrDir


exit (0)
