#!/bin/sh
# Windows Logon Module deploy
# ---------------------------------------------- #


##### Main routine ###############################

# Function read
. ${INST_HOME}/function.sh

##### TRAP #######################################
# Deny Ctrl+C
trap 'echo "Deny Ctrl+C"' 1 2 3 15

# init 
initialize

# variable init
FLAG=0
WINXP_BACK_ERR=0
WINXP_ERR_UNKNOWN=0
WINVISTA_ERR_UNKNOWN=0

# Get IP info
load_setup_properties

# START
echo_sp "${SMX_WINLOGON_SET}."
write_log "${SMX_WINLOGON_SETSTART}"

#----- WindowsLogon module backup ----#
if [ ! -e ${BACKUP_DIR} ] ; then
  $mkdir ${BACKUP_DIR}
fi

# Backup downLoad zip and base files
####### XP #######
# check for XP backup error
$tar $ZC_OPT ${BACKUP_WIN} ${WIN_XP_FILE_PATH} ${WIN_DOWNLOAD_PATH}/${WIN_XP_DOWNLOAD_MODULE} ${SMX_INST}/${AUTH_DEFINE_FILE} ${SMX_INST}/${CONF_SH_FILENAME} ${ADMIN_PROP_DIR}/${ADMIN_PROP_WIN_SET_EN} ${ADMIN_PROP_DIR}/${ADMIN_PROP_WIN_SET_JA} ${ADMIN_PROP_DIR}/${ADMIN_PROP_WIN_SET_SJIS} 1> /dev/null 2>> ${ERR_LOG_FILE}
if [ $? -ne 0 ] ; then
  # if a backup error occurred
  # set error flag
  WINXP_BACK_ERR=1
  if [ -e ${WIN_DOWNLOAD_PATH}/${WIN_XP_DOWNLOAD_MODULE} -a ${WIN_SW} = "y" ] ; then
    # if file exists and win logon func is enabled
    # cause of backup error unknown
    WINXP_ERR_UNKNOWN=1
  fi
else
  # if no error occurred
  # temporarily rename XP tar file
  $mv ${BACKUP_WIN} ${BACKUP_WIN}_TEMP 1> /dev/null 2>> ${ERR_LOG_FILE}
fi
####### VISTA #######
if [ "${WINXP_BACK_ERR}" -eq 0 ] ; then
  # update tar 
  # tar.gz cannot be updated, so create a new one (XP and VISTA)
  $tar $ZC_OPT ${BACKUP_WIN} ${WIN_XP_FILE_PATH} ${WIN_VISTA_FILE_PATH} ${WIN_DOWNLOAD_PATH}/${WIN_XP_DOWNLOAD_MODULE} ${WIN_DOWNLOAD_PATH}/${WIN_VISTA_DOWNLOAD_MODULE} ${SMX_INST}/${AUTH_DEFINE_FILE} ${SMX_INST}/${CONF_SH_FILENAME} ${ADMIN_PROP_DIR}/${ADMIN_PROP_WIN_SET_EN} ${ADMIN_PROP_DIR}/${ADMIN_PROP_WIN_SET_JA} ${ADMIN_PROP_DIR}/${ADMIN_PROP_WIN_SET_SJIS} 1> /dev/null 2>> ${ERR_LOG_FILE}
  # If an error occurred use the XP only backup file
  if [ $? -ne 0 ] ; then
    # rename XP tar file to original file name
    $mv ${BACKUP_WIN}_TEMP ${BACKUP_WIN} 1> /dev/null 2>> ${ERR_LOG_FILE}
  else
    # if backup was successful delete temporary file
    $rm -f ${BACKUP_WIN}_TEMP 1> /dev/null 2>> ${ERR_LOG_FILE}
  fi
elif [ "${WINXP_BACK_ERR}" -ne 0 ] ; then
  # new tar (Vista only)
  $tar $ZC_OPT ${BACKUP_WIN} ${WIN_VISTA_FILE_PATH} ${WIN_DOWNLOAD_PATH}/${WIN_VISTA_DOWNLOAD_MODULE} ${SMX_INST}/${AUTH_DEFINE_FILE} ${SMX_INST}/${CONF_SH_FILENAME} ${ADMIN_PROP_DIR}/${ADMIN_PROP_WIN_SET_EN} ${ADMIN_PROP_DIR}/${ADMIN_PROP_WIN_SET_JA} ${ADMIN_PROP_DIR}/${ADMIN_PROP_WIN_SET_SJIS} 1> /dev/null 2>> ${ERR_LOG_FILE}
  if [ $? -ne 0 ] ; then
    # if a backup error occurred
    if [ -e ${WIN_DOWNLOAD_PATH}/${WIN_VISTA_DOWNLOAD_MODULE} -a $WIN_SW = "y" ] ; then
      # if file exists and win logon func is enabled
      # cause of backup error unknown
      WINVISTA_ERR_UNKNOWN=1
    fi
  fi
