#!/bin/bash
###############################################################################
# Cowrie Debian Package Builder
#
# Project : Cowrie SSH/Telnet Honeypot
# Package : cowrie
# Install : /var/lib/cowrie
# User    : cowrie
#
# Build:
#   chmod +x build-deb
#   ./build-deb
#
# Install:
#   sudo dpkg -i dist/cowrie_VERSION_ARCH.deb
#
# Run:
#   cowrie status
#   sudo -u cowrie cowrie foreground
#
###############################################################################

set -e

###############################################################################
# Configuration
###############################################################################

PROJECT="cowrie"

INSTALL_DIR="/var/lib/cowrie"
COWRIE_USER="cowrie"
COWRIE_GROUP="cowrie"

BUILD_DIR="$(pwd)/.deb-build"
PKG_ROOT="${BUILD_DIR}/pkg"
DIST_DIR="$(pwd)/dist"

APP_DIR="${PKG_ROOT}${INSTALL_DIR}/app"
VENV_DIR="${PKG_ROOT}${INSTALL_DIR}/venv"
ETC_DIR="${PKG_ROOT}${INSTALL_DIR}/etc"
LOG_DIR="${PKG_ROOT}${INSTALL_DIR}/log"
LIB_DIR="${PKG_ROOT}${INSTALL_DIR}/lib"
RUN_DIR="${PKG_ROOT}${INSTALL_DIR}/run"

DEBIAN_DIR="${PKG_ROOT}/DEBIAN"

DEB_ARCH="$(dpkg --print-architecture)"

###############################################################################
# Version detection
###############################################################################

VERSION="1.0.0"

if git rev-parse --git-dir >/dev/null 2>&1; then

    GIT_VERSION="$(git describe --tags --always 2>/dev/null || true)"

    if [ -n "${GIT_VERSION}" ]; then

        GIT_VERSION="${GIT_VERSION#v}"

        # Remove git commit suffix.
        GIT_VERSION="$(
            printf '%s' "${GIT_VERSION}" |
            sed -E 's/-g[0-9a-fA-F]+$//'
        )"

        # Only accept clean numeric semantic versions.
        if printf '%s' "${GIT_VERSION}" |
            grep -Eq '^[0-9]+(\.[0-9]+)*$'; then

            VERSION="${GIT_VERSION}"

        fi

    fi

fi

###############################################################################
# Header
###############################################################################

echo
echo "============================================================"
echo "          Cowrie Debian Package Builder"
echo "============================================================"
echo
echo " Package : ${PROJECT}"
echo " Version : ${VERSION}"
echo " Arch    : ${DEB_ARCH}"
echo " Install : ${INSTALL_DIR}"
echo " User    : ${COWRIE_USER}"
echo
echo "============================================================"
echo

###############################################################################
# Check source tree
###############################################################################

echo "[INFO] Checking Cowrie source..."

if [ ! -f "pyproject.toml" ]; then
    echo "[ERROR] pyproject.toml not found."
    echo "[ERROR] Run this script from the Cowrie source directory."
    exit 1
fi

if [ ! -d "src/cowrie" ]; then
    echo "[ERROR] src/cowrie directory not found."
    exit 1
fi

if [ ! -f "requirements.txt" ]; then
    echo "[ERROR] requirements.txt not found."
    exit 1
fi

echo "[OK] Cowrie source detected."

###############################################################################
# Build dependencies
###############################################################################

echo "[INFO] Checking build dependencies..."

REQUIRED_COMMANDS="
dpkg
dpkg-deb
python3
git
rsync
sed
grep
head
"

for cmd in ${REQUIRED_COMMANDS}; do

    if ! command -v "${cmd}" >/dev/null 2>&1; then
        echo "[ERROR] Missing command: ${cmd}"
        exit 1
    fi

done

if ! python3 -m venv --help >/dev/null 2>&1; then

    echo "[ERROR] Python venv module is unavailable."
    echo
    echo "Install it with:"
    echo
    echo "  sudo apt install python3-venv"
    echo

    exit 1

fi

echo "[OK] Build dependencies available."

###############################################################################
# Clean previous build
###############################################################################

echo "[INFO] Cleaning previous build..."

rm -rf "${BUILD_DIR}"
rm -rf "${DIST_DIR}"

mkdir -p "${PKG_ROOT}"
mkdir -p "${DIST_DIR}"

###############################################################################
# Create package filesystem
###############################################################################

echo "[INFO] Creating package filesystem..."

