aboutsummaryrefslogtreecommitdiffstats
path: root/bin/functions.sh
blob: 951def5a2051a919541ee45e20a483dd321495b3 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
# some functions

# zsh color output
# echo "$FG[46]$FX[blink]Hello, World"

function echo_green() {
  echo -e '\E[1;32m'$*'\E[0m'
}

function echo_red() {
  echo -e '\E[1;31m'$*'\E[0m'
}

function echo_magenta() {
  echo -e '\E[1;35m'$*'\E[0m'
}

function echo_cyan() {
   echo -e '\E[1;36m'$*'\E[0m'
}

# log text ...
function log() {
  date=`date "+%Y-%m-%d %H:%M:%S"`
  if [ -n $VERBOSE ]
  then
    echo $date $@ | tee -a $LOGFILE
  fi
  [ -n $LOGFILE ] && echo $date $@ >> $LOGFILE
}

function log_green() {
  date=`date "+%Y-%m-%d %H:%M:%S"`
  if [ -n $VERBOSE ]
  then
    echo_green $date $@
  fi
  [ -n $LOGFILE ] && echo $date $@ >> $LOGFILE
}

function log_red() {
  date=`date "+%Y-%m-%d %H:%M:%S"`
  if [ -n $VERBOSE ]
  then
    echo_red $date $@
  fi
  [ -n $LOGFILE ] && echo $date $@ >> $LOGFILE
}

function log_magenta() {
  date=`date "+%Y-%m-%d %H:%M:%S"`
  if [ -n $VERBOSE ]
  then
    echo_magenta $date $@
  fi
  [ -n $LOGFILE ] && echo $date $@ >> $LOGFILE
}

function log_cyan() {
  date=`date "+%Y-%m-%d %H:%M:%S"`
  if [ -n $VERBOSE ]
  then
    echo_cyan $date $@
  fi
  [ -n $LOGFILE ] && echo $date $@ >> $LOGFILE
}

function notify() {
  title="$1"
  text="$2"
  echo 'naughty.notify({title = "'$title'", text = "'$text'"})' | awesome-client
}

# mute command
function mute() {
  $@ > /dev/null 2> /dev/null
}

# run pidfile command
function run() {
  pidfile=$1
  shift
  command=$@

  exec $command > /dev/null 2> /dev/null &
  pid=$!
  sleep 0.25
  npid=$(pidof -s $1)
  if [ -n "$npid" ] && [ $npid -ne $pid ]
  then
    pid=$npid
  fi
  echo $pid > $pidfile
  log_green executed $command"\n"
}

# run_once $pidfile $command[]
function run_once() {
  pidfile=/tmp/run/lock/$1
  shift
  command=$@

  mkdir -p $(dirname $pidfile)

  if [ -f $pidfile ];
  then
    pid=`head -n 1 $pidfile`
    running=$(ps -u $(whoami) -p $pid | grep '^[ ]*'$pid)
    if [ -z "$running" ]
    then
      log_cyan stale pidfile found
      rm -f $pidfile

      npid=$(pidof -s $1)
      if [ -n "$npid" ]
      then
        log_green "pidfile updated\n"
        echo $npid > $pidfile
      else
        run $pidfile $command
      fi

    else
      log_cyan "active pidfile found\n"
    fi
  else
    pid=`ps | grep "$command" | grep -v grep | head -n 1 | awk '{ print $1 }'`
    if [ -z "$pid" ]
    then
      log_green not running
      run $pidfile $command
    else
      log_cyan "pidfile updated\n"
      echo $pid > $pidfile
    fi
  fi
}

# run_auto $file
function run_auto() {
  file=$1
  . $file

  cur_host=`hostname`
  case $cur_host in
    annoyance|silence|remembrance)
      cur_area=private
      ;;
    c*|s*)
      cur_area=unixpool
      ;;
  esac

  if [ $area != $cur_area -a $area != "any" ]
  then
    log_magenta invalid_area $(basename $file)"\n"
    return
  fi

  if [ $host != $cur_host -a $host != "any" ]
  then
    log_magenta invalid_host $(basename $file)"\n"
    return
  fi

  if [ $allow_multiple -eq 1 ]
  then
    log_green run $(basename $file)
    run /dev/null $command
  else
    log_green run_once $(basename $file)
    run_once `basename $file` $command
  fi
}