#!/bin/sh

# ###############################################################
# Exceed Connection Server 2008
# Copyright (c) 1997-2008 Hummingbird Ltd. All Rights Reserved.
# ###############################################################
# setperms
# $Revision: 10408 $
# ###############################################################

# messages 
SP_Msg_Pre=\
"\n\
Exceed Connection Server can be configured so that all members of a given \n\
UNIX group can start and stop the server and perform administrative tasks \n\
at the command line.  \
\n"
SP_Msg_EnterGroup=\
"User group that can administer Exceed Connection Server [Enter to skip]:\c"

SP_Msg_Skipping=\
"Skipping."

SP_Msg_NoGroup=\
"Cannot change file group.  Please ensure this group exists.\n\n"

SP_Msg_NoInstall=\
"Cannot find Exceed Connection Server installation.  Please run this script \n\
from the bin dir of the installation or provide the fully specified path to \n\
the install location on the command line."

SP_Msg_Working="Setting file permissions..\c"

SP_Msg_Done="Finished."

SP_Msg_Banner="setperms - Exceed Connection Server 2008"



# get the correct echo command line for this OS
if [ ${OSTYPE:="UNKNOWN"} = "Linux" \
    -o $OSTYPE = "linux-gnu" \
    -o $OSTYPE = "linux" \
    ]; then
        ECHO="echo -e"
else
        ECHO="echo"
fi
export ECHO

$ECHO
$ECHO ${SP_Msg_Banner}
$ECHO

# figure out the EoD installation dir
InstallDir=${1}

if [ "x${InstallDir}" = "x" ] ; then
    if [ -x "ewebhost" -a -x "keymgr" ] ; then
	# we are in bin/sys
        cd ../..
    elif [ -d "sys" -a -x "elpr" ] ; then
	# we are in bin
        cd ..
    elif [ "(" ! -d "bin" ")"  -o "(" ! -d "conf" ")" ] ; then
	# we are not in the root install dir
        ${ECHO} "${SP_Msg_NoInstall}"
        exit 1
    fi
    InstallDir=`pwd`
fi

# files
testfile="test.file"
changegroupfiles="${InstallDir} \
                ${InstallDir}/bin/sys/esessionmgr \
                ${InstallDir}/bin/ecsstart \
                ${InstallDir}/bin/ecsstop \
                ${InstallDir}/bin/openssl \
                ${InstallDir}/bin/openssl.cnf \
                ${InstallDir}/bin/keymanager \
                ${InstallDir}/bin/ecsinfo \
                ${InstallDir}/bin/sys/sessclean \
                ${InstallDir}/bin/sys/keymgr \
                ${InstallDir}/bin/sys/info \
                ${InstallDir}/bin/sys/msg.txt \
                ${InstallDir}/bin/sys/ewebhost \
                ${InstallDir}/bin/sys/authenticator \
                ${InstallDir}/sessions"

changegroupRfiles="${InstallDir}/conf"

changepermsuid="${InstallDir}/bin/sys/esessionmgr \
                ${InstallDir}/bin/sys/keymgr"

changepermgrx="${InstallDir} \
                ${InstallDir}/bin/ecsstart \
                ${InstallDir}/bin/ecsstop \
                ${InstallDir}/bin/keymanager \
                ${InstallDir}/bin/openssl \
                ${InstallDir}/bin/openssl.cnf \
                ${InstallDir}/bin/ecsinfo \
                ${InstallDir}/bin/sys/sessclean \
                ${InstallDir}/bin/sys/libssl.* \
                ${InstallDir}/bin/sys/libcrypto.* \
                ${InstallDir}/bin/sys/msg.txt \
                ${InstallDir}/bin/sys/ewebhost \
                ${InstallDir}/bin/sys/authenticator \
                ${InstallDir}/bin/sys/info"

changepermgrwx="${InstallDir}/sessions"

changepermRgrwx="${InstallDir}/conf"

# get name of group to use from user
tflag="0"
${ECHO} "$SP_Msg_Pre"
while [ "${tflag}" = "0" ]
do
    ${ECHO} "${SP_Msg_EnterGroup}"
    read group
    ${ECHO}
    if [ "x${group}" = "x" ] ; then
        ${ECHO} "${SP_Msg_Skipping}"
        exit 0
    fi
    
    touch ${testfile}
    if ( chgrp ${group} ${testfile} ) ; then
        tflag="1"
    else
        ${ECHO} "${SP_Msg_NoGroup}"
    fi
    rm ${testfile}
done

${ECHO} "${SP_Msg_Working}"
# change group
for changefile in ${changegroupfiles} ; do
    chgrp "${group}" "${changefile}"
    ${ECHO} ".\c"
done

for changefile in ${changegroupRfiles} ; do
    chgrp -R "${group}" "${changefile}"
    ${ECHO} ".\c"
done

# change perms 
for changefile in ${changepermsuid} ; do
    chmod 6750 "${changefile}"
    ${ECHO} ".\c"
done

for changefile in ${changepermgrwx} ; do
    chmod g+rwx "${changefile}"
    ${ECHO} ".\c"
done

for changefile in ${changepermgrx} ; do
    chmod g+rx "${changefile}"
    ${ECHO} ".\c"
done

for changefile in "${changepermRgrwx}" ; do
    chmod -R g+rwx "${changefile}"
    ${ECHO} ".\c"
done

${ECHO} "${SP_Msg_Done}"

