#!/bin/bash
# $Copyright: Copyright (c) 2022 Veritas Technologies LLC. All rights reserved $

if [ $# -eq 0 ];then
    echo "Enter 1 to enable FIPS mode or 0 to disable FIPS mode."
    exit 1
fi

if [ $1 -ne 0 ] && [ $1 -ne 1 ];then
    echo "Enter 1 to enable FIPS mode or 0 to disable FIPS mode."
    exit 1
fi

value=$1

set_client_fips_mode(){

    if [ -f "/etc/nbapp-release" -o -f "/etc/ltr-app-release" ];then
        if [ ! -f "/etc/pdregistry.cfg" ];then

            if [ ! -f "/usr/openv/lib/ost-plugins/fips.conf" ];then
                touch "/usr/openv/lib/ost-plugins/fips.conf"
            fi
            
            "/usr/openv/pdde/pdag/bin/pdcfg" --write="/usr/openv/lib/ost-plugins/fips.conf" --section=Main --option=FIPS --value=$value

            echo "Change only the MSDP client's FIPS mode."
            if [ $value -eq 1  ];then
                echo "Successfully enabled FIPS mode. Restart the MSDP services on the server and client."
            else
                echo "Successfully disabled FIPS mode. Restart the MSDP services on the server and client."
            fi

            exit 0 
        fi
    fi
   
    path_tmp0=`cat /etc/pdregistry.cfg | awk "/Install/{getline;print}"`
    agent_path=`echo ${path_tmp0##*=}`"/../lib/ost-plugins/fips.conf"
    #echo $agent_path
    if [ ! -f "$agent_path" ];then
        touch $agent_path
    fi

    pdcfg_path=`echo ${path_tmp0##*=}`"/pdag/bin/pdcfg"
    #echo $pdcfg_path
    $pdcfg_path --write="${agent_path}" --section=Main --option=FIPS --value=$value
}

set_client_fips_mode

ret=`grep -w "Agent\.cfg" /etc/pdregistry.cfg`
if [ "$ret" == "" ]
then
    echo "Change the MSDP server's FIPS mode." 
    path_tmp1=`$pdcfg_path --read "/etc/pdregistry.cfg" --section "StoragePoolAuthority" --option=configfilepath`
    spa_path=`echo ${path_tmp1##*=}`
    if [ ! -f "$spa_path" ];then
        $pdcfg_path --write="${agent_path}" --section=Main --option=FIPS --value=0
        echo "Incorrect path set for MSDP Deduplication Manager: spad. Unable to set the FIPS mode."
        exit 1
    fi

    #echo $spa_path
    $pdcfg_path --write="${spa_path}" --section=Main --option=FIPS --value=$value

    path_tmp2=`$pdcfg_path --read "/etc/pdregistry.cfg" --section "ContentRouter" --option=configfilepath`
    spoold_path=`echo ${path_tmp2##*=}`
    if [ ! -f "$spoold_path" ]
    then
        $pdcfg_path --write="${agent_path}" --section=Main --option=FIPS --value=0
        $pdcfg_path --write="${spa_path}" --section=Main --option=FIPS --value=0
        echo "Incorrect path set for MSDP Deduplication Engine: spoold. Unable to set the FIPS mode."
        exit 1
    fi

    #echo $spoold_path
    $pdcfg_path --write="${spoold_path}" --section=Main --option=FIPS --value=$value

    $pdcfg_path --write=/etc/pdregistry.cfg --section=OpenCloudStorageDaemon --option=FIPS --value=$value
else
    echo "Change only the MSDP client's FIPS mode."
fi

if [ $value -eq 1  ]
then
    echo "Successfully enabled FIPS mode. Restart the MSDP services on the server and client."
else
    echo "Successfully disabled FIPS mode. Restart the MSDP services on the server and client."
fi





