sf
#!/usr/bin/env bash
# The script is used as a wrapper for the Python script. It checks if the environment is set up correctly and sources the init.sh file.
# The init.sh file is used to set up the environment variables.
__sf_prechecks() {
local SCRIPTS_DIR="${SCRIPTS_DIR:-/usr/local/share/scripts}"
local PYTHON_BIN="${PYTHON_BIN:-$(command -v python3 || command -v python || echo /usr/bin/env python3)}"
status=0
if ! command -v "$PYTHON_BIN" &> /dev/null; then
echo "[ERROR] $PYTHON_BIN could not be found. Please install python3 and try again."
status=1
return
fi
if [ -z "$PROFILE_ENV_FILE" ]; then
local PROFILE_ENV_FILE="$HOME/.profile_env"
if [ -f "$PROFILE_ENV_FILE" ]; then
echo "[INFO] Sourcing environment file '$PROFILE_ENV_FILE'..."
source "$PROFILE_ENV_FILE"
else
echo "[ERROR] Could not find env file '$PROFILE_ENV_FILE'. Please make sure the environment is set up correctly."
status=1
return
fi
fi
if [ -z "$SCRIPTS_DIR" ]; then
echo "[ERROR] SCRIPTS_DIR is not set. Please make sure the environment is set up correctly."
status=1
return
fi
if [ ! -f "$SCRIPTS_DIR/sf/.dependencies_installed" ]; then
echo "Installing required Python packages..."
. "$PYTHON_BIN -m pip install -r $SCRIPTS_DIR/sf/requirements.txt" &> /dev/null
touch "$SCRIPTS_DIR/sf/.dependencies_installed"
fi
local sf_script="$SCRIPTS_DIR/sf/sf.py"
if [ ! -f "$sf_script" ]; then
echo "[ERROR] Could not find the main script $sf_script. Please make sure the script is in the correct location."
status=1
return
fi
local sf_utils="$SCRIPTS_DIR/sf/sf_utils.py"
if [ ! -f "$sf_utils" ]; then
echo "[ERROR] Could not find the functions file in $sf_utils/sf/sf_functions. Please make sure the file is in the correct location."
status=1
return
fi
echo "[SF $SF_VERSION]"
}
__run_sf() {
export PYTHONPATH="$SCRIPTS_DIR/sf/sf_functions:$PYTHONPATH"
$PYTHON_BIN $SCRIPTS_DIR/sf/sf.py $@
}
# prechecks="$(__sf_prechecks)"
#[ __sf_prechecks != "0" ] && exit 1
# if [[ "$prechecks" != "0" ]]; then
# echo "[ERROR] $prechecks] Prechecks failed. Exiting..."
# exit 1
# fi
__sf_prechecks
if [ "$status" -ne 0 ]; then
echo "[ERROR] $status - Prechecks failed. Exiting..."
exit 1
fi
echo
__run_sf "$@"
echo