#!/bin/sh
#added by installer
Installpath=/data/Exceed_Connection_Server_13.7

# ####################################################################
# Exceed Connection Server 13.7
# Copyright (c) 1997-2009 Open Text Corporation. All Rights Reserved.
# ####################################################################
# setperms
# $Revision: 15671 $
# ####################################################################

initStrings()
{
  VERSION="13.7"
  SRV_NAME="Exceed Connection Server"
  PROD_NAME="${SRV_NAME} ${VERSION}"

  # messages 
  SP_Msg_Pre="\n${SRV_NAME} can be configured so that all members of a given \
\nUNIX group can start and stop the server and perform administrative tasks \
\nat the command line.\n"

SP_Msg_EnterGroup="User group that can administer the 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 ${PROD_NAME} installation.  Please run this script \
\nfrom the bin dir of the installation or provide the fully specified path to \
\nthe install location on the command line."

  SP_Msg_Working="Setting file permissions..\c"

  SP_Msg_Done="Finished."

  SP_Msg_Banner="setperms - ${PROD_NAME}"
}

set_export_echo()
{
  # 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
}

display_banner()
{
  BannerBar="=================================================="
  
# $ECHO
# $ECHO "${BannerBar}"
  $ECHO
  $ECHO "${indent}${SP_Msg_Banner}"
  $ECHO
# $ECHO "${BannerBar}"
}

setup_files_directories()
{             
  changegroupRfiles="${InstallDir}/conf"
  changepermRgrwx="${InstallDir}/conf"
  changepermgrwx="${InstallDir}/sessions"

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

  changepermgrx="${InstallDir} \
                  ${InstallDir}/bin/ecsinfo \
                  ${InstallDir}/bin/ecsstart \
                  ${InstallDir}/bin/ecsstop \
                  ${InstallDir}/bin/ecswall \
                  ${InstallDir}/bin/getversions \
                  ${InstallDir}/bin/keymanager \
                  ${InstallDir}/bin/sys/authenticator \
                  ${InstallDir}/bin/sys/cli \
                  ${InstallDir}/bin/sys/ewebhost \
                  ${InstallDir}/bin/sys/msg.txt \
                  ${InstallDir}/bin/sys/sessclean \
                "

  changegroupfiles="${changepermgrwx} ${changepermsuid} ${changepermsuid} ${changepermgrx}"
}

get_group_name()
{
  testfile="test.file"

  # 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
}

change_group()
{
  # 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_permissions()
{
  # 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
}

# figure out the ECS installation dir
InstallDir=${Installpath}

initStrings
set_export_echo
setup_files_directories

display_banner
get_group_name

${ECHO} "${SP_Msg_Working}"

change_group
change_permissions

${ECHO} "${SP_Msg_Done}"

