Skip to content

Commit 59b303a

Browse files
committed
Automatically set shell rc files, but warn user if duplicate variable exports involved.
1 parent ffaa73d commit 59b303a

4 files changed

Lines changed: 202 additions & 22 deletions

File tree

.github/workflows/install_and_test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
cd ..
6363
6464
- name: Get FastChem
65-
run: ./src/get_fastchem.sh
65+
run: ./src/get_fastchem.sh -y
6666

6767
- name: Build AGNI
6868
run: |

src/get_data.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function zenodo {
124124
# try again at downloading the file?
125125
echo "Trying again to download the file"
126126
sleep 1
127-
wget --user-agent $ua-qO $tgt $url
127+
wget --user-agent $ua -qO $tgt $url
128128

129129
# check if command failed or if file does not exist
130130
if [ $? -ne 0 ]; then

src/get_fastchem.sh

Lines changed: 105 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,85 @@ tag="v3.1.3"
55

66
set -e
77

8-
root=$(dirname $(realpath $0))
8+
auto_yes=false
9+
while getopts ":y" opt; do
10+
case "$opt" in
11+
y) auto_yes=true ;;
12+
*)
13+
echo "Usage: $0 [-y]" >&2
14+
exit 1
15+
;;
16+
esac
17+
done
18+
shift $((OPTIND - 1))
19+
20+
if [ $# -ne 0 ]; then
21+
echo "Usage: $0 [-y]" >&2
22+
exit 1
23+
fi
24+
25+
confirm_yes() {
26+
local prompt="$1"
27+
if [ "$auto_yes" = true ]; then
28+
echo "$prompt [y/N] y (-y)"
29+
return 0
30+
fi
31+
32+
read -r -p "$prompt [y/N] " choice
33+
case "$choice" in
34+
[Yy]|[Yy][Ee][Ss]) return 0 ;;
35+
*) return 1 ;;
36+
esac
37+
}
38+
39+
warn_duplicate_exports() {
40+
local var_name="$1"
41+
local shell_rc="$2"
42+
local export_count
43+
44+
if [ ! -f "$shell_rc" ]; then
45+
return
46+
fi
47+
48+
export_count=$(grep -Ec "^[[:space:]]*export[[:space:]]+${var_name}=" "$shell_rc")
49+
if [ "$export_count" -gt 1 ]; then
50+
echo "WARNING: Found ${export_count} export entries for ${var_name} in '$shell_rc'."
51+
echo " Consider keeping only one to avoid ambiguity."
52+
fi
53+
}
54+
55+
detect_shell_rc() {
56+
case "$(basename "${SHELL:-}")" in
57+
bash) echo "$HOME/.bashrc" ;;
58+
zsh) echo "$HOME/.zshrc" ;;
59+
ksh) echo "$HOME/.kshrc" ;;
60+
*) echo "$HOME/.profile" ;;
61+
esac
62+
}
63+
64+
root=$(dirname "$(realpath "$0")")
965
root=$(realpath "$root/..")
1066

67+
# Installation path
68+
default_fcpath="$root/fastchem"
69+
if [ -n "$FC_DIR" ]; then
70+
fcpath="$FC_DIR"
71+
echo "FC_DIR is already set: '$fcpath'"
72+
if ! confirm_yes "Install FastChem there and overwrite existing contents if needed?"; then
73+
echo "Exiting without changes."
74+
exit 0
75+
fi
76+
else
77+
fcpath="$default_fcpath"
78+
fi
79+
80+
if [ -z "$fcpath" ] || [ "$fcpath" = "/" ]; then
81+
echo "ERROR: Invalid installation path: '$fcpath'" >&2
82+
exit 1
83+
fi
84+
1185
# Download via HTTPS only
12-
fcpath="$root/fastchem"
86+
mkdir -p "$(dirname "$fcpath")"
1387
rm -rf "$fcpath"
1488

1589
git clone --depth 1 --branch "$tag" https://github.com/NewStrangeWorlds/FastChem.git "$fcpath"
@@ -22,10 +96,34 @@ cd build
2296
cmake ".."
2397
make
2498

25-
cd $root
26-
export FC_DIR=$fcpath
99+
cd "$root"
100+
export FC_DIR="$fcpath"
101+
102+
# Check fastchem executable exists
103+
fcexec="$fcpath/fastchem"
104+
if [ -f "$fcexec" ]; then
105+
echo "FastChem has been installed"
106+
echo ""
107+
else
108+
echo "Could not find FastChem executable - failed to compile"
109+
exit 1
110+
fi
111+
112+
rcfile="$(detect_shell_rc)"
113+
if confirm_yes "Add FC_DIR to '$rcfile'?"; then
114+
export_line="export FC_DIR='$fcpath'"
115+
touch "$rcfile"
116+
{
117+
echo ""
118+
echo "# FastChem installation"
119+
echo "$export_line"
120+
} >> "$rcfile"
121+
echo "Added FC_DIR to '$rcfile'."
122+
echo "Please restart your terminal or run 'source $rcfile' to apply the changes."
123+
echo " "
124+
else
125+
echo "Skipped editing shell rc file."
126+
fi
127+
warn_duplicate_exports "FC_DIR" "$rcfile"
27128

28-
echo "FastChem has been installed"
29-
echo "It is recommended that you add the following line to your shell rc file"
30-
echo "export FC_DIR='$fcpath'"
31129
exit 0

