#!/bin/ksh

####################################################################################
# Defines.
####################################################################################

AWK=/usr/bin/awk
CAT=/usr/bin/cat
CHMOD=/usr/bin/chmod
CP=/usr/bin/cp
ECHO=/usr/bin/echo
GREP=/usr/bin/grep
ID=/usr/bin/id
INSTALLF=/usr/sbin/installf
LN=/usr/bin/ln
MKDIR=/usr/bin/mkdir
RM=/usr/bin/rm
SED=/usr/bin/sed
UNIQ=/usr/bin/uniq

OSTYPE=`/bin/uname -s`
if [ "$OSTYPE" = "Linux" ]; then
  CAT=/bin/cat
  CHMOD=/bin/chmod
  CP=/bin/cp
  GREP=/bin/grep
  LN=/bin/ln
  MKDIR=/bin/mkdir
  RM=/bin/rm
  SED=/bin/sed
fi

BELL_CHAR='\a'

PS_STATE_FILE="/etc/opt/SUNWps/PSConfig.properties"

WEBNFS_PKGINFO_FILE="/var/sadm/pkg/SUNWebnfs/pkginfo"
JCIFS_PKGINFO_FILE="/var/sadm/pkg/SUNWjcifs/pkginfo"


####################################################################################
# Get configuration from file
####################################################################################

GrabConfig() {

  GRABCONFIG_FILE=$1
  GRABCONFIG_KEY=$2
  GRABCONFIG_SEPARATOR=$3

  ANSWER=`$GREP "^$GRABCONFIG_KEY$GRABCONFIG_SEPARATOR" $GRABCONFIG_FILE | $UNIQ | $SED -e "s/$GRABCONFIG_KEY$GRABCONFIG_SEPARATOR//"`

}

####################################################################################
# Make sure that the user is root.
####################################################################################

CheckUser() {

    if [ `$ID | $AWK '{print $1}'` != "uid=0(root)" ]; then
      $ECHO "You must be root user. $BELL_CHAR"
      exit 1
    fi

}

####################################################################################
# Make sure required state files are present.
####################################################################################

CheckStateFiles() {

    if [ ! -f $PS_STATE_FILE ]; then
      $ECHO "Error: $PS_STATE_FILE does not exist. $BELL_CHAR"
      exit 1
    fi

}

####################################################################################
# Initialize all global variables.
####################################################################################

Initialize() {

    # Portal server base directory.
    GrabConfig $PS_STATE_FILE "BASEDIR" "="
    if [ "$ANSWER" != "" ]; then
      PS_BASEDIR=$ANSWER
    else
      $ECHO "Error: Cannot determine BASEDIR. $BELL_CHAR"
      exit 1
    fi
    
    # JDK directory.
    GrabConfig $PS_STATE_FILE "DEPLOY_JDK_DIR" "="
    if [ "$ANSWER" != "" ]; then
      DEPLOY_JDK_DIR=$ANSWER
    else
      GrabConfig $PS_STATE_FILE "JDK_DIR" "="
      DEPLOY_JDK_DIR=$ANSWER
    fi
    
    # Deploy type.
    GrabConfig $PS_STATE_FILE "DEPLOY_TYPE" "="
    if [ "$ANSWER" != "" ]; then
      DEPLOY_TYPE=$ANSWER
    else
      $ECHO "Error: Cannot determine DEPLOY_TYPE. $BELL_CHAR"
      exit 1
    fi
    
    if [ "$DEPLOY_TYPE" = "SUNONE" ]; then

      # Deploy domain
      GrabConfig $PS_STATE_FILE "DEPLOY_DOMAIN" "="
      if [ "$ANSWER" != "" ]; then
        DEPLOY_DOMAIN=$ANSWER
      else
        $ECHO "Error: Cannot determine DEPLOY_DOMAIN. $BELL_CHAR"
        exit 1
      fi
      
      # Deploy instance 
      GrabConfig $PS_STATE_FILE "DEPLOY_INSTANCE" "="
      if [ "$ANSWER" != "" ]; then
        DEPLOY_INSTANCE=$ANSWER
      else
        $ECHO "Error: Cannot determine DEPLOY_INSTANCE. $BELL_CHAR"
        exit 1
      fi

    fi

    # Deploy directory.
    GrabConfig $PS_STATE_FILE "DEPLOY_DIR" "="
    if [ "$ANSWER" != "" ]; then
      DEPLOY_DIR=$ANSWER
    else
      $ECHO "Error: Cannot determine DEPLOY_DIR. $BELL_CHAR"
      exit 1
    fi
    
    # Identity server base directory.
    GrabConfig $PS_STATE_FILE "IDSAME_BASEDIR" "="
    if [ "$ANSWER" != "" ]; then
      IDSAME_BASEDIR=$ANSWER
    else
      $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
      exit 1
    fi
    
    # Others.
    FILE="/etc/opt/SUNWam/config/AMConfig.properties"
    ADMIN_DN=`$GREP "^com.sun.identity.authentication.super.user=" $FILE | $SED -e "s/com.sun.identity.authentication.super.user=//"`
    DPADMIN="$PS_BASEDIR/SUNWps/bin/dpadmin"

}

