#!/usr/bin/qsh

if [ $# -lt 2 ]; then
  echo "Usage: _fixprofileauthorities PROFILEPATH updateType [-setown]"
  echo "  where "
  echo "    PROFILEPATH is the directory to the profile root"
  echo "      E.g. /QIBM/UserData/WebSphere/AppServer/V7/Base/profiles/default"
  echo "    updateType is one of PRFCRT1 or PRFCRT2 or ALL"
  echo "    -setown is optional parameter indicating to set QEJBSVR as the owner"
  echo "      of the profile directory structure and contents. Use this option"
  echo "      when you have down a restore of the profile from a zip file"
  echo "      Example: _fixprofileauthorities profile_root ALL -setown"
  exit 1
fi

PROFILEPATH=$1
UPDATETYPE=$2
SETOWN=

if [ ! -e ${PROFILEPATH} ]; then
  echo "Error: Profile root directory, ${PROFILEPATH}, does not exist"
  exit 1
fi

if [ "${UPDATETYPE}" = "PRFCRT1" ] || [ "${UPDATETYPE}" = "ALL" ]; then
  echo "Update authorities, phase 1"
  if [ -e ${PROFILEPATH}/config ]; then
    chmod o=  ${PROFILEPATH}/config
  fi
  if [ -e ${PROFILEPATH}/etc ]; then
    chmod o=  ${PROFILEPATH}/etc
  fi
  if [ -e ${PROFILEPATH}/installableApps ]; then
    chmod o=  ${PROFILEPATH}/installableApps
  fi
  if [ -e ${PROFILEPATH}/installedApps ]; then
    chmod o=  ${PROFILEPATH}/installedApps
  fi
  if [ -e ${PROFILEPATH}/installedConnectors ]; then
    chmod o=  ${PROFILEPATH}/installedConnectors
  fi
  if [ -e ${PROFILEPATH}/temp ]; then
    chmod o=  ${PROFILEPATH}/temp
  fi
  if [ -e ${PROFILEPATH}/wstemp ]; then
    chmod o=  ${PROFILEPATH}/wstemp
  fi

  system "CHGAUT OBJ('${PROFILEPATH}/bin/*') USER(QEJBSVR) DTAAUT(*RWX)"

  if [ -e /QSYS.LIB/QTMHHTTP.USRPRF ]; then
    if [ -e ${PROFILEPATH}/config ]; then
      system "QSYS/CHGAUT OBJ('${PROFILEPATH}/config') USER(QTMHHTTP) DTAAUT(*X)"
    fi
    if [ -e ${PROFILEPATH}/config/cells ]; then
      system "QSYS/CHGAUT OBJ('${PROFILEPATH}/config/cells') USER(QTMHHTTP) DTAAUT(*X)"
    fi
    system "QSYS/CHGAUT OBJ('${PROFILEPATH}/logs') USER(QTMHHTTP) DTAAUT(*RWX)"
    system "QSYS/CHGAUT OBJ('${PROFILEPATH}/etc') USER(QTMHHTTP) DTAAUT(*RX)"
    if [ -e ${PROFILEPATH}/etc/plugin-key.kdb ]; then
      system "QSYS/CHGAUT OBJ('${PROFILEPATH}/etc/plugin-key.kdb') USER(QTMHHTTP) DTAAUT(*RX)"
      system "QSYS/CHGAUT OBJ('${PROFILEPATH}/etc/plugin-key.sth') USER(QTMHHTTP) DTAAUT(*RX)"
    fi
  fi

  if [ -e /QSYS.LIB/QNOTES.USRPRF ]; then
    if [ -e ${PROFILEPATH}/config ]; then
      system "QSYS/CHGAUT OBJ('${PROFILEPATH}/config') USER(QNOTES) DTAAUT(*X)"
    fi
    if [ -e ${PROFILEPATH}/config/cells ]; then
      system "QSYS/CHGAUT OBJ('${PROFILEPATH}/config/cells') USER(QNOTES) DTAAUT(*X)"
    fi
    system "QSYS/CHGAUT OBJ('${PROFILEPATH}/logs') USER(QNOTES) DTAAUT(*RWX)"
    system "QSYS/CHGAUT OBJ('${PROFILEPATH}/etc') USER(QNOTES) DTAAUT(*RX)"
    if [ -e ${PROFILEPATH}/etc/plugin-key.kdb ]; then
      system "QSYS/CHGAUT OBJ('${PROFILEPATH}/etc/plugin-key.kdb') USER(QNOTES) DTAAUT(*RX)"
      system "QSYS/CHGAUT OBJ('${PROFILEPATH}/etc/plugin-key.sth') USER(QNOTES) DTAAUT(*RX)"
    fi
  fi

  chmod o=rx ${PROFILEPATH}
  chmod -R o=rx ${PROFILEPATH}/bin
  chmod o=rx ${PROFILEPATH}/logs
  chmod o=rx ${PROFILEPATH}/properties
  if [ -e ${PROFILEPATH}/properties/java.security ]; then
    chmod o=rx ${PROFILEPATH}/properties/java.security
  fi
  chmod o=rx ${PROFILEPATH}/properties/.instance.properties

  if [ -e ${PROFILEPATH}/properties/sas.client.props ]; then
    chmod o=   ${PROFILEPATH}/properties/sas.client.props
  fi
  if [ -e ${PROFILEPATH}/properties/service ]; then
    chmod o=   ${PROFILEPATH}/properties/service
  fi
  if [ -e ${PROFILEPATH}/properties/soap.client.props ]; then
    chmod o=   ${PROFILEPATH}/properties/soap.client.props
  fi
  if [ -e ${PROFILEPATH}/properties/sas.stdclient.properties ]; then
    chmod o=   ${PROFILEPATH}/properties/sas.stdclient.properties
  fi
  if [ -e ${PROFILEPATH}/properties/sas.tools.properties ]; then
    chmod o=   ${PROFILEPATH}/properties/sas.tools.properties
  fi
  if [ -e ${PROFILEPATH}/properties/sib.client.ssl.properties ]; then
    chmod o=   ${PROFILEPATH}/properties/sib.client.ssl.properties
  fi
  if [ -e ${PROFILEPATH}/properties/wsserver.key ]; then
    chmod o=   ${PROFILEPATH}/properties/wsserver.key
  fi
fi

if [ "${UPDATETYPE}" = "PRFCRT2" ] || [ "${UPDATETYPE}" = "ALL" ]; then
  echo "Update authorities, phase 2"
  chmod o=  ${PROFILEPATH}/config
  chmod o=  ${PROFILEPATH}/properties/wsadmin.properties

  if [ -e ${PROFILEPATH}/samples ]; then
    chmod o=  ${PROFILEPATH}/samples
  fi
  if [ -e ${PROFILEPATH}/servers ]; then
    chmod o=   ${PROFILEPATH}/servers
  fi
  if [ -e ${PROFILEPATH}/properties/ssl.client.props ]; then
    chmod o=   ${PROFILEPATH}/properties/ssl.client.props
  fi
  if [ -e ${PROFILEPATH}/properties/profileKey.metadata ]; then
    chmod o=   ${PROFILEPATH}/properties/profileKey.metadata
  fi
fi

if [ $# -eq 3 ]; then
  SETOWN=$3
  if [ "${SETOWN}" = "-setown" ]; then
    chown -R QEJBSVR ${PROFILEPATH}
  fi
fi