mkdir -p \
    "${APP_DIR}" \
    "${VENV_DIR}" \
    "${ETC_DIR}" \
    "${LOG_DIR}" \
    "${LIB_DIR}" \
    "${RUN_DIR}" \
    "${DEBIAN_DIR}" \
    "${PKG_ROOT}/usr/bin" \
    "${PKG_ROOT}/etc/init.d" \
    "${PKG_ROOT}/etc/systemd/system"

###############################################################################
# Copy Cowrie application
###############################################################################

echo "[INFO] Copying Cowrie application..."

rsync -a \
    --exclude=".git" \
    --exclude=".deb-build" \
    --exclude="dist" \
    --exclude="venv" \
    --exclude="__pycache__" \
    --exclude="*.pyc" \
    ./ \
    "${APP_DIR}/"

###############################################################################
# Copy configuration
###############################################################################

echo "[INFO] Copying Cowrie configuration..."

if [ -d "etc" ]; then

    rsync -a \
        "etc/" \
        "${ETC_DIR}/"

fi

###############################################################################
# Ensure cowrie.cfg exists
###############################################################################

if [ ! -f "${ETC_DIR}/cowrie.cfg" ]; then

    if [ -f "${ETC_DIR}/cowrie.cfg.dist" ]; then

        echo "[INFO] Creating cowrie.cfg from cowrie.cfg.dist..."

        cp \
            "${ETC_DIR}/cowrie.cfg.dist" \
            "${ETC_DIR}/cowrie.cfg"

    else

        echo "[INFO] Creating default cowrie.cfg..."

        cat > "${ETC_DIR}/cowrie.cfg" <<'EOF'
[honeypot]
hostname = srv
download_limit_size = 10485760

[ssh]
enabled = true
listen_endpoints = tcp:2222:interface=0.0.0.0

[telnet]
enabled = true
listen_endpoints = tcp:2223:interface=0.0.0.0

[output_jsonlog]
enabled = true
logfile = ${COWRIE_HOME}/var/log/cowrie/cowrie.json
EOF

    fi

fi

###############################################################################
# Documentation
###############################################################################

echo "[INFO] Copying documentation..."

for file in \
    README.rst \
    CHANGELOG.rst \
    LICENSE.rst \
    INSTALL.rst \
    CONTRIBUTING.rst; do

    if [ -f "${file}" ]; then

        cp \
            "${file}" \
            "${PKG_ROOT}${INSTALL_DIR}/"

    fi

done

###############################################################################
# Create Python virtual environment
###############################################################################

echo "[INFO] Creating Python virtual environment..."

#
# --copies is important.
#
# The default Python venv can create symbolic links.
# We want a package that contains its own executable rather than a link
# back to the build machine.
#

python3 -m venv \
    --copies \
    "${VENV_DIR}"

###############################################################################
# Venv paths
###############################################################################

VENV_PYTHON="${VENV_DIR}/bin/python"
VENV_PIP="${VENV_DIR}/bin/pip"

FINAL_VENV_PYTHON="${INSTALL_DIR}/venv/bin/python"

###############################################################################
# Verify venv Python
###############################################################################

if [ ! -x "${VENV_PYTHON}" ]; then

    echo "[ERROR] Virtual environment Python was not created:"
    echo "        ${VENV_PYTHON}"

    exit 1

fi

###############################################################################
# Upgrade packaging tools
###############################################################################

echo "[INFO] Installing Python build tools..."

"${VENV_PYTHON}" -m pip install \
    --upgrade \
    pip \
    setuptools \
    wheel \
    setuptools-scm

###############################################################################
# Install Cowrie dependencies
###############################################################################

echo "[INFO] Installing Cowrie dependencies..."

"${VENV_PIP}" install \
    --no-cache-dir \
    -r requirements.txt

###############################################################################
# Install Cowrie Python package
###############################################################################

echo "[INFO] Installing Cowrie Python package..."

#
# Cowrie uses setuptools-scm.
#
# The .git directory is deliberately excluded from the package.
# Therefore setuptools-scm needs an explicit version during build.
#

export SETUPTOOLS_SCM_PRETEND_VERSION="${VERSION}"

"${VENV_PIP}" install \
    --no-cache-dir \
    --no-deps \
    .

unset SETUPTOOLS_SCM_PRETEND_VERSION

###############################################################################
# Fix Python virtualenv entrypoints
###############################################################################

echo "[INFO] Fixing Python virtualenv entrypoints..."

