#!/bin/bash

# Dynamic Scan for Machine Learning Models version 0.1.
#
# Copyright (c) 2025 Oracle, Inc.
# Licensed under the Universal Permissive License v 1.0 as shown at 
# https://oss.oracle.com/licenses/upl/

# Check if running as root
if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

# Disable network
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

# Allow loopback
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

# Switch to the non-root user and source .bashrc
# Use a default command (/bin/bash) if no arguments are passed
# echo "Number of parameters: $#"
if [ -z "$1" ]; then
    chown -R sandboxuser:sandboxuser /tmp_*
    exec su - sandboxuser -c "export AUTH_TOKEN=\"${AUTH_TOKEN}\" PATH=\"/usr/local/cuda/bin:${PATH}\" LD_LIBRARY_PATH=\"/usr/local/cuda/lib64:${LD_LIBRARY_PATH}\" MODEL_TO_SCAN=\"${MODEL_TO_SCAN}\" && exec /share/scan.sh"
else
    chown -R sandboxuser:sandboxuser /tmp_*
    exec su - sandboxuser -c "export AUTH_TOKEN=\"${AUTH_TOKEN}\" PATH=\"/usr/local/cuda/bin:${PATH}\" LD_LIBRARY_PATH=\"/usr/local/cuda/lib64:${LD_LIBRARY_PATH}\" MODEL_TO_SCAN=\"${MODEL_TO_SCAN}\" && exec /share/scan.sh && exec \"$@\""
fi

# tail -f /dev/null
