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?
552if ! [ -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
1865fi
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
2173if [ -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
2787fi
2888
2989# Check SSH access to GitHub
3898use_ssh=false
3999
40100# Download
41- root=$( dirname $( realpath $0 ) )
42- root=$( realpath " $root /.." )
43- socpath=" $root /socrates"
101+ mkdir -p " $( dirname " $socpath " ) "
44102rm -rf " $socpath "
45103if [ " $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
61119radlib=" $socpath /bin/radlib.a"
@@ -67,10 +125,34 @@ else
67125 exit 1
68126fi
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
72138echo " You must now run the following command:"
73139echo " 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+
76158exit 0
0 commit comments