#
# pip creates scripts such as:
#
#   bin/twistd
#   bin/cowrie
#   bin/trial
#
# Their original shebang points to the build-time Python:
#
#   #!/home/koosha/.../.deb-build/pkg/var/lib/cowrie/venv/bin/python
#
# That path does not exist after installing the .deb.
#
# Rewrite only Python entrypoint scripts.
#
# DO NOT modify:
#
#   activate
#   activate.csh
#   activate.fish
#   Activate.ps1
#
# Those are virtualenv activation scripts and legitimately contain
# VIRTUAL_ENV information from the build environment.
#

for script in "${VENV_DIR}/bin/"*; do

    [ -f "${script}" ] || continue

    basename_script="$(basename "${script}")"

    case "${basename_script}" in

        activate)
            continue
            ;;

        activate.csh)
            continue
            ;;

        activate.fish)
            continue
            ;;

        Activate.ps1)
            continue
            ;;

    esac

    #
    # Check whether this is a text file.
    #

    if ! LC_ALL=C grep -Iq . "${script}" 2>/dev/null; then
        continue
    fi

    SHEBANG="$(sed -n '1p' "${script}" 2>/dev/null || true)"

    case "${SHEBANG}" in

        "#!"*"python"*)

            sed -i \
                "1c#!${FINAL_VENV_PYTHON}" \
                "${script}"

            ;;

    esac

done

###############################################################################
# Verify virtualenv
###############################################################################

echo "[INFO] Verifying virtualenv..."

if [ ! -x "${VENV_DIR}/bin/python" ]; then

    echo "[ERROR] Venv Python was not created."

    exit 1

fi

if [ ! -x "${VENV_DIR}/bin/twistd" ]; then

    echo "[ERROR] twistd was not installed."

    exit 1

fi

if [ ! -x "${VENV_DIR}/bin/cowrie" ]; then

    echo "[ERROR] Cowrie executable was not installed."

    exit 1

fi

echo "[OK] Python virtual environment ready."

###############################################################################
# Verify Python entrypoints
###############################################################################

echo "[INFO] Checking Python entrypoints..."

ENTRYPOINT_ERROR=0

for script in "${VENV_DIR}/bin/"*; do

    [ -f "${script}" ] || continue

    basename_script="$(basename "${script}")"

    case "${basename_script}" in

        activate)
            continue
            ;;

        activate.csh)
            continue
            ;;

        activate.fish)
            continue
            ;;

        Activate.ps1)
            continue
            ;;

    esac

    #
    # Ignore binary files.
    #

    if ! LC_ALL=C grep -Iq . "${script}" 2>/dev/null; then
        continue
    fi

    SHEBANG="$(sed -n '1p' "${script}" 2>/dev/null || true)"

    case "${SHEBANG}" in

        "#!"*"python"*)

            if [ "${SHEBANG}" != "#!${FINAL_VENV_PYTHON}" ]; then

                echo "[ERROR] Incorrect Python shebang:"
                echo "        ${script}"
                echo "        ${SHEBANG}"

                ENTRYPOINT_ERROR=1

            fi

            ;;

    esac

done

if [ "${ENTRYPOINT_ERROR}" -ne 0 ]; then

    echo
    echo "[ERROR] One or more Python entrypoints contain an incorrect"
    echo "        interpreter path."
    echo

    exit 1

fi

echo "[OK] Python entrypoints verified."

###############################################################################
# Check build-time paths in Python entrypoints only
###############################################################################

echo "[INFO] Checking Python entrypoints for build-time paths..."

BUILD_PATH_FOUND=0

for script in "${VENV_DIR}/bin/"*; do

    [ -f "${script}" ] || continue

    basename_script="$(basename "${script}")"

    case "${basename_script}" in

        activate)
            continue
            ;;

        activate.csh)
            continue
            ;;

        activate.fish)
            continue
            ;;

        Activate.ps1)
            continue
            ;;

    esac

    #
    # Ignore binary files.
    #

    if ! LC_ALL=C grep -Iq . "${script}" 2>/dev/null; then
        continue
    fi

    SHEBANG="$(sed -n '1p' "${script}" 2>/dev/null || true)"

    case "${SHEBANG}" in

        "#!"*"python"*)

            if grep -Fq "${BUILD_DIR}" "${script}" 2>/dev/null; then

                echo "[ERROR] Build-time path found:"
                echo "        ${script}"

                BUILD_PATH_FOUND=1

            fi

            ;;

    esac

done