####################################################################################
# Add netlet provider to display profile.
####################################################################################

AddNetletProviderToDP() {

    $ECHO "Adding Netlet provider to display profile..."
    $DPADMIN add -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -g $PS_BASEDIR/SUNWps/samples/netlet/netletProvider.xml

}

####################################################################################
# Copy netletevalpac.jar to JDK directory.
####################################################################################

CopyNetletEvalPactoJDKDir() {

    $ECHO "Copying netletevalpac.jar to JDK directory..."
    $CP $PS_BASEDIR/SUNWps/web-src/WEB-INF/lib/netletevalpac.jar $DEPLOY_JDK_DIR/jre/lib/ext
    if [ "$OSTYPE" = "SunOS" ]; then
      $INSTALLF -f "SUNWpsnl"
    fi
}

####################################################################################
# Link webnfs.jar to portal web-src.
####################################################################################

AddWebNFSToPortal() {

    $ECHO "Creating softlink for WebNFS..."
    if [ -f $WEBNFS_PKGINFO_FILE ]; then
      GrabConfig $WEBNFS_PKGINFO_FILE "BASEDIR" "="
      $LN -s $ANSWER/SUNWebnfs/webnfs.zip $PS_BASEDIR/SUNWps/web-src/WEB-INF/lib/webnfs.jar
      if [ "$OSTYPE" = "SunOS" ]; then
        $INSTALLF "SUNWpsnf" $PS_BASEDIR/SUNWps/web-src/WEB-INF/lib/webnfs.jar
      fi
    fi
    if [ "$OSTYPE" = "SunOS" ]; then
      $INSTALLF -f "SUNWpsnf"
    fi
}

####################################################################################
# Link jcifs.jar to portal web-src.
####################################################################################

AddJCIFSToPortal() {

    $ECHO "Creating softlink for JCIFS..."
    if [ -f $JCIFS_PKGINFO_FILE ]; then
      GrabConfig $JCIFS_PKGINFO_FILE "BASEDIR" "="
      $LN -s $ANSWER/jcifs/lib/jcifs-0.7.3.jar $PS_BASEDIR/SUNWps/web-src/WEB-INF/lib/jcifs-0.7.3.jar
      if [ "$OSTYPE" = "SunOS" ]; then
              $INSTALLF "SUNWpsnf" $PS_BASEDIR/SUNWps/web-src/WEB-INF/lib/jcifs-0.7.3.jar
      fi
    fi
    if [ "$OSTYPE" = "SunOS" ]; then
      $INSTALLF -f "SUNWpsnf"
    fi
}

####################################################################################
# Put .so files in LD_LIBRRAY_PATH for netfile.
####################################################################################

CopyLibraryFilesForNetfile() {

    $ECHO "Placing Netfile .so file in library path..."
    PS_LIB_DIR=$PS_BASEDIR/SUNWps/lib
    if [ "$DEPLOY_TYPE" = "IWS" ]; then
      $CP $PS_LIB_DIR/libgetpwnam.so $DEPLOY_DIR/bin/https/lib
      $CP $PS_LIB_DIR/libgetpwnam2.so $DEPLOY_DIR/bin/https/lib
    fi

}

####################################################################################
# Add SocketPermission & NetPermission to server.policy of S1AS needed for NetFile 
####################################################################################

AddNetFilePermissionsToServerPolicy() {

    if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
      FILE="$DEPLOY_DOMAIN/$DEPLOY_INSTANCE/config/server.policy"
      $ECHO "Adding required netfile permissions to $FILE ..."
      $CAT >> $FILE << EOF

// S1PS - SRA NetFile
grant {
      permission java.net.SocketPermission    "*", "connect,accept,listen,resolve";
      permission java.net.NetPermission "*", "specifyStreamHandler";
};
// END OF S1PS - SRA NetFile

EOF
    fi

}

####################################################################################
# Add proxylet provider to display profile.
####################################################################################

AddProxyletProviderToDP() {

    $ECHO "Adding Proxylet provider to display profile..."
    $DPADMIN add -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -g $PS_BASEDIR/SUNWps/samples/proxylet/proxyletProvider.xml

}

####################################################################################
# Main
####################################################################################

# Make sure that the user is root.
CheckUser

# Make sure that the required state files are present.
CheckStateFiles

# Initialize all global variables.
Initialize

# Add netlet provider to display profile.
AddNetletProviderToDP

# Copy netletevalpac.jar to JDK directory.
CopyNetletEvalPactoJDKDir

# Link webnfs.jar to portal web-src.
AddWebNFSToPortal

# Link jcifs.jar to portal web-src.
AddJCIFSToPortal

# Put .so files in LD_LIBRRAY_PATH for netfile.
CopyLibraryFilesForNetfile

# Add SocketPermission & NetPermission to server.policy of S1AS needed for NetFile 
AddNetFilePermissionsToServerPolicy

# Add proxylet provider to display profile.
AddProxyletProviderToDP

exit 0
