1#!/usr/local/bin/bash
2#
3# Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18if [ "$1" = "--help" ]; then
19cat <<EOF
20${0##*/}, version %version%
21postadd plot script, send a certain plot script to gnuplot
22
23Usage: ${0##*/}
24
25Used external packages: gnuplot
26EOF
27   exit 0
28fi
29
30gnuplot=$(csg_get_property cg.inverse.gnuplot.bin)
31[ -n "$(type -p $gnuplot)" ] || die "${0##*/}: gnuplot binary '$gnuplot' not found"
32
33opts=$(csg_get_interaction_property --allow-empty inverse.post_add_options.plot.gnuplot_opts)
34
35script=$(csg_get_interaction_property inverse.post_add_options.plot.script)
36[[ -f $(get_main_dir)/$script ]] || die "${0##*/}: plot script '$script' is not in maindir"
37
38what_to_kill="$(csg_get_interaction_property --allow-empty inverse.post_add_options.plot.kill)"
39
40msg "Plotting '$script' using $gnuplot"
41if [[ -z ${what_to_kill} ]]; then
42  [[ -z $(type -p flock) ]] && die "${0##*/}: could not find flock needed by gnuplot pipe"
43  [[ -z $(type -p mkfifo) ]] && die "${0##*/}: could not find mkfifo needed by gnuplot pipe"
44  (
45    flock -n 7 || exit 0 #see flock man page to understand this trick
46    cd $(get_main_dir)
47    rm -rf gnuplot_pipe gnuplot_pipe.log
48    msg "Creating gnuplot_pipe ..."
49    mkfifo gnuplot_pipe
50    while true; do
51      if read <gnuplot_pipe; then
52        echo -e "$REPLY"
53        echo -e "$REPLY" >> gnuplot_pipe.log
54        [[ $REPLY = "exit" ]] && break
55      fi
56    done | $gnuplot ${CSG_RUNTEST:+-d} $opts &
57    while true; do
58      if [[ -z $(ps -o pid= -p "${CSG_MASTER_PID}") ]]; then
59	echo "exit" > $(get_main_dir)/gnuplot_pipe
60	rm -rf gnuplot_pipe gnuplot_pipe.log gnuplot_pipe.lock
61        exit
62      fi
63      sleep 1 #lowers the load
64    done &
65    sleep 1 #wait for gnuplot_pipe
66    cd - > /dev/null
67  ) 7> $(get_main_dir)/gnuplot_pipe.lock
68
69  #gnuplot is in laststep, move to current one
70  echo "cd '$PWD'" > $(get_main_dir)/gnuplot_pipe || die "piping to gnuplot_pipe failed"
71
72  #name pipe accept only one command at the time, for i in $(cat ); do echo $i > pipe; done would do the same
73  echo "load '$(get_main_dir)/$script'" > $(get_main_dir)/gnuplot_pipe || die "piping to gnuplot_pipe failed"
74else
75  [[ -z $(type -p killall) ]] && die "${0##*/}: could not find killall needed to kill gnuplot"
76  killall $what_to_kill
77  $gnuplot ${CSG_RUNTEST:+-d} $opts "$(get_main_dir)/$script" || true #exit code not always clear
78fi
79