#/bin/bash

### 
# Script de patch pour le i-Parapheur signature SHA256

if [ -n "$1" ]
then
  PARAPH_PATH=$1
else
  PARAPH_PATH="/opt/iParapheur"
fi

PROP_PATH="$PARAPH_PATH/tomcat/shared/classes/alfresco-global.properties"

if [ -f "$PROP_PATH" ]
then
  while IFS='=' read -r key value
  do
    key=$(echo $key | tr '.' '_')
    eval "${key}"=\${"value"} 2> /dev/null
  done < "$PROP_PATH"
else
  echo "Fichier de propriétés introuvable"
  exit 1
fi

DATA_PATH="$dir_root/contentstore/"

if [ ! -d "$DATA_PATH" ]
then
  echo "Dossier de données 'alf_data' introuvable"
  exit 1
fi

# Get only URL ...
db_url=`echo "$db_url" | awk -F/ '{ print $3 }' | awk -F: '{ print $1 }'`
# Find and define mysql port
db_port=`echo "$db_url" | awk -F/ '{ print $3 }' | awk -F: '{ print $2 }'`

if [ -z "$db_port" ]
then
  db_port="3306"
fi

echo $db_port

OLD_UID="urn:oid:1.2.250.1.131.1.5.18.21.1.4"
OLD_FAST_UID="urn:oid:1.2.250.1.5.3.1.1.8"
OLD_SLOW_UID="urn:oid:1.2.250.1.5.3.1.1.10"
NEW_UID="urn:oid:1.2.250.1.131.1.5.18.21.1.7"

OLD_HASH="Jkdb+aba0Hz6+ZPKmKNhPByzQ+Q="
OLD_FAST_HASH="q5M\/dx3\/162m+j6MFe8LzEAFDJk="
OLD_SLOW_HASH="G4CqRa9R5c9Yg+dzMH3gbEc4Kqo="
NEW_HASH="roF9+cfRHNPtVJolhdqfIqGMVuUXX8aR4rpiquf0u5E="

OLD_URL="https:\/\/portail.dgfip.finances.gouv.fr\/documents\/PS_Helios_DGFiP.pdf"
OLD_FAST_URL="http:\/\/www.fast.caissedesdepots.fr\/PS-SIGNATAIRE#"
OLD_SLOW_URL="http:\/\/www.s2low.org\/PolitiqueSignature-Agent"
NEW_URL="https:\/\/www.collectivites-locales.gouv.fr\/files\/files\/finances_locales\/dematerialisation\/ps_helios_dgfip.pdf"

NEW_DESC="Politique de signature Helios de la DGFiP"
OLD_FAST_DESC="Politique de Signature du Signataire"
OLD_SLOW_DESC="Politique de Signature de l'Agent"

echo "Application du patch en cours ..."

# -- delete all known signInfo properties in database
mysql -h $db_url -P $db_port -u $db_username -p$db_password -e 'DELETE np FROM alf_node_properties as np, alf_qname as qn WHERE qn.local_name="signInfo" AND np.qname_id=qn.id;' $db_name

if [ $? -ne 0 ]; then
    echo "Connexion a la base de donnée impossible"
    exit 1
fi

## TODELETE
FILES=`grep -rl -e "<pPolicyIdentifierID>$OLD_UID</pPolicyIdentifierID>" -e "<pPolicyIdentifierID>$OLD_FAST_UID</pPolicyIdentifierID>" -e "<pPolicyIdentifierID>$OLD_SLOW_UID</pPolicyIdentifierID>" $DATA_PATH`

# For S2low config
echo "$FILES" | xargs sed -i "s/$OLD_UID/$NEW_UID/g"
echo "$FILES" | xargs sed -i "s/$OLD_HASH/$NEW_HASH/g"
echo "$FILES" | xargs sed -i "s/$OLD_URL/$NEW_URL/g"

# For Fast config
echo "$FILES" | xargs sed -i "s/$OLD_FAST_UID/$NEW_UID/g"
echo "$FILES" | xargs sed -i "s/$OLD_FAST_HASH/$NEW_HASH/g"
echo "$FILES" | xargs sed -i "s/$OLD_FAST_URL/$NEW_URL/g"
echo "$FILES" | xargs sed -i "s/$OLD_FAST_DESC/$NEW_DESC/g"

# For very old Slow config
echo "$FILES" | xargs sed -i "s/$OLD_SLOW_UID/$NEW_UID/g"
echo "$FILES" | xargs sed -i "s/$OLD_SLOW_HASH/$NEW_HASH/g"
echo "$FILES" | xargs sed -i "s/$OLD_SLOW_URL/$NEW_URL/g"
echo "$FILES" | xargs sed -i "s/$OLD_SLOW_DESC/$NEW_DESC/g"

echo "Patch appliqué avec succès"
exit 0
