#!/bin/sh
#
# (C) 1994 Free Software Assiociation of Germany
#
#  Install GREAT for an defined user
#
#
###########################################################

# set -vx


show_welcome()
{
	clear
	echo "
             User Installation Script for the G.R.E.A.T Enviroment
             =====================================================

  This Script will create a GREAT home for the user $USER,
  and copy all the neccessary files for cmdframe, glex etc. to
  the home/.great directory.

  Type [RETURN] to continue.

  Type CTRL-C for abort.
"
	read a

	clear
}

set_great_env()
{
	echo -n "Set GREAT's Environment : "
	sleep 1
	. /usr/GREAT/bin/greatenv
	echo "Ok"
}

find_user_home()
{
	echo -n "Locate home from $USER : "
	sleep 1

	PWD_ENTRY=`cat /etc/passwd | grep $USER`
	if [ -z "$PWD_ENTRY" ]
	then
		echo "User $USER not found "
		exit 1
	fi

	U_HOME=`echo $PWD_ENTRY | cut -f6 -d:`
	GROUP=`echo $PWD_ENTRY | cut -f4 -d:`

	if [ ! -d "$U_HOME" ]
	then
		echo "Can't find home = [$U_HOME] from user $USER"
		exit 1
	fi	

	echo "Ok"
}

create_great_users_dir()
{
	echo -n "Create GREAT's home $USER_HOME for user $USER : "
	sleep 1
	err_cd=`mkdir $USER_HOME 2>&1`
	error=$?	
	if [ $error -ne 0 ]
	then
		echo "Can't create $USER_HOME"
		echo "Error = [$err_cd]"
		exit 1
	fi
	echo "Ok"
}

copy_great_conf_files()
{
	echo -n "Copy GREAT's rc's to $USER_HOME : "
	sleep 1
	cp $GREAT_MASTER/config/system.cmdf_games $USER_HOME/cmdf_games
	cp $GREAT_MASTER/config/system.cmdf_graphics $USER_HOME/cmdf_graphics
	cp $GREAT_MASTER/config/system.cmdf_net $USER_HOME/cmdf_net
	cp $GREAT_MASTER/config/system.cmdframerc $USER_HOME/cmdframerc
	cp $GREAT_MASTER/config/system.gflexrc $USER_HOME/.gflexrc
	cp $GREAT_MASTER/config/Greatdefaults $USER_HOME
	echo "Ok"
}


change_owner()
{
	echo -n "Change the owner to $USER:$GROUP : "
	sleep 1
	chown -R $USER:$GROUP $USER_HOME
	if [ $? -ne 0 ]
	then
		echo "Can't change the owner $USER:$GROUP"
		exit 1
	fi
	echo "Ok"
}


if [ -z $1 ]
then
	echo "$0: Usage $0 <user>"
	exit 1
fi

USER=$1
G_HOME=.great

show_welcome
set_great_env
find_user_home

USER_HOME=$U_HOME/$G_HOME

create_great_users_dir

copy_great_conf_files
change_owner

echo "
The GREAT installation for user $USER is now complete.
Put the line :

. /usr/GREAT/bin/greatenv 

to your system profile in /etc."

