-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmettalog
executable file
·75 lines (69 loc) · 1.94 KB
/
mettalog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/bash
# Restart the script with --repl if no arguments are provided.
if [ "$#" -eq 0 ]; then
exec "$0" --repl
fi
# Function to resolve path upward but stop one level higher than /usr/
function resolve_upward {
local current_dir="$1"
while [ "$current_dir" != "/" ]; do
if [ "$current_dir" == "/tmp" ]; then
echo "/tmp"
return
fi
if [ "$current_dir" == "/usr" ]; then
echo "/usr"
return
fi
if [ -d "$current_dir/$2" ]; then
echo "$current_dir/$2"
return
fi
current_dir=$(dirname "$current_dir")
done
}
# Check if we are inside a Docker container
if [ -f /.dockerenv ]; then
# Check if the first argument is a directory
if [ -d "$1" ]; then
# Change directory to the first argument, if it's a directory
cd "$1"
shift 1
else
# Otherwise, change directory to the specified path inside the host's root
resolved_path=$(resolve_upward "/host/root" "$1")
if [ -n "$resolved_path" ]; then
cd "$resolved_path"
shift 1
fi
fi
# Execute the rest of the arguments
exec "$@"
else
# Not inside a Docker container
# Check if the Docker image named 'mettalog' already exists
if docker image inspect mettalog &>/dev/null; then
# Build the Docker image named 'mettalog'
# Store the output in a temporary file
temp_file=$(mktemp)
if ! docker build -t mettalog . > "$temp_file" 2>&1; then
echo "Docker build failed. Output:"
cat "$temp_file"
rm "$temp_file"
exit 1
fi
rm "$temp_file"
else
docker build -t mettalog .
fi
UPWARD=$(resolve_upward "$(pwd)")
# Run the Docker container with necessary volumes mounted
# Note: It's crucial to quote '$(pwd)' to handle spaces in the path
docker run --rm -it \
-v "${UPWARD}:${UPWARD}" \
-v "$(pwd)":/host/cwd \
-v /:/host/root \
-w "$(pwd)" \
mettalog \
/home/user/vspace-metta/mettalog /home/user/vspace-metta/MeTTa --compatio=true "$@"
fi