if [ "${BUILD_PATH_FOUND}" -ne 0 ]; then

    echo
    echo "[ERROR] A Python entrypoint still contains a build-time path."
    echo

    exit 1

fi

echo "[OK] Python entrypoints contain no build-time paths."

###############################################################################
# Verify critical entrypoints
###############################################################################

echo "[INFO] Checking Cowrie entrypoints..."

for script in \
    "${VENV_DIR}/bin/python" \
    "${VENV_DIR}/bin/twistd" \
    "${VENV_DIR}/bin/cowrie"; do

    if [ ! -e "${script}" ]; then

        echo "[ERROR] Missing:"
        echo "        ${script}"

        exit 1

    fi

done

echo "[OK] Cowrie entrypoints available."

###############################################################################
# Create Cowrie application launcher
###############################################################################

echo "[INFO] Creating Cowrie launcher..."

mkdir -p "${APP_DIR}/bin"

cat > "${APP_DIR}/bin/cowrie" <<'EOF'
#!/bin/bash

###############################################################################
# Cowrie launcher
###############################################################################

set -e

COWRIE_HOME="/var/lib/cowrie"
COWRIE_VENV="${COWRIE_HOME}/venv"

export COWRIE_HOME
export PYTHONPATH="${COWRIE_HOME}/app/src${PYTHONPATH:+:${PYTHONPATH}}"

cd "${COWRIE_HOME}"

#
# Cowrie must never run as root.
#

if [ "$(id -u)" -eq 0 ]; then

    echo
    echo "ERROR: You must not run cowrie as root!"
    echo
    echo "Run it as the cowrie user:"
    echo
    echo "  sudo -u cowrie cowrie foreground"
    echo

    exit 1

fi

case "${1:-}" in

    start)

        exec "${COWRIE_VENV}/bin/twistd" \
            --pidfile "${COWRIE_HOME}/run/cowrie.pid" \
            cowrie

        ;;

    stop)

        if [ -f "${COWRIE_HOME}/run/cowrie.pid" ]; then

            PID="$(cat "${COWRIE_HOME}/run/cowrie.pid")"

            if kill -0 "${PID}" 2>/dev/null; then
                kill "${PID}" 2>/dev/null || true
            fi

        else

            echo "Cowrie is not running."

        fi

        ;;

    restart)

        "$0" stop

        sleep 1

        exec "$0" start

        ;;

    status)

        if [ -f "${COWRIE_HOME}/run/cowrie.pid" ]; then

            PID="$(cat "${COWRIE_HOME}/run/cowrie.pid")"

            if kill -0 "${PID}" 2>/dev/null; then

                echo "Cowrie is running."
                echo "PID: ${PID}"

                exit 0

            fi

        fi

        echo "Cowrie is not running."

        exit 1

        ;;

    foreground)

        exec "${COWRIE_VENV}/bin/twistd" \
            --nodaemon \
            cowrie

        ;;

    *)

        echo
        echo "Cowrie Honeypot"
        echo
        echo "Usage:"
        echo "  cowrie start"
        echo "  cowrie stop"
        echo "  cowrie restart"
        echo "  cowrie status"
        echo "  cowrie foreground"
        echo

        exit 1

        ;;

esac

EOF

chmod 0755 "${APP_DIR}/bin/cowrie"

###############################################################################
# Create global /usr/bin/cowrie
###############################################################################

echo "[INFO] Creating /usr/bin/cowrie..."

cat > "${PKG_ROOT}/usr/bin/cowrie" <<'EOF'
#!/bin/bash

exec /var/lib/cowrie/app/bin/cowrie "$@"
EOF

chmod 0755 "${PKG_ROOT}/usr/bin/cowrie"

###############################################################################
# Create SysV init script
###############################################################################

echo "[INFO] Creating SysV init script..."

cat > "${PKG_ROOT}/etc/init.d/cowrie" <<'EOF'
#!/bin/sh

### BEGIN INIT INFO
# Provides:          cowrie
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Cowrie SSH/Telnet honeypot
### END INIT INFO

COWRIE="/usr/bin/cowrie"

case "$1" in

    start)

        su -s /bin/bash -c "${COWRIE} start" cowrie

        ;;

    stop)

        su -s /bin/bash -c "${COWRIE} stop" cowrie

        ;;

    restart)

        su -s /bin/bash -c "${COWRIE} restart" cowrie

        ;;

    status)

        su -s /bin/bash -c "${COWRIE} status" cowrie

        ;;

    *)

        echo "Usage: $0 {start|stop|restart|status}"

        exit 1

        ;;