fi

# check if backup failed (no backup file created)
if [ "${WINXP_ERR_UNKNOWN}" -ne 0 -o "${WINVISTA_ERR_UNKNOWN}" -ne 0 ] ; then
      FLAG=1
      arize_err "dmy" "${WIN_BACKUPMISS}"
fi


# delete old xp and vista modules
if [ $FLAG -eq 0 -a $WIN_SW = "y" ] ; then
  # delete old_msi (XP)
  $rm ${WIN_DOWNLOAD_PATH}/${WIN_XP_DOWNLOAD_MODULE} ${WIN_XP_FILE_PATH}/${WIN_XP_OLDMODULE} 1> /dev/null 2>> ${ERR_LOG_FILE}
  # save result to determine if an exception occurred
  WINXP_DEL=$?
  
  # delete old_msi (VISTA)
  $rm ${WIN_DOWNLOAD_PATH}/${WIN_VISTA_DOWNLOAD_MODULE} ${WIN_VISTA_FILE_PATH}/${WIN_VISTA_OLDMODULE} 1> /dev/null 2>> ${ERR_LOG_FILE}
  # save result to determine if an exception occurred
  WINVISTA_DEL=$?

  if [ ${WINXP_DEL} -ne 0 -o ${WINVISTA_DEL} -ne 0 ] ; then
    FLAG=1
    arize_err "dmy" "${WIN_OLDMODULE_DELETEMISS}"
  fi
fi

# patch module comp
# module from module_xxx.tar.gz
# extract patch module
if [ ! -d $MODULE_DIR ] ; then
  $tar $ZX_OPT $MODULE_PKG_FILE > /dev/null 2>&1
  if [ $? -ne 0 ] ; then
    FLAG=1
    arize_err "dmy" "${INSTMODULE_COMP_MISS}"
  fi
fi

