Skip to content

Commit 5b0fb73

Browse files
committed
Added: Backup function to crest.prepare; make creating directory optional
1 parent 5f08268 commit 5b0fb73

1 file changed

Lines changed: 31 additions & 5 deletions

File tree

crest.prepare.sh

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ helpme ()
6666
exit 0
6767
}
6868

69+
backup_if_exists ()
70+
{
71+
local move_source="$1"
72+
# File does not exist, then everithing is fine, return with status 0
73+
[[ -f "$move_source" ]] || return 0
74+
# File exists, print a warning
75+
warning "File '$move_source' already exists."
76+
# make a backup
77+
# (test default name first, if that exists, relegate to tool)
78+
local move_target="${move_source}.bak"
79+
[[ -f "$move_target" ]] && move_target=$( mktemp "${move_target}.XXXX" )
80+
# if moving failed for whatever reason, return with status 1
81+
message "Create backup: $( mv -v -- "$move_source" "$move_target" 2>&1 )" || return 1
82+
}
83+
6984
###
7085
#
7186
# MAIN
@@ -91,7 +106,10 @@ while getopts :d:qh options ; do
91106
case $options in
92107
#hlp OPTIONS:
93108
#hlp
94-
#hlp -d <ARG> Use <ARG> as directory name to set up.
109+
#hlp -d <ARG> Use <ARG> as directory name to set up. [Default: crest]
110+
#hlp If <ARG> is '.', then skip creating a directory,
111+
#hlp instead convert a found '*.xyz' to 'coord', or
112+
#hlp rename existing 'xtbopt.coord' to 'coord'.
95113
d)
96114
crest_dir="$OPTARG"
97115
;;
@@ -115,26 +133,34 @@ while getopts :d:qh options ; do
115133
done
116134

117135
crest_dir="${crest_dir:-crest}"
118-
[[ -d "$crest_dir" ]] && fatal "Directory exists: $crest_dir"
119-
message "$( mkdir -v -- "$crest_dir" )" || fatal "Failed to create '$crest_dir'."
136+
if [[ "$crest_dir" != '.' ]] ; then
137+
[[ -d "$crest_dir" ]] && fatal "Directory exists: $crest_dir"
138+
message "$( mkdir -v -- "$crest_dir" )" || fatal "Failed to create '$crest_dir'."
139+
else
140+
debug "Target directory is '$crest_dir'."
141+
fi
120142

121143
if [[ -r "xtbopt.xyz" ]] ; then
122144
obabel_cmd="$( command -v obabel )" || fatal "Command not found: obabel"
123-
message "$( "$obabel_cmd" -ixyz "xtbopt.xyz" -oTmol -O"$crest_dir/coord" )"
145+
backup_if_exists "$crest_dir/coord"
146+
message "$( "$obabel_cmd" -ixyz "xtbopt.xyz" -oTmol -O"$crest_dir/coord" 2>&1 )" || fatal "Failure in Open Babel."
124147
elif [[ -r "xtbopt.coord" ]] ; then
125148
message "Found optimised molecular structure in turbomole format."
149+
backup_if_exists "$crest_dir/coord"
126150
message "$( cp -v -- "xtbopt.coord" "$crest_dir/coord" )"
127151
else
128152
warning "No optimised molecular structure found in current directory."
129153
for structure_file in "coord" *.xyz ; do
130154
if [[ -r "$structure_file" ]] ; then
131155
message "Will use molecular structure in '$structure_file' instead."
132156
if [[ "$structure_file" == "coord" ]] ; then
157+
[[ "$crest_dir" == '.' ]] && fatal "'$structure_file' and '$crest_dir/coord' are the same file."
133158
message "$( cp -v -- "$structure_file" "$crest_dir/coord" )"
134159
break
135160
else
136161
obabel_cmd="$( command -v obabel )" || fatal "Command not found: obabel"
137-
message "$( "$obabel_cmd" -ixyz "xtbopt.xyz" -oTmol -O"$crest_dir/coord" )"
162+
backup_if_exists "$crest_dir/coord"
163+
message "$( "$obabel_cmd" -ixyz "$structure_file" -oTmol -O"$crest_dir/coord" 2>&1 )" || fatal "Failure in Open Babel."
138164
break
139165
fi
140166
fi

0 commit comments

Comments
 (0)