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

#Used to specify the start directory for finding acroread

set_lang_ITA()
{
    ST_SEP_INFO_01="Specificare la directory di installazione per Adobe Reader 8.1 [%s]."
    ST_SEP_INFO_05="Impostazioni dei nuovi contesti per i file di libreria in corso"
    ST_SEP_INFO_09="Errore del comando \'%s\'."
    ST_SEP_INFO_10=" possibile che la patch non sia stata completata correttamente. Il registro  disponibile nella posizione %s."
    ST_SEP_INFO_08="Impostazione dei contesti completata. Il registro  disponibile nella posizione %s."
    ST_SEP_INFO_02="ERRORE INTERNO ..... Nessun nome OS corrispondente."
    ST_SEP_INFO_06="I file di libreria verranno aggiornati per renderli compatibili con SELinux."
    ST_SEP_INFO_03="Impossibile trovare i file appropriati nel percorso di installazione."
    ST_SEP_INFO_04="Reimpostazione dei contesti precedenti in corso..."
    ST_SEP_INFO_07="Impossibile trovare la cartella di installazione."
}


set_lang_ITA_utf()
{
    ST_SEP_INFO_01="Specificare la directory di installazione per Adobe Reader 8.1 [%s]."
    ST_SEP_INFO_05="Impostazioni dei nuovi contesti per i file di libreria in corso…"
    ST_SEP_INFO_09="Errore del comando \'%s\'."
    ST_SEP_INFO_10="È possibile che la patch non sia stata completata correttamente. Il registro è disponibile nella posizione %s."
    ST_SEP_INFO_08="Impostazione dei contesti completata. Il registro è disponibile nella posizione %s."
    ST_SEP_INFO_02="ERRORE INTERNO ..... Nessun nome OS corrispondente."
    ST_SEP_INFO_06="I file di libreria verranno aggiornati per renderli compatibili con SELinux."
    ST_SEP_INFO_03="Impossibile trovare i file appropriati nel percorso di installazione."
    ST_SEP_INFO_04="Reimpostazione dei contesti precedenti in corso..."
    ST_SEP_INFO_07="Impossibile trovare la cartella di installazione."
}

if echo $LANG | grep -i utf >/dev/null; then
    set_lang_ITA_utf
else
    set_lang_ITA
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 $?
