diff --git a/src/gitlab-tofu.sh b/src/gitlab-tofu.sh
index 45fd3001b9e2faabd401929f0616a30cfdcd960a..8a27947e7023c712df87748c98c9fea91cf0d719 100644
--- a/src/gitlab-tofu.sh
+++ b/src/gitlab-tofu.sh
@@ -1,11 +1,10 @@
-#!/bin/sh -e
-
-# ATTENTION: this script is yet JUST A COPY of https://gitlab.com/gitlab-org/terraform-images/-/blob/master/src/bin/gitlab-terraform.sh
-#            with some VERY MINIMAL MODIFICATIONS.
-#            It definitely needs cleanup in the future.
+#!/usr/bin/env sh
 
+# set some shell options
+set -o errexit
+ 
 if [ "${DEBUG_OUTPUT}" = "true" ]; then
-    set -x
+    set -o xtrace
 fi
 
 # Feature Flags
@@ -37,6 +36,29 @@ else # All other shells: examine $0 for known shell binary filenames.
   case ${0##*/} in sh|-sh|dash|-dash) sourced=1;; esac
 fi
 
+# Dependencies
+# ============
+# Defines all the external dependencies and checks if they exist, if not, abort with an error.
+dependencies="dirname basename pwd sed idn2 jq tofu"
+
+if [ -n "$ZSH_VERSION" ]; then
+  # ZSH is the only supported SHELL that does not split by word by default,
+  # so we set this option to actually do it.
+  setopt sh_word_split
+fi
+
+for dep in $dependencies; do
+  if ! command -v "$dep" >/dev/null 2>&1; then
+    echo "Error: gitlab-tofu is missing dependency: '$dep'" >&2
+    exit 1
+  fi
+done
+
+if [ -n "$ZSH_VERSION" ]; then
+  # see comment above when setting sh_word_split.
+  unsetopt sh_word_split
+fi
+
 JQ_PLAN='
   (
     [.resource_changes[]?.change.actions?] | flatten