aboutsummaryrefslogtreecommitdiff
path: root/timelog
blob: 66541df0e1f2cb52b583b8faef9ec072099d8852 (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
#!/bin/bash
#
# Worklog wrapper
#
# Alternatives to current worklog backend:
#
# https://github.com/distilledhype/node-worklog
# https://github.com/snaptortoise/worklog
# https://github.com/tormaroe/worklog
# https://github.com/winged/worklog
# https://github.com/yaccz/worklog

BASE="$HOME/file"
CODE="$HOME/code"
GROUP="$1"
ACTION="$2"
BASENAME="`basename $0`"
EDITOR=${EDITOR:=vi}

# Set window title
# http://stackoverflow.com/questions/899609/gnu-screen-run-script-that-sends-commands-to-the-screen-session-it-is-being-run
function timelog_window_title {
  if [ -n "$STY" ]; then
    screen -p $WINDOW -X title "$*"
  else
    if which xtitle &> /dev/null; then
      xtitle "$*"
    fi
  fi
}

# Setup a new worklog configuration
function timelog_setup {
  cat > $BASE/$GROUP/worklog/projects <<EOF
# Worklog project file
# note that projects appear in Worklog in REVERSE order

#H:Hosting
#I:Infrastructure
#O:Organization
EOF

  echo "First run, you should edit your project list..."
  $EDITOR $BASE/$GROUP/worklog/projects
  ( cd $BASE/$GROUP/worklog && worklog )
}

# Loop over available worklogs
function timelog_menu {
  # Basic variables
  count=0
  bases=(`find "$BASE" -maxdepth 2 -name worklog -exec dirname {} \;`)
  codes=(`find "$CODE" -maxdepth 2 -name worklog -exec dirname {} \;`)
  candidates=(${bases[@]} ${codes[@]})

  # Clear the screen everytime we enter the menu
  clear

  # Draw menu and parse options
  if [ ! -z "$candidates" ]; then
    echo "Please select one of the following projects:"
    echo ""

    for candidate in ${candidates[@]}; do
      echo -e "\t[$count] `basename $candidate`"
      let count++
    done

    echo ""
    read -p "Enter option (r to refresh, Ctrl-C or q to quit): " option

    if [ "$option" == "q" ]; then
      exit
    elif [ "$option" == "r" ]; then
      return
    elif [[ "$option" =~ ^[0-9]+$ ]] && [ ! -z "${candidates[$option]}" ]; then
      project="`basename ${candidates[$option]}`"
      timelog $project
    else
      echo "Invalid option"
      exit 1
    fi
  else
    echo "Usage: $BASENAME [group] [edit]"
    exit 1
  fi
}

# Run worklog for a given project
function timelog_run {
  # Set window title
  timelog_window_title "log: $GROUP"

  # Set base folder
  if [ -d "$CODE/$GROUP" ] && [ ! -d "$BASE/$GROUP" ]; then
    BASE="$CODE"
  fi

  # Ensure folder exist
  mkdir -p $BASE/$GROUP/worklog

  # Run
  if [ ! -e "$BASE/$GROUP/worklog/projects" ]; then
    timelog_setup
  elif [ "$ACTION" == "edit" ]; then
    $EDITOR $BASE/$GROUP/worklog/projects
  else
    ( cd $BASE/$GROUP/worklog && worklog )
  fi
}

# Main
if [ ! -z "$GROUP" ]; then
  timelog_run
else
  while true; do
    # Set window title
    timelog_window_title "log"

    timelog_menu
  done
fi

# Restore screen title
timelog_window_title terminal