esac

exit 0
EOF

chmod 0755 "${PKG_ROOT}/etc/init.d/cowrie"

###############################################################################
# Create systemd service
###############################################################################

echo "[INFO] Creating systemd service..."

cat > "${PKG_ROOT}/etc/systemd/system/cowrie.service" <<'EOF'
[Unit]
Description=Cowrie SSH/Telnet Honeypot
After=network.target

[Service]

Type=simple

User=cowrie
Group=cowrie

WorkingDirectory=/var/lib/cowrie

Environment=COWRIE_HOME=/var/lib/cowrie
Environment=PYTHONPATH=/var/lib/cowrie/app/src

ExecStart=/var/lib/cowrie/venv/bin/twistd --nodaemon cowrie

Restart=on-failure
RestartSec=5

UMask=0027

NoNewPrivileges=true
PrivateTmp=true

ProtectSystem=full
ProtectHome=true

ReadWritePaths=/var/lib/cowrie

[Install]
WantedBy=multi-user.target
EOF

###############################################################################
# postinst
###############################################################################

echo "[INFO] Creating post-install script..."

cat > "${DEBIAN_DIR}/postinst" <<'EOF'
#!/bin/bash

set -e

###############################################################################
# Create group
###############################################################################

if ! getent group cowrie >/dev/null 2>&1; then

    addgroup \
        --system \
        cowrie

fi

###############################################################################
# Create user
###############################################################################

if ! getent passwd cowrie >/dev/null 2>&1; then

    adduser \
        --system \
        --ingroup cowrie \
        --home /var/lib/cowrie \
        --no-create-home \
        --shell /usr/sbin/nologin \
        cowrie

fi

###############################################################################
# Runtime directories
###############################################################################

mkdir -p \
    /var/lib/cowrie \
    /var/lib/cowrie/etc \
    /var/lib/cowrie/log \
    /var/lib/cowrie/run \
    /var/lib/cowrie/lib

###############################################################################
# Ownership
###############################################################################

chown -R cowrie:cowrie \
    /var/lib/cowrie

###############################################################################
# Permissions
###############################################################################

chmod 0750 \
    /var/lib/cowrie

chmod 0750 \
    /var/lib/cowrie/etc \
    /var/lib/cowrie/log \
    /var/lib/cowrie/run

chmod 0640 \
    /var/lib/cowrie/etc/cowrie.cfg \
    2>/dev/null || true

###############################################################################
# Remove stale PID
###############################################################################

rm -f \
    /var/lib/cowrie/run/cowrie.pid

###############################################################################
# systemd
###############################################################################

if command -v systemctl >/dev/null 2>&1; then

    if [ -d /run/systemd/system ]; then

        systemctl daemon-reload || true

    fi

fi

###############################################################################
# Final message
###############################################################################

echo
echo "============================================================"
echo " Cowrie installed successfully"
echo "============================================================"
echo
echo "Application : /var/lib/cowrie/app"
echo "Python      : /var/lib/cowrie/venv"
echo "Config      : /var/lib/cowrie/etc/cowrie.cfg"
echo "Data        : /var/lib/cowrie"
echo
echo "Run foreground:"
echo
echo "  sudo -u cowrie cowrie foreground"
echo
echo "Check status:"
echo
echo "  sudo -u cowrie cowrie status"
echo
echo "Global command:"
echo
echo "  cowrie status"
echo
echo "============================================================"
echo

exit 0
EOF

chmod 0755 "${DEBIAN_DIR}/postinst"

###############################################################################
# prerm
###############################################################################

echo "[INFO] Creating pre-remove script..."

cat > "${DEBIAN_DIR}/prerm" <<'EOF'
#!/bin/bash

set -e

if [ -x /usr/bin/cowrie ]; then

    if getent passwd cowrie >/dev/null 2>&1; then

        su -s /bin/bash -c \
            "/usr/bin/cowrie stop" \
            cowrie \
            2>/dev/null || true

    fi

fi

exit 0
EOF

chmod 0755 "${DEBIAN_DIR}/prerm"

###############################################################################
# postrm
###############################################################################

cat > "${DEBIAN_DIR}/postrm" <<'EOF'
#!/bin/bash

set -e

if [ "$1" = "purge" ]; then

    if getent passwd cowrie >/dev/null 2>&1; then
        deluser --system cowrie 2>/dev/null || true
    fi

    if getent group cowrie >/dev/null 2>&1; then
        delgroup --system cowrie 2>/dev/null || true
    fi