# if no error occurred during the previous processes,
# copy new files to their respective paths
if [ $FLAG -ne 1 ] ; then
  # XP -> {INST_HOME}/module/SECUREMATRIXforWindows_*.*.*.msi 
  #        /home/smxd/installer/package/winauth/XP
  $cp -f ${MODULE_DIR}/${WIN_XP_MODULE} ${WIN_XP_FILE_PATH} 1> /dev/null 2>> ${ERR_LOG_FILE}
  XP_CP=$?
  # XP -> {INST_HOME}/module/SmxGina.dll 
  #       /home/smxd/installer/package/winauth/XP
  $cp -f ${MODULE_DIR}/${WIN_DLL_MODULE} ${WIN_XP_FILE_PATH} 1> /dev/null 2>> ${ERR_LOG_FILE}
  DLL_CP=$?
  # XP -> {INST_HOME}/module/SmxGina.ini.org 
  #       /home/smxd/installer/package/winauth/XP
  $cp -f ${MODULE_DIR}/${WIN_XP_CONF_ORG} ${WIN_XP_FILE_PATH} 1> /dev/null 2>> ${ERR_LOG_FILE}
  INI_CP=$?
  # XP -> {INST_HOME}/module/SmxGinaInstall.ini 
  #       /home/smxd/installer/package/winauth/XP
  $cp -f ${MODULE_DIR}/${WIN_XP_CONF_INST} ${WIN_XP_FILE_PATH} 1> /dev/null 2>> ${ERR_LOG_FILE}
  SGI_CP=$?
  # XP -> {INST_HOME}/module/SmxGinaErrMsgJp.ini 
  #       /home/smxd/installer/package/winauth/XP
  $cp -f ${MODULE_DIR}/${WIN_XP_ERR_MSG} ${WIN_XP_FILE_PATH} 1> /dev/null 2>> ${ERR_LOG_FILE}
  SGE_CP=$?
  # XP -> {INST_HOME}/module/smxtool.exe 
  #       /home/smxd/installer/package/winauth/XP
  $cp -f ${MODULE_DIR}/${WIN_XP_SMX_TOOL} ${WIN_XP_FILE_PATH} 1> /dev/null 2>> ${ERR_LOG_FILE}
  STL_CP=$?
  # VISTA -> {INST_HOME}/module/SECUREMATRIXforWindowsVista_*.*.*.msi
  #          /home/smxd/installer/package/winauth/VISTA
  $cp -f ${MODULE_DIR}/${WIN_VISTA_MODULE} ${WIN_VISTA_FILE_PATH} 1> /dev/null 2>> ${ERR_LOG_FILE}
  VISTA_CP=$?
  # VISTA -> {INST_HOME}/module/config.org
  #          /home/smxd/installer/package/winauth/VISTA
  $cp -f ${MODULE_DIR}/${WIN_VISTA_CONF_ORG} ${WIN_VISTA_FILE_PATH} 1> /dev/null 2>> ${ERR_LOG_FILE}
  XML_CP=$?
  # {INST_HOME}/module/auth_define.sh {SMXD_HOME}/installer
  $cp -f ${MODULE_DIR}/${AUTH_DEFINE_FILE} ${SMX_INST} 1> /dev/null 2>> ${ERR_LOG_FILE}
  DEFINE_CP=$?
  # {INST_HOME}/module/configure.sh {SMXD_HOME}/installer
  $cp -f ${MODULE_DIR}/${CONF_SH_FILENAME} ${SMX_INST} 1> /dev/null 2>> ${ERR_LOG_FILE}
  CONFIG_SH=$?
  # {INST_HOME}/module/windowsAuth_en.properties   /opt/cse/smx/admin/servlet/webapps/admin/WEB-INF/classes/
  $cp -f ${MODULE_DIR}/${ADMIN_PROP_WIN_SET_EN} ${ADMIN_PROP_DIR} 1> /dev/null 2>> ${ERR_LOG_FILE}
  ADM_WIN_PROP_EN=$?
  # {INST_HOME}/module/windowsAuth_en.properties   /opt/cse/smx/admin/servlet/webapps/admin/WEB-INF/classes/
  $cp -f ${MODULE_DIR}/${ADMIN_PROP_WIN_SET_JA} ${ADMIN_PROP_DIR} 1> /dev/null 2>> ${ERR_LOG_FILE}
  ADM_WIN_PROP_JA=$?
  # {INST_HOME}/module/windowsAuth_en.properties   /opt/cse/smx/admin/servlet/webapps/admin/WEB-INF/classes/
  $cp -f ${MODULE_DIR}/${ADMIN_PROP_WIN_SET_SJIS} ${ADMIN_PROP_DIR} 1> /dev/null 2>> ${ERR_LOG_FILE}
  ADM_WIN_PROP_SJIS=$?
  
  if [ ${XP_CP} -ne 0 -o ${DLL_CP} -ne 0 -o ${INI_CP} -ne 0 -o ${VISTA_CP} -ne 0 -o ${XML_CP} -ne 0 -o ${DEFINE_CP} -ne 0 -o ${CONFIG_SH} -ne 0 -o ${ADM_WIN_PROP_EN} -ne 0 -o ${ADM_WIN_PROP_JA} -ne 0 -o ${ADM_WIN_PROP_SJIS} -ne 0 -o ${SGI_CP} -ne 0 -o ${SGE_CP} -ne 0 -o ${STL_CP} -ne 0 ] ; then
    FLAG=1
    arize_err "dmy" "${INSTMODULE_DEPLOY_MISS}"
  fi
fi

#----- Win Logon ini and xml file replace -----#
if [ $FLAG -ne 1 ] ; then
  
  # Windows on or off, when off is finish at once
  if [ "$WIN_SW" = "n" ] ; then
    write_log "${WINLOGON_NOUSE}"
  else
    ####### XP #######
    # IP and Port replace
    RET=0
    # Secondary AUTH exists
    if [ "$AUTH2_SW" = "y" ] ; then
      # get the IP addresses (PRIM & SEC) from setup.props
      # and transfer all files temp to INI_TMP
      $cat ${WIN_XP_FILE_PATH}/${WIN_XP_CONF_ORG} | $sed -e s/'###AUTH1_IP###'/${AUTH1_IP}/g -e s/'###AUTH2_IP###'/${AUTH2_IP}/g -e s/^'Port=0'/'Port=8777'/g > INI_TMP 2>&1
      if [ $? -ne 0 ] ; then
        FLAG=1
      fi
    # Only Primary
    else
      # get the IP addresses (PRIM) from setup.props
      # and transfer all files temp to INI_TMP
      $cat ${WIN_XP_FILE_PATH}/${WIN_XP_CONF_ORG} | $sed -e s/'###AUTH1_IP###'/${AUTH1_IP}/g -e s/'###AUTH2_IP###'/''/g > INI_TMP 2>&1
      if [ $? -ne 0 ] ; then
        FLAG=1
      fi
    fi
    
    if [ ${FLAG} -ne 0 ] ; then
        # if an error occurred
        arize_err "dmy" "${WIN_XP_CONF} ${FILE_REPLACE_MISS}"
        # exit $FLAG
    else
        # if no errors occurred
        # INI FILE rename (move)
        $mv INI_TMP ${WIN_XP_FILE_PATH}/${WIN_XP_CONF} 2>&1
    fi
    
    
    ####### VISTA #######
    # Process only if no errors occurred
    if [ ${FLAG} -eq 0 ] ; then
      # Secondary AUTH exists
      if [ "$AUTH2_SW" = "y" ] ; then
        # get the IP addresses (PRIM & SEC) from setup.props
        # and transfer all files temp to INI_TMP
        $cat ${WIN_VISTA_FILE_PATH}/${WIN_VISTA_CONF_ORG} | $sed -e s/'###AUTH1_IP###'/${AUTH1_IP}/g -e s/'###AUTH2_IP###'/${AUTH2_IP}/g -e s/'###AUTH2_PORT###'/'8777'/g > XML_TMP 2>&1
        if [ $? -ne 0 ] ; then
          FLAG=1
        fi
      # Only Primary
      else
        # get the IP addresses (PRIM) from setup.props
        # and transfer all files temp to INI_TMP
        $cat ${WIN_VISTA_FILE_PATH}/${WIN_VISTA_CONF_ORG} | $sed -e s/'###AUTH1_IP###'/${AUTH1_IP}/g -e s/'###AUTH2_IP###'/''/g -e s/'###AUTH2_PORT###'/'0'/g > XML_TMP 2>&1
        if [ $? -ne 0 ] ; then
          FLAG=1
        fi
      fi

      if [ ${FLAG} -ne 0 ] ; then
          # if an error occurred
          arize_err "dmy" "${WIN_VISTA_CONF} ${FILE_REPLACE_MISS}"
          #exit $FLAG
      else
          # if no errors occurred
          # XML FILE rename (move)
          $mv XML_TMP ${WIN_VISTA_FILE_PATH}/${WIN_VISTA_CONF} 2>&1
      fi
    fi
  fi
fi

# module zip
if [ $FLAG -eq 0 -a "$WIN_SW" != "n" ] ; then
    # ZIP file locate client path
    CD_PWD=`$pwd`
    ####### XP #######
    cd ${WIN_XP_FILE_PATH}
    $zip ${WIN_DOWNLOAD_PATH}/${WIN_XP_DOWNLOAD_NEWMODULE} * -x *.org* > /dev/null 2>&1
    if [ $? -ne 0 ] ; then
      arize_err "WIN001" "File. [ ${WIN_XP_DOWNLOAD_NEWMODULE} ]"
      FLAG=1
    else
      write_log "${WINLOGON_MODULE_ARCHIVE}"
    fi

    ####### VISTA #######
    if [ $FLAG -eq 0 ] ; then
      cd ${WIN_VISTA_FILE_PATH}
      $zip ${WIN_DOWNLOAD_PATH}/${WIN_VISTA_DOWNLOAD_NEWMODULE} * -x *.org* > /dev/null 2>&1
      if [ $? -ne 0 ] ; then
        arize_err "WIN001" "File. [ ${WIN_VISTA_DOWNLOAD_NEWMODULE} ]"
        FLAG=1
      else
        write_log "${WINLOGON_MODULE_ARCHIVE_VISTA}"
      fi
      cd ${CD_PWD}
    fi
fi

# Delete comp module directory
if [ -d $MODULE_DIR ] ; then
  $rm -rf $MODULE_DIR > /dev/null 2>&1
fi

# result
if [ $FLAG -eq 0 ] ; then
  $echo "${END}"
  write_log "${SMX_WINLOGON_MODULESET}"
else
  $echo "${OCCURED_ERROR}"
  $echo ""
  msg_err "${SMX_WINLOGON_MODULE_MISS}" \
          "${PROSECC_QUIT}" \
          " " \
          "<!> ${NOEXPECT_ERROR_SAPORT}"
  arize_err "dmy" "${SMX_WINLOGON_MODULE_MISS}"
fi

exit ${FLAG}
