##----------------------------------------------------------------------------
## Script         : format_ligne.lib
## Module Puppet  : gnc-script_core_dtsi
## Auteur         : Emmanuel Confrere
## Date           : 10-02-2016
## Version        : 1.0.0
## Objet          : Permet de formater le texte dans la fonction fct_affiche_ligne.
##           
## Fonction       : fct_format Texte taille justification
##
## Detail         : Ne pas appeler directement.
##
# ----------------------------------------------------------------------------
# Mise a jour :
# 1.0.0 10-02-2016 - Emmanuel Confrere - Creation : Reprise du script du socle Oracle
##----------------------------------------------------------------------------

function fct_format
{
local L_TEXT="$1"
local L_TAILLE=$2
local L_JUSTIFIER=$3
local L_TMP=""
local L_TMP2=""
local L_RESULT=""
local L_REPETITION=0

case "${L_JUSTIFIER}" in
  "r" ) L_TMP=1
        L_REPETITION=$(( ${L_TAILLE} / ${#L_TEXT} + 1))
        while [ ${L_TMP} -le ${L_REPETITION} ]
        do
    L_RESULT="${L_RESULT}${L_TEXT}"
    L_TMP=$(( ${L_TMP} + 1 ))
        done ;;
  "c" ) L_TMP=1
        L_REPETITION=$(( ${L_TAILLE} - ${#L_TEXT} ))
        L_REPETITION=$(( ${L_REPETITION} / 2 ))
        L_TMP2=$(( ${L_TAILLE} - ${#L_TEXT} - ${L_REPETITION} ))
        while [ ${L_TMP} -le ${L_REPETITION} -a ${L_REPETITION} -gt 0 ]
              do
    L_RESULT="${L_RESULT} "
    L_TMP=$(( ${L_TMP} + 1 ))
        done
        if [ ${L_REPETITION} -gt 0 ]
        then
            if [ ${L_REPETITION} -eq ${L_TMP2} ]
                  then
          L_RESULT="${L_RESULT}${L_TEXT}${L_RESULT}"
            else
          L_RESULT="${L_RESULT}${L_TEXT}${L_RESULT} "
            fi
        else
      L_RESULT="${L_TEXT}"
        fi ;;
  "d" ) L_TMP=1
        L_REPETITION=$(( ${L_TAILLE} - ${#L_TEXT} ))
        while [ ${L_TMP} -le ${L_REPETITION} -a ${L_REPETITION} -gt 0 ]
        do
    L_RESULT="${L_RESULT} "
    L_TMP=$(( ${L_TMP} + 1 ))
        done
        L_RESULT="${L_TEXT}${L_RESULT}" ;;
esac
L_RESULT=${L_RESULT:0:${L_TAILLE}}
echo ${L_RESULT}
return 0
}
typeset -Ffx fct_format