#!/usr/bin/env bash

# if file exists, use it
__temp_var__certs_file="$FORNIX_FOLDER/settings/extensions/nix/cacert.pem"
if [ -f "$__temp_var__certs_file" ]
then
    export SSL_CERT_FILE="$__temp_var__certs_file"
    export NIX_SSL_CERT_FILE="$SSL_CERT_FILE"
    # for some reason git needs its own var 
    export GIT_SSL_CAINFO="$SSL_CERT_FILE"
    export CURL_CA_BUNDLE="$SSL_CERT_FILE"
    
    wgetrc_path="$FORNIX_HOME/.wgetrc"
    # ensure file exists
    if ! [ -f "$wgetrc_path" ]
    then
        rm -rf "$wgetrc_path" 2>/dev/null
        touch "$wgetrc_path"
    fi
    # check if already included
    if ! { cat "$wgetrc_path" | grep -F "$SSL_CERT_FILE" 1>/dev/null 2>/dev/null }
    then
        # TODO: this probably wont work with spaces in the path (breaks if any parent folder of the project has space in the name)
        echo "
ca_certificate = $SSL_CERT_FILE" >> "$wgetrc_path"
    fi
fi
unset __temp_var__certs_file
