#!/bin/sh

have(){
    command -v $1 2> /dev/null && echo -n $1
}

WGET=$(have wget)
CURL=$(have curl)
APT=$(have apt-get)
APK=$(have apk)
YUM=$(have yum)
PACMAN=$(have pacman)


RED='\033[0;31m'
GREEN='\033[0;32m' 
NC='\033[0m' # No Color

red() {
    printf "${RED}$@${NC}\n"
}

green() {
    printf "${GREEN}$@${NC}\n"
}


install(){
    if   [ ! -z "$APT" ] ; then
        apt-get update
        apt-get install -y $1
    elif [ ! -z "$APK" ] ; then 
        apk upgrade
        apk add $1
    elif [ ! -z "$YUM" ] ; then 
        yum -y install $1
    elif [ ! -z "$PACMAN" ] ; then 
        yes | pacman -Syu $1
    fi
}

download_cert(){
    O=
    if [ ! -z "$WGET" ] ; then
        O=$(wget -O - http://api.internal.netfree.link/ca/netfree-ca.crt 2> /dev/null) 2> /dev/null 
    else
        if [ -z "$CURL" ] ; then
            install curl
            CURL=$(have curl)
        fi
        O=$(curl http://api.internal.netfree.link/ca/netfree-ca.crt 2> /dev/null)
    fi
    echo "$O"
}

append_file(){
    #sh -c 'echo "$1" >> $2'
    touch "$2"
    echo "$1" | tee -a "$2" > /dev/null
}

set_env(){
    E=$1
    if [ ! -f  /etc/environment ] || ! grep -q -e "$E" /etc/environment ; then 
        append_file "$E" /etc/environment
    fi
    export "$E"
}



CERT=$(download_cert)

if echo "$CERT" | grep -q "BEGIN CERTIFICATE" ; then

    if [ -z "$CURL" ] ; then
        install curl
        CURL=$(have curl)
    fi

    command -v "update-ca-certificates" 2> /dev/null || (
        install ca-certificates
        update-ca-certificates
    )

    BUNDLE=

    if [ -f /etc/pki/tls/certs/ca-bundle.crt ]; then 
        BUNDLE=/etc/pki/tls/certs/ca-bundle.crt
    fi

    if [ -f /etc/ssl/certs/ca-certificates.crt ]; then 
        BUNDLE=/etc/ssl/certs/ca-certificates.crt
    fi

    DIRCA=

    if [ -d /usr/local/share/ca-certificates ]; then 
        DIRCA=/usr/local/share/ca-certificates
    fi

    if [ -d /etc/pki/ca-trust/source/anchors ]; then 
        DIRCA=/etc/pki/ca-trust/source/anchors
    fi

    if [ -d /etc/ca-certificates/trust-source/anchors ]; then 
        DIRCA=/etc/ca-certificates/trust-source/anchors
    fi

    CERT1L=$(echo "$CERT" | tr -d \\n\\r)
    CERTS1L=$(cat "$BUNDLE" | tr -d \\n\\r)

    if echo "$CERTS1L" | grep -q -e "$CERT1L" ; then
        echo "installed"
    else
        
        
        append_file "$CERT" "$BUNDLE"

        append_file "$CERT" "${DIRCA}/netfree-ca.crt"

        update-ca-certificates
        update-ca-trust
        
        if [ ! -z "$APK" ]; then 
            append_file "$CERT" "$BUNDLE"
        fi

        ln -s "$BUNDLE" /etc/ca-bundle.crt
        # for nodejs
        set_env "NODE_EXTRA_CA_CERTS=/etc/ca-bundle.crt"

        # for requests python
        set_env "REQUESTS_CA_BUNDLE=/etc/ca-bundle.crt" 

        # for python
        set_env "SSL_CERT_FILE=/etc/ca-bundle.crt"

        cat /etc/environment

        ## test
        TEST=
        if [ ! -z "$CURL" ] ; then
            TEST=$(curl https://test.internal.netfree.link/user/0 2>&1 )  
        else
            TEST=$(wget -O - https://test.internal.netfree.link/user/0 2>&1 )
        fi

        if echo "$TEST" | grep -q "userKey" ; then
            green "Certificate works successfully"
        elif echo "$TEST" | grep -q "unable to get local issuer" ; then
            red "Certificate error"
            exit 1
        else
            red "Some error"
        fi
    fi
else
    red "Network not NetFree"
fi