Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial commit. doesn't do much

  • Loading branch information...
commit 16f4b781409e4736226ed3d7aa0bebc6505cf07e 0 parents
Dave Reisner authored July 01, 2010

Showing 1 changed file with 206 additions and 0 deletions. Show diff stats Hide diff stats

  1. 206  clockwerk
206  clockwerk
... ...
@@ -0,0 +1,206 @@
  1
+#!/bin/bash
  2
+
  3
+usage() {
  4
+  cat << EOF
  5
+ clockwerk <action> [arguments]
  6
+
  7
+ actions: job
  8
+          category
  9
+          report
  10
+
  11
+ arguments: job start <category> [comment]
  12
+                stop
  13
+                delete <jobid>
  14
+
  15
+            category add <name> [description]
  16
+                     edit <name> <newname> [description]
  17
+                     delete <name>
  18
+
  19
+            report [from] [to]
  20
+
  21
+EOF
  22
+
  23
+  exit
  24
+}
  25
+
  26
+CW_DATADIR="${XDG_DATA_HOME:-$HOME/.local/share}/clockwerk"
  27
+DBFILE="${CW_DATADIR}/clockwerk.db"
  28
+LOCKFILE="${CW_DATADIR}/clockwerk.lock"
  29
+
  30
+debug() {
  31
+  local mesg=$1; shift
  32
+  printf "%s\n" "$mesg"
  33
+}
  34
+
  35
+die() {
  36
+  :
  37
+}
  38
+
  39
+warn() {
  40
+  :
  41
+}
  42
+
  43
+info() {
  44
+  :
  45
+}
  46
+
  47
+initialize_db() {
  48
+  local sql="CREATE TABLE job(j_id INTEGER PRIMARY KEY AUTOINCREMENT,
  49
+                              j_category TEXT NOT NULL,
  50
+                              j_start INTEGER NOT NULL,
  51
+                              j_stop INTEGER DEFAULT 0,
  52
+                              j_comment NULL,
  53
+                              FOREIGN KEY (j_category) REFERENCES category (c_name));
  54
+             CREATE TABLE category(c_name TEXT PRIMARY KEY,
  55
+                                   c_desc TEXT);"
  56
+
  57
+  exec_query "$sql"
  58
+}
  59
+
  60
+exec_query() {
  61
+  sqlite3 "$DBFILE" "$@"
  62
+}
  63
+
  64
+# returns 0 on success
  65
+# returns non-zero on failure
  66
+lock_acquire() {
  67
+  debug "Acquiring lock"
  68
+  [[ -f $LOCKFILE ]] && return 1
  69
+  touch "$LOCKFILE" && return 0 || return 1
  70
+}
  71
+
  72
+# returns nonzero (job_id) on success
  73
+# returns 0 on failure
  74
+lock_release() {
  75
+  debug "Releasing lock"
  76
+  [[ ! -f $LOCKFILE ]] && return 0
  77
+  local job_id=$(< "$LOCKFILE")
  78
+  rm "$LOCKFILE" || return 0
  79
+  return $job_id
  80
+}
  81
+
  82
+# return 0 on success
  83
+# return 1 on failure to acquire lock
  84
+# return 2 on sql error
  85
+job_start() {
  86
+  lock_acquire || return 1
  87
+  local jobstart=$(date +%s)
  88
+  local category=$1
  89
+  local comment=$2
  90
+
  91
+  # insert into table
  92
+  sql="INSERT INTO job(job_category, job_start, job_comment)
  93
+                VALUES('$category', $jobstart, '$comment');"
  94
+
  95
+  exec_query "$sql" || return 2
  96
+  local jobid=$(exec_query 'select max(job_id) from jobs;')
  97
+
  98
+  echo "$jobid $jobstart" > "$LOCKFILE"
  99
+}
  100
+
  101
+# return 0 on success
  102
+# return 1 on no job recorded (too short)
  103
+# return 2 on error
  104
+job_stop() {
  105
+  local jobid jobstart jobstop=$(date +%s)
  106
+  read jobid jobstart <<< "$(lock_release)"
  107
+
  108
+  if [[ $(get_duration $jobstart $jobstop) -gt 60 ]]; then
  109
+    job_delete $job_id
  110
+    return 1
  111
+  fi
  112
+
  113
+  local sql="UPDATE job SET job_stop = '$jobstart' WHERE job_id = '$jobid';"
  114
+
  115
+  exec_query "$sql" &>/dev/null
  116
+
  117
+  [[ $jobid -eq 0 ]] && return 2
  118
+}
  119
+
  120
+# returns duration in seconds
  121
+# if only 1 arg, end time is assumed to be now
  122
+get_duration() {
  123
+  local start=$1 end dur
  124
+  [[ -n $2 ]] && end=$2 || end=$(date +%s)
  125
+
  126
+  dur=$(( $end - $start ))
  127
+
  128
+  [[ $dur -lt 0 ]] && return 0 || return $dur
  129
+}
  130
+
  131
+date_to_seconds() {
  132
+  local seconds=$(date --date="$1" +%s || echo "0")
  133
+  return seconds
  134
+}
  135
+
  136
+seconds_to_hms() {
  137
+  local seconds=$1 hours=0 minutes=0
  138
+
  139
+  while (( seconds >= 3600 )); do
  140
+    (( ++hours ))
  141
+    (( seconds -= 3600 ))
  142
+  done
  143
+
  144
+  while (( seconds >= 60 )); do
  145
+    (( ++minutes ))
  146
+    (( seconds -= 60 ))
  147
+  done
  148
+
  149
+  echo $hours $minutes $seconds
  150
+}
  151
+
  152
+# return 0 on success
  153
+# return 1 on category exists
  154
+# return 2 on sql error
  155
+category_add() {
  156
+  sql="INSERT INTO category(c_name, c_desc) VALUES('$1', '$2');"
  157
+  local result=$(exec_query "$sql" 2>&1)
  158
+
  159
+  [[ $result = "Error: column c_name is not unique" ]] && return 1
  160
+  [[ $result =~ ^Error* ]] && return 2
  161
+
  162
+  return 0
  163
+}
  164
+
  165
+category_del() {
  166
+  :
  167
+}
  168
+
  169
+do_job() {
  170
+  local action=$1; shift
  171
+  case $action in
  172
+    "start")
  173
+      [[ $# -gt 0 ]] && job_start "$@" || usage
  174
+      ;;
  175
+    "stop")
  176
+      job_stop
  177
+      ;;
  178
+    *) usage
  179
+      ;;
  180
+  esac
  181
+}
  182
+
  183
+do_category() {
  184
+  local action=$1; shift
  185
+}
  186
+
  187
+do_report() {
  188
+  local action=$1; shift
  189
+}
  190
+
  191
+#sanity checks
  192
+[[ ! -d $CW_DATADIR ]] && mkdir -p "$CW_DATADIR"
  193
+exec_query 'SELECT * FROM job' &>/dev/null || initialize_db
  194
+
  195
+# option parsing
  196
+action=$1; shift
  197
+
  198
+[[ $# -eq 0 ]] && usage
  199
+
  200
+case $action in
  201
+  "job") do_job "$@" ;;
  202
+  "category") do_category "$@" ;;
  203
+  "report") do_report "$@" ;;
  204
+  *) usage ;;
  205
+esac
  206
+

0 notes on commit 16f4b78

Please sign in to comment.
Something went wrong with that request. Please try again.