fi

exit 0
EOF

chmod 0755 "${DEBIAN_DIR}/postrm"

###############################################################################
# Debian control file
###############################################################################

echo "[INFO] Creating Debian control file..."

cat > "${DEBIAN_DIR}/control" <<EOF
Package: cowrie
Version: ${VERSION}
Section: net
Priority: optional
Architecture: ${DEB_ARCH}
Maintainer: KYGnus
Depends: python3 (>= 3.13), adduser
Description: Cowrie SSH and Telnet honeypot
 Cowrie is a medium to high interaction SSH and Telnet honeypot
 designed to log brute-force attacks and shell interaction.
 .
 This package installs Cowrie under /var/lib/cowrie with a dedicated
 non-root service account and an isolated Python virtual environment.
EOF



###############################################################################
# Package permissions
###############################################################################

echo "[INFO] Setting package permissions..."

chmod 0755 \
    "${PKG_ROOT}/usr/bin/cowrie" \
    "${PKG_ROOT}/etc/init.d/cowrie" \
    "${DEBIAN_DIR}/postinst" \
    "${DEBIAN_DIR}/prerm" \
    "${DEBIAN_DIR}/postrm"

chmod 0755 \
    "${APP_DIR}/bin/cowrie"

# Make sure the Python entrypoints are executable.
find "${VENV_DIR}/bin" \
    -type f \
    -perm -0100 \
    -exec chmod 0755 {} \; 2>/dev/null || true



###############################################################################
# Final package content verification
###############################################################################

echo "[INFO] Checking package contents..."

REQUIRED_FILES="
usr/bin/cowrie
etc/init.d/cowrie
etc/systemd/system/cowrie.service
var/lib/cowrie/app/bin/cowrie
var/lib/cowrie/venv/bin/python
var/lib/cowrie/venv/bin/twistd
var/lib/cowrie/venv/bin/cowrie
var/lib/cowrie/etc/cowrie.cfg
"

for relative_file in ${REQUIRED_FILES}; do

    if [ ! -e "${PKG_ROOT}/${relative_file}" ]; then

        echo "[ERROR] Missing package file:"
        echo "        ${relative_file}"

        exit 1

    fi

done

echo "[OK] Package contents verified."

###############################################################################
# Verify critical shebangs
###############################################################################

echo "[INFO] Checking critical shebangs..."

TWISTD_SHEBANG="$(sed -n '1p' "${VENV_DIR}/bin/twistd")"
COWRIE_SHEBANG="$(sed -n '1p' "${VENV_DIR}/bin/cowrie")"

if [ "${TWISTD_SHEBANG}" != "#!${FINAL_VENV_PYTHON}" ]; then

    echo "[ERROR] Invalid twistd shebang:"
    echo "${TWISTD_SHEBANG}"

    exit 1

fi

if [ "${COWRIE_SHEBANG}" != "#!${FINAL_VENV_PYTHON}" ]; then

    echo "[ERROR] Invalid cowrie shebang:"
    echo "${COWRIE_SHEBANG}"

    exit 1

fi

echo "[OK] Critical shebangs verified."

###############################################################################
# Build Debian package
###############################################################################

echo "[INFO] Building Debian package..."

DEB_FILE="${DIST_DIR}/cowrie_${VERSION}_${DEB_ARCH}.deb"

dpkg-deb \
    --build \
    --root-owner-group \
    "${PKG_ROOT}" \
    "${DEB_FILE}"

###############################################################################
# Verify resulting package
###############################################################################

echo "[INFO] Verifying generated package..."

dpkg-deb \
    --info \
    "${DEB_FILE}" \
    >/dev/null

dpkg-deb \
    --contents \
    "${DEB_FILE}" \
    >/dev/null

###############################################################################
# Final output
###############################################################################

echo
echo "============================================================"
echo "                 BUILD SUCCESSFUL"
echo "============================================================"
echo
echo "Package:"
echo
echo "  ${DEB_FILE}"
echo
echo "Architecture:"
echo
echo "  ${DEB_ARCH}"
echo
echo "Install:"
echo
echo "  sudo dpkg -i ${DEB_FILE}"
echo
echo "Run foreground:"
echo
echo "  sudo -u cowrie cowrie foreground"
echo
echo "Check status:"
echo
echo "  sudo -u cowrie cowrie status"
echo
echo "Global command:"
echo
echo "  cowrie status"
echo
echo "============================================================"
echo
