#!/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 dest=./ helpme() { echo "Usage: $0 [output_format] [user_name] [path to $metadata files] [destination path]" 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 echo ' - if no detination path is specified, $PWD is used' 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() { 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} >> ${2}${simple_suffix} fi if [[ -n "$line" ]]; then echo ${activity}${separator}${timestamp}${separator}${uid}${separator}${title}${separator}${title_set}${separator}${buddies} >> $2 fi exit 0 } # # Start argument processing # if [[ "$#" -gt 5 ]]; 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 [[ -n "$3" ]]; then # Path provided as arg? path=$3 if [ ! -d "$path" ]; then echo "Error, path providen is incorrect (check permissions)" exit 1 fi if [[ -n "$4" ]]; then dest=${4}/ if [ ! -d "$dest" ]; then mkdir $dest fi fi fi elif [ -e /ofw/serial-number ]; then # if we are from an XO, grab 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=${dest}${user}_`date +%F`.txt out_simple=${out}${simple_suffix} # create template files if [[ "$1" = "-s" || "$1" = "-f" ]]; then if [ ! -f $out_simple ]; then echo $template_simple > $out_simple else echo >> $out_simple 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