Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/jparser
blob: 1090e0f40f0e78aa9fd183238d2905a001686d34 (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
#!/bin/bash

# Features:
#
# output format will be as defined by the variable $template
# for exemple
# org.laptop.WebActivity:1305831706:b7b8928d-b203-4aea-8924-f5a070637282:OLPC:0:
# in a file named like user_YYYY-MM-DD.txt
# blank line separate new record (erase the file manually if you don't like this feature)

# This script is under the GPLv2 license. If you get this file without the license, please read it there:
# http://www.gnu.org/licenses/gpl-2.0.html


metadata=.metadata
path=$PWD
callme=child
separator=:
template=#activity${separator}timestamp${separator}uid${separator}title${separator}title_set_by_user${separator}buddies
template_simple=#date${separator}uid${separator}title${separator}buddies
simple_suffix=_simple.txt
user=$USER
format=-c

helpme()
{
  echo "Usage: $0 [output_format] [user_name] [path to $metadata files]."
  echo "       add  -h as first arg to print this help" 
  echo "       - file(s) output format could be -s to create a simplified output"
  echo "                                        -c to create the complet output (Default)"
  echo "                                        -f to create both files"
  echo "       - if no path specified, the current dir would be used"
  echo '       - if no user name is specified, the current $USER would be used' #should check an XO id
  exit 0
}

#
# Fuction used to get the value from the key pairs
#

get_sed()
{
  echo $1 | sed "s/.*://"
}


#
# function used to parse all metadata files from $path
# parameters are: 
#   parse filename output name output_format
#

parse()
{
  out=$2
  tmp=$(sed -e 's/": ["]/:/g;s/["], "/#/g;s/^{"//g;s/: ""}//g;s/": /:/g;s/, "/#/g;s/"}//g' $1)

  IFS='#' read -ra ADDR <<< "$tmp"
  for i in "${ADDR[@]}"; do 
    if [[ $i == *epub* ]]; then
      activity=epub   # litle trick cause ebooks as epub have no activity saved
    fi
    case $i in       # Because not all metadafiles use the same template, we have to look for each part one by one.
        *title_set*) title_set=`get_sed $i`;;
        *title:*)    title=`get_sed $i`;;
        *uid*)       uid=`get_sed $i`;;
        *timestamp*) timestamp=`get_sed $i`;;
        *activity:*) activity=`get_sed $i`;;
        *buddies*)   buddies=`get_sed $i`;;
    esac
  done

  case $3 in
    -s)  #simplified output
         line_so=true 
         ;; 
    -f)  # both output
         line_so=true
         line=true
         ;;
    *)   # only complet output
         line=true
         ;;
  esac
 
  if [[ -n "$line_so" ]]; then
    echo `date -d @${timestamp} +%d/%m/%Y`${separator}${uid}${separator}${title}${separator}${buddies} >> ${out}${simple_suffix}
  fi
  if [[ -n "$line" ]]; then
    echo ${activity}${separator}${timestamp}${separator}${uid}${separator}${title}${separator}${title_set}${separator}${buddies} >> $out
  fi

  exit 0
}

#
# Start argument processing
#

if [[ "$#" -gt 4 ]]; then
  helpme
fi

if [[ -n "$1" ]]; then
  if [[ "$1" = "-h" ]]; then
    helpme
  elif [[ "$1" = "$callme" ]]; then # Am I a child process used for parsing?
    if [ ! "$#" = 4 ]; then
      echo "Error there should have been 4 args: callme path filename format"
      echo "  But you give me: $*"
      exit 1
    else parse $2 $3 $4
    fi
  elif [[ "$1" = "-s" || "$1" = "-f" || "$1" = "-c" ]]; then # Format output provided as arg?
    format=$1
    if [[ -n "$2" ]]; then             # User provided as arg?
        user=$2 
      if [ $# -lt 4 ]; then            # Path provided as arg?
         path=$3
      fi
    elif [ -e /ofw/serial-number ]; then # if we are from an XO, grad its serial 
        user=`cat /ofw/serial-number`
    fi # default value is $USER
  else
    echo "Error, wrong output specified"
    helpme
  fi
fi

#
# Let's start parsing
#

out=${user}_`date +%F`.txt

# create template files
if [[ "$1" = "-s" || "$1" = "-f" ]]; then
  if [ ! -f ${out}${simple_suffix} ]; then
    echo $template_simple > ${out}${simple_suffix}
  else
    echo >>  ${out}${simple_suffix}
  fi
elif [[ "$1" = "-c" || "$1" = "-f" ]]; then
  if [ ! -f $out ]; then
    echo $template > $out
  else 
    echo >> $out
  fi
fi

# Now we call ourself to be able to inter in the function. Also used to run in concurrency
find $path -name "*$metadata" -exec $0 $callme '{}' $out $format \;&
wait  # wait the end of parsing
case $format in
  -s)  #simplified output
       echo "Simple output file could be find in $out$simple_suffix"
       ;;
  -f)  # both output
       echo "Metadata could be find in $out" 
       echo "Simple output file could be find in $out$simple_suffix"
       ;;
  *)   # only complet output
       echo "Metadata could be find in $out" 
       ;;
esac
exit 0