#!/bin/sh
#
# Copyright (c) 1996-2007, Adobe Systems Incorporated
# All Rights Reserved
#

#Used to specify the start directory for finding acroread

set_lang_KOR_utf()
{
    ST_SEP_INFO_01="Adobe Reader 8.1[%s]의 설치 디렉토리를 입력하십시오."
    ST_SEP_INFO_05="라이브러리 파일의 새 컨텍스트 설정 중 ....."
    ST_SEP_INFO_09="명령 \'%s\' 실패."
    ST_SEP_INFO_10="패치 작업이 제대로 완료되지 않았습니다. %s에서 로그를 확인할 수 있습니다."
    ST_SEP_INFO_08="컨텍스트 설정이 완료되었습니다. %s에서 로그를 확인할 수 있습니다."
    ST_SEP_INFO_02="내부 오류. 일치하는 OS 이름이 없습니다."
    ST_SEP_INFO_06="SELinux에서 인식할 수 있도록 라이브러리 파일을 업데이트합니다. "
    ST_SEP_INFO_03="설치 경로에서 적절한 파일을 찾을 수 없습니다."
    ST_SEP_INFO_04="기존 컨텍스트 재설정 중 ....."
    ST_SEP_INFO_07="설치 폴더를 찾을 수 없습니다."
}


set_lang_KOR()
{
    ST_SEP_INFO_01="Adobe Reader 8.1[%s] ġ 丮 ԷϽʽÿ."
    ST_SEP_INFO_05="̺귯   ؽƮ   ....."
    ST_SEP_INFO_09=" \'%s\' ."
    ST_SEP_INFO_10="ġ ۾  Ϸ ʾҽϴ. %s α׸ Ȯ  ֽϴ."
    ST_SEP_INFO_08="ؽƮ  ϷǾϴ. %s α׸ Ȯ  ֽϴ."
    ST_SEP_INFO_02=" . ġϴ OS ̸ ϴ."
    ST_SEP_INFO_06="SELinux ν  ֵ ̺귯  Ʈմϴ. "
    ST_SEP_INFO_03="ġ ο   ã  ϴ."
    ST_SEP_INFO_04=" ؽƮ 缳  ....."
    ST_SEP_INFO_07="ġ  ã  ϴ."
}

if echo $LANG | grep -i utf >/dev/null; then
    set_lang_KOR_utf
else
    set_lang_KOR
fi

SCRIPT_PATH=

#Contains the install directory path for acroread
ACROREAD_DIR=

#OS specific for acroread
CONFIG_FOLDER=

#log file
LOG_FILE="/tmp/adobe_selinux_log"

#file context file
FILE_CONTEXT_FILE="/tmp/adobe_file_context"

#This function gets the complete path of this script.
GetScriptPath()
{
	CURRENT_PATH=`pwd`
	SCRIPT_PATH=`dirname "$0"`
	COMPLETE_PATH="${SCRIPT_PATH}/../../"
	cd "$COMPLETE_PATH"
	SCRIPT_PATH=`pwd`
	cd "$CURRENT_PATH"
}


#This function fetches the installation path if not specified and validates the existance of the plugin file.
GetAcroreadInfo()
{
    #Prompt the user for acroread's installation path if not specified on the command prompt
    if [ -z "$1" ]
	then
	GetScriptPath
	printf "$ST_SEP_INFO_01" "${SCRIPT_PATH}"
	read ACROREAD_DIR
	if [ -z "$ACROREAD_DIR" ]
	    then
	    ACROREAD_DIR="$SCRIPT_PATH"
	fi
    else
	ACROREAD_DIR="$1"
    fi

    OSNAME=`uname -s`
    if [ "$OSNAME" = "Linux" ]
	then
	CONFIG_FOLDER="intellinux"
    elif [ "$OSNAME" = "SunOS" ]
	then
	CONFIG_FOLDER="sparcsolaris"
    else
	printf "%s\n" "$ST_SEP_INFO_02"
	return 1
    fi
}


#This function fetches the absolute path of the file and dereferences them if it is a link.
GetAbsoluteFile()
{
    if [ -h "$1" ]
	then
	CURRENT_PATH=`pwd`
	cd `dirname $1`
	COMPLETE_PATH=`ls -l $1 | awk '{ print $NF }'`
	COMPLETE_PATH_DIR=`dirname $COMPLETE_PATH`
	COMPLETE_PATH_FILE=`basename $COMPLETE_PATH`
	cd $COMPLETE_PATH_DIR
	COMPLETE_PATH_DIR=`pwd`
	cd "$CURRENT_PATH"
	GetAbsoluteFile ${COMPLETE_PATH_DIR}/${COMPLETE_PATH_FILE}
    else
	CURRENT_PATH=`pwd`
	COMPLETE_PATH_DIR=`dirname $1`
	COMPLETE_PATH_FILE=`basename $1`
	cd $COMPLETE_PATH_DIR
	COMPLETE_PATH_DIR=`pwd`
	cd "$CURRENT_PATH"
	echo "${COMPLETE_PATH_DIR}/${COMPLETE_PATH_FILE}"
	return 0
    fi
}


PerformPatch()
{
    ERROR_OCCURED=0

    if [ ! -d "${ACROREAD_DIR}/Reader/${CONFIG_FOLDER}/lib" ] || 
       [ ! -d "${ACROREAD_DIR}/Reader/${CONFIG_FOLDER}/plug_ins" ] || 
       [ ! -d "${ACROREAD_DIR}/Reader/${CONFIG_FOLDER}/SPPlugins" ]
	then
	printf "%s\n" "$ST_SEP_INFO_03"
	return 1
    fi

    printf "%s\n" "$ST_SEP_INFO_04"
    echo "restorecon ----" >> $LOG_FILE
    restorecon -R -v -F $ACROREAD_DIR >> $LOG_FILE 2>&1
    if [ $? -ne 0 ]
	then
	printf "$ST_SEP_INFO_09\n" "restorecon"
	ERROR_OCCURED=1
    fi

    printf "%s\n" "$ST_SEP_INFO_05"
    echo "file listing ----" >> $LOG_FILE

#    No need to enforce the patch on library files in Reader 8
#
#    cd ${ACROREAD_DIR}/Reader/${CONFIG_FOLDER}/lib
#    LIB_FILES=`ls`
#    for i in $LIB_FILES
#      do
#      CURRENT_LIB_FILE=`GetAbsoluteFile $i`
#      #setfiles does the chcon for us, so we don't need to redo it.
#      #chcon -v -u system_u -r object_r -t shlib_t $CURRENT_LIB_FILE >> $LOG_FILE 2>&1
#      echo "$CURRENT_LIB_FILE -- system_u:object_r:shlib_t" >> $FILE_CONTEXT_FILE
#    done

    cd ${ACROREAD_DIR}/Reader/${CONFIG_FOLDER}/plug_ins
    LIB_FILES=`ls`
    for i in $LIB_FILES
      do
#    No need to enforce the patch on all plugins in Reader 8. Needed for only PPKLite.
	if [ $i == "PPKLite.api" ]
	then
      CURRENT_LIB_FILE=`GetAbsoluteFile $i`
      #setfiles does the chcon for us, so we don't need to redo it.
      #chcon -v -u system_u -r object_r -t shlib_t $CURRENT_LIB_FILE >> $LOG_FILE 2>&1
      echo "$CURRENT_LIB_FILE -- system_u:object_r:shlib_t" >> $FILE_CONTEXT_FILE
      fi
    done

#    No need to enforce the patch on all plugins in Reader 8
#     
#     cd ${ACROREAD_DIR}/Reader/${CONFIG_FOLDER}/SPPlugins
#     LIB_FILES=`ls`
#     for i in $LIB_FILES
#       do
#       CURRENT_LIB_FILE=`GetAbsoluteFile $i`
#       #setfiles does the chcon for us, so we don't need to redo it.
#       #chcon -v -u system_u -r object_r -t shlib_t $CURRENT_LIB_FILE >> $LOG_FILE 2>&1
#       echo "$CURRENT_LIB_FILE -- system_u:object_r:shlib_t" >> $FILE_CONTEXT_FILE
#     done

    LIB_FILES=`cat $FILE_CONTEXT_FILE | sort | uniq`
    echo "$LIB_FILES" > $FILE_CONTEXT_FILE

    echo "setfiles ----" >> $LOG_FILE
    setfiles -l -vv -W -F $FILE_CONTEXT_FILE $ACROREAD_DIR >> $LOG_FILE 2>&1
    if [ $? -ne 0 ]
	then
	printf "$ST_SEP_INFO_09\n" "setfiles"
	ERROR_OCCURED=1
    fi

    return $ERROR_OCCURED
}


Init()
{
    echo "" >> $LOG_FILE
    echo "" >> $LOG_FILE
    echo "--------------" >> $LOG_FILE
    CUR_DATE=`date`
    echo "$CUR_DATE" >> $LOG_FILE

    clear
    printf "%s\n" "$ST_SEP_INFO_06"
    GetAcroreadInfo "$1"
    if [ $? -ne 0 ]
	then
	return 1
    fi
    
    if [ ! -d "$ACROREAD_DIR" ]
	then
	printf "%s\n" "$ST_SEP_INFO_07"
	return 1
    fi

    PerformPatch
    return $?
}

Cleanup()
{
    rm -f $FILE_CONTEXT_FILE
#   rm -f $LOG_FILE
    if [ $1 -eq 0 ]
	then
	printf "$ST_SEP_INFO_08\n" "$LOG_FILE"
    else
	printf "$ST_SEP_INFO_10\n" "$LOG_FILE"
    fi
}

trap 'Cleanup ; exit 0' SIGINT
Init "$1"
Cleanup $?