src/get_socrates.sh

Lines changed: 95 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,53 @@
11
#!/bin/bash
22
# Download and compile socrates
33

4+
auto_yes=false
5+
while getopts ":y" opt; do
6+
case "$opt" in
7+
y) auto_yes=true ;;
8+
*)
9+
echo "Usage: $0 [-y]" >&2
10+
exit 1
11+
;;
12+
esac
13+
done
14+
shift $((OPTIND - 1))
15+
16+
if [ $# -ne 0 ]; then
17+
echo "Usage: $0 [-y]" >&2
18+
exit 1
19+
fi
20+
21+
confirm_yes() {
22+
local prompt="$1"
23+
if [ "$auto_yes" = true ]; then
24+
echo "$prompt [y/N] y (-y)"
25+
return 0
26+
fi
27+
28+
read -r -p "$prompt [y/N] " choice
29+
case "$choice" in
30+
[Yy]|[Yy][Ee][Ss]) return 0 ;;
31+
*) return 1 ;;
32+
esac
33+
}
34+
35+
warn_duplicate_exports() {
36+
local var_name="$1"
37+
local shell_rc="$2"
38+
local export_count
39+
40+
if [ ! -f "$shell_rc" ]; then
41+
return
42+
fi
43+
44+
export_count=$(grep -Ec "^[[:space:]]*export[[:space:]]+${var_name}=" "$shell_rc")
45+
if [ "$export_count" -gt 1 ]; then
46+
echo "WARNING: Found ${export_count} export entries for ${var_name} in '$shell_rc'."
47+
echo " Consider keeping only one to avoid ambiguity."
48+
fi
49+
}
50+
451
# Do we have NetCDF?
552
if ! [ -x "$(command -v nc-config)" ]; then
653
echo 'ERROR: NetCDF is not installed.' >&2
@@ -17,13 +64,26 @@ if ! [ -x "$(command -v gfortran)" ]; then
1764
exit 1
1865
fi
1966

20-
# Already setup?
67+
# Root paths
68+
root=$(dirname "$(realpath "$0")")
69+
root=$(realpath "$root/..")
70+
default_socpath="$root/socrates"
71+
72+
# Installation path
2173
if [ -n "$RAD_DIR" ]; then
22-
echo "WARNING: You already have SOCRATES installed"
23-
echo " RAD_DIR=$RAD_DIR"
24-
echo "Reinstalling SOCRATES..."
25-
echo ""
26-
sleep 5
74+
socpath="$RAD_DIR"
75+
echo "RAD_DIR is already set: '$socpath'"
76+
if ! confirm_yes "Install SOCRATES there and overwrite existing contents if needed?"; then
77+
echo "Exiting without changes."
78+
exit 0
79+
fi
80+
else
81+
socpath="$default_socpath"
82+
fi
83+
84+
if [ -z "$socpath" ] || [ "$socpath" = "/" ]; then
85+
echo "ERROR: Invalid installation path: '$socpath'" >&2
86+
exit 1
2787
fi
2888

2989
# Check SSH access to GitHub
@@ -38,9 +98,7 @@ fi
3898
use_ssh=false
3999

40100
# Download
41-
root=$(dirname $(realpath $0))
42-
root=$(realpath "$root/..")
43-
socpath="$root/socrates"
101+
mkdir -p "$(dirname "$socpath")"
44102
rm -rf "$socpath"
45103
if [ "$use_ssh" = true ]; then
46104
git clone git@github.com:FormingWorlds/SOCRATES.git "$socpath"
@@ -54,8 +112,8 @@ cd "$socpath"
54112
./build_code
55113

56114
# Environment
57-
export RAD_DIR=$socpath
58-
cd $root
115+
export RAD_DIR="$socpath"
116+
cd "$root"
59117

60118
# Check radlib exists
61119
radlib="$socpath/bin/radlib.a"
@@ -67,10 +125,34 @@ else
67125
exit 1
68126
fi
69127

128+
detect_shell_rc() {
129+
case "$(basename "${SHELL:-}")" in
130+
bash) echo "$HOME/.bashrc" ;;
131+
zsh) echo "$HOME/.zshrc" ;;
132+
ksh) echo "$HOME/.kshrc" ;;
133+
*) echo "$HOME/.profile" ;;
134+
esac
135+
}
70136

71137
# Inform user
72138
echo "You must now run the following command:"
73139
echo " export RAD_DIR='$socpath'"
74-
echo " "
75-
echo "You should also add this command to your shell rc file (e.g. ~/.bashrc)"
140+
141+
rcfile="$(detect_shell_rc)"
142+
if confirm_yes "Add RAD_DIR to '$rcfile'?"; then
143+
export_line="export RAD_DIR='$socpath'"
144+
touch "$rcfile"
145+
{
146+
echo ""
147+
echo "# SOCRATES installation"
148+
echo "$export_line"
149+
} >> "$rcfile"
150+
echo "Added RAD_DIR to '$rcfile'."
151+
echo "Please restart your terminal or run 'source $rcfile' to apply the changes."
152+
echo " "
153+
else
154+
echo "Skipped editing shell rc file."
155+
fi
156+
warn_duplicate_exports "RAD_DIR" "$rcfile"
157+
76158
exit 0

0 commit comments

Comments
 (0)