#!/bin/bash

# Define the directory path and URLs
client_dir="/home/ssleverywhere_client"
ssleverywhere_script_url="http://192.168.250.16/ssleverywhere"  # Replace with the actual URL

# Create the /home/ssleverywhere_client/ directory if it doesn't exist
#if [ ! -d "$client_dir" ]; then
 #   mkdir -p "$client_dir"
#fi


if [ "$(id -u)" -ne 0 ]; then
    echo "Error: This script must be run with root privileges (use sudo)."
    exit 1
fi

#user=$(whoami)

#mkdir -p /home/$user/ssleverywhere_client
#mkdir -p /home/$user/ssleverywhere_client/certs
#mkdir -p /home/$user/ssleverywhere_client/csr
#mkdir -p /home/$user/ssleverywhere_client/private

#chmod 777 /home/$user/ssleverywhere_client
#chmod 777 /home/$user/ssleverywhere_client/certs
#chmod 777 /home/$user/ssleverywhere_client/csr
#chmod 777 /home/$user/ssleverywhere_client/private

# Download the ssleverywhere script and place it in /bin/openhsm
if command -v wget &>/dev/null; then
    wget -O /bin/ssleverywhere "$ssleverywhere_script_url"
elif command -v curl &>/dev/null; then
    curl -o /bin/ssleverywhere "$ssleverywhere_script_url"
else
    echo "Error: Neither 'wget' nor 'curl' is available. Please install one of them to download the script."
    exit 1
fi

# Make the downloaded script executable
chmod +x /bin/ssleverywhere

echo "ssleverywhere script has been downloaded and placed in /bin/ssleverywhere."


user=$SUDO_USER
echo "Creating files at /home/$user"


mkdir -p /home/$user/ssleverywhere_client 
mkdir -p /home/$user/ssleverywhere_client/certs
mkdir -p /home/$user/ssleverywhere_client/csr
mkdir -p /home/$user/ssleverywhere_client/private

chmod 777 /home/$user/ssleverywhere_client
chmod 777 /home/$user/ssleverywhere_client/certs
chmod 777 /home/$user/ssleverywhere_client/csr
chmod 777 /home/$user/ssleverywhere_client/private
