#!/bin/sh

# $Id: memo,v 1.45 2013/11/11 19:32:19 gilles Exp gilles $

count_nice() {

        #set -x
        # count # prints 01 02 03 04 05 06 07 08 09 10
        # count 2       # prints 2 3 4 5 6 7 8 9 10 11
        # count 005     # prints 005 006 007 008 009 010 011 012 013 014
        # count 5 23    # prints 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
        # count 5 23 3  # prints 5 8 11 14 17 20 23

        nb_from=${1:-"01"}
        nb_from_ln=`expr length $nb_from`

        nb_to=${2:-`expr $nb_from + 9`}
        nb_incr=${3:-"1"}
        nb_counter=$nb_from
        while test $nb_counter -le $nb_to;do
                nb_list="$nb_list $nb_counter"
                nb_counter=`expr $nb_counter + $nb_incr`
                nb_counter=`printf %0${nb_from_ln}d $nb_counter`
        done
        echo $nb_list
        #set +x
}


software_version() {

	VERSION=`cat VERSION`

}

statistics_lfo_frequency() {
cat <<EOFF
tail -f /usr/local/apache/logs/access_log|cat -n|grep prj/imapsync/VERSION|cat -n |grep VERSION # lfo frequency
EOFF
}

statistics_lfo_run() {
# 62.147.165.21 - - [31/Oct/2010:23:45:28 +0100] "GET /prj/imapsync/VERSION HTTP/1.0" 200 6 "-" "imapsync/1.368 (linux system, perl 5.8.8, Mail::IMAPClient 2.2.9 imapsync)"
#grep    prj/imapsync/VERSION /usr/local/apache/logs/access_log  | sort -n | cut -d ' ' -f 1,12,13|uniq -c | sort -n # list ip

echo lfo list ip
ssh glamiral@linux-france.org "grep -h prj/imapsync/VERSION /usr/local/apache/logs/access_log.bak|cut -d ' ' -f 1,12,13,18,19|sort -n|uniq -c|sort -n" # list ip
echo lfo Win32
ssh glamiral@linux-france.org "grep -h prj/imapsync/VERSION /usr/local/apache/logs/access_log.bak|cut -d ' ' -f 1,12,13,18,19|sort -n|uniq -c|sort -n |grep MSWin32" # Win32
echo lfo OS usage+basename
ssh glamiral@linux-france.org "grep -h prj/imapsync/VERSION /usr/local/apache/logs/access_log.bak|cut -d ' ' -f 13,18,19     |sort -n|uniq -c| sort -n" # OS usage+basename
echo lfo imapsync release
ssh glamiral@linux-france.org "grep -h prj/imapsync/VERSION /usr/local/apache/logs/access_log.bak|cut -d ' ' -f 12           |sort -n|uniq -c| sort -n" # imapsync release
echo -n "lfo count ip:      "
ssh glamiral@linux-france.org "grep -h prj/imapsync/VERSION /usr/local/apache/logs/access_log.bak|cut -d ' ' -f 1,12,13,18,19|sort -n|uniq -c|sort -n|wc -l" # count ip
echo -n "lfo imapsync runs: "
ssh glamiral@linux-france.org "grep -h prj/imapsync/VERSION /usr/local/apache/logs/access_log.bak|wc -l" # imapsync runs
date
}


statistics_ks_frequency() {

cat <<EOFF
tail -f /var/log/apache2/access.log |cat -n|grep prj/imapsync/VERSION|cat -n |grep VERSION # ks frequency
EOFF
}

statistics_ks_run() {

echo ks list ip
ssh root@ks "cd /var/log/apache2/;grep -h prj/imapsync/VERSION access.log_????????|cut -d ' ' -f 1,12,13,18,19|sort -n|uniq -c|sort -n" # list ip
echo ks Win32
ssh root@ks "cd /var/log/apache2/;grep -h prj/imapsync/VERSION access.log_????????|cut -d ' ' -f 1,12,13,18,19|sort -n|uniq -c|sort -n |grep MSWin32" # Win32
echo OS usage+basename
ssh root@ks "cd /var/log/apache2/;grep -h prj/imapsync/VERSION access.log_????????|cut -d ' ' -f 13,18,19     |sort -n|uniq -c| sort -n" # OS usage+basename
echo ks imapsync release
ssh root@ks "cd /var/log/apache2/;grep -h prj/imapsync/VERSION access.log_????????|cut -d ' ' -f 12           |sort -n|uniq -c| sort -n" # imapsync release
echo -n "ks count ip:      "
ssh root@ks "cd /var/log/apache2/;grep -h prj/imapsync/VERSION access.log_????????|cut -d ' ' -f 1,12,13,18,19|sort -n|uniq -c|sort -n|wc -l" # count ip
echo -n "ks imapsync runs: "
ssh root@ks "cd /var/log/apache2/;grep -h prj/imapsync/VERSION access.log_????????|wc -l" # imapsync runs
date
}


statistics_VERSION_lfo() {

TMPDIR=.
export TMPDIR

echo statistics_VERSION_getstats
statistics_VERSION_getstats() {
	for f in /home/lf/backuplog/linux-france.org.??-??-201[123456789].bz2; do 
		b=`basename "$f" .bz2`
		#echo "$b"
		test -f ${b}.imapsync_VERSION && continue
		echo NOT DONE ${b}.imapsync_VERSION
		bzip2 -dc "$f" | grep -h /prj/imapsync/VERSION > ${b}.imapsync_VERSION
	done
}

echo "statistics_VERSION_monthly_ip 02 2012 # long"
statistics_VERSION_monthly_ip() {
	month=${1:-`date '+%m'`}
	year=${2:-`date '+%Y'`}
	cut -d ' ' -f 1,12,13,18,19 linux-france.org.??-${month}-${year}.imapsync_VERSION |sort -n |uniq -c|sort -n > stats_imapsync_${year}_${month}.ip
}

echo "statistics_VERSION_monthly_ip_wc 02 2012 # short"
statistics_VERSION_monthly_ip_wc() {
	month=${1:-`date '+%m'`}
	year=${2:-`date '+%Y'`}
	test -f stats_imapsync_${year}_${month}.ip || statistics_VERSION_monthly_ip $month  ${year}
	wc -l stats_imapsync_${year}_${month}.ip
}

echo "statistics_VERSION_monthly_runs 02 2012 # short"
statistics_VERSION_monthly_runs() {
	month=${1:-`date '+%m'`}
	year=${2:-`date '+%Y'`}
	test -f stats_imapsync_${year}_${month}.runs || wc -l linux-france.org.??-${month}-${year}.imapsync_VERSION > stats_imapsync_${year}_${month}.runs
	cat stats_imapsync_${year}_${month}.runs
}

echo "statistics_VERSION_yearly_os 2012 # short"
statistics_VERSION_yearly_os() {
	year=${1:-`date '+%Y'`}
	(
	cd /home/lf/glamiral/imapsync_stats
	Linux=`grep -i linux     stats_imapsync_${year}.ip | wc -l`
	Win32=`grep -i MSWin32   stats_imapsync_${year}.ip | wc -l`
	Darwin=`grep -i darwin   stats_imapsync_${year}.ip | wc -l`
	FreeBSD=`grep -i freebsd stats_imapsync_${year}.ip | wc -l`
	Solaris=`grep -i solaris stats_imapsync_${year}.ip | wc -l`
	OpenBSD=`grep -i openbsd stats_imapsync_${year}.ip | wc -l`
	Other=`egrep -i -v 'linux|MSWin32|darwin|freebsd|solaris|openbsd'    stats_imapsync_${year}.ip |wc -l`
	Nb_All=`cat           stats_imapsync_${year}.ip | wc -l`
	for OS in Linux Win32 Darwin FreeBSD Solaris OpenBSD Other; do
		#echo $OS `eval "echo \\$$OS"` / $Nb_All
		Nb_OS=`eval "echo \\$$OS"`
		PerCent=`echo "scale=2; 100*$Nb_OS/$Nb_All" | bc -l`
		echo $OS $PerCent %
	done
	)
}

echo
echo "statistics_VERSION_monthly 02 2012 # long, good choice"
statistics_VERSION_monthly() {
	month=${1:-`date '+%m'`}
	year=${2:-`date '+%Y'`}
	(
	statistics_VERSION_getstats
	statistics_VERSION_monthly_ip    $month $year
	statistics_VERSION_monthly_runs  $month $year
	statistics_VERSION_monthly_ip_wc $month $year
	)
}


echo "statistics_VERSION_yearly_ip 2012 # very long"
statistics_VERSION_yearly_ip() {
	year=${1:-`date '+%Y'`}
	(
	statistics_VERSION_getstats
	cd /home/lf/glamiral/imapsync_stats
	cut -d ' ' -f 1,12,13,18,19 linux-france.org.??-??-${year}.imapsync_VERSION |sort -n |uniq -c|sort -n > stats_imapsync_${year}.ip
	)
}

statistics_VERSION_yearly_runs() {
	year=${1:-`date '+%Y'`}
	(
	cd /home/lf/glamiral/imapsync_stats
	test -f stats_imapsync_${year}.ip && cat stats_imapsync_${year}.ip | awk '{ sum+=$1 } END {print sum}'
	)
}


echo "statistics_VERSION_synthesis 2012 # short"
statistics_VERSION_synthesis() {
	year=${1:-`date '+%Y'`}
	(
	cd /home/lf/glamiral/imapsync_stats
	echo Nb users each month
	wc -l stats_imapsync_${year}_??.ip
	echo Nb runs each month
	tail -n 1 stats_imapsync_${year}_??.runs
	echo % Operating systems
	statistics_VERSION_yearly_os $year
	echo Biggest users
	tail -n 5 stats_imapsync_${year}.ip
	echo Nb users each year
	wc -l stats_imapsync_????.ip
	echo "Nb runs each year : "
	for yyyy in `count_nice 2011 ${year}`; do
		echo -n "$yyyy: "
		statistics_VERSION_yearly_runs $yyyy
	done
	)
}


}

statistics_VERSION_ks() {

TMPDIR=.
export TMPDIR

echo statistics_VERSION_getstats
statistics_VERSION_getstats() {
	(
	cd $HOME/imapsync_stats
	for f in /var/log/apache2/access.log_????????.gz ; do 
		b=`basename "$f" .gz`
		#echo "$b"
		test -f ${b}.imapsync_VERSION && continue
		echo NOT DONE ${b}.imapsync_VERSION
		zgrep -h /prj/imapsync/VERSION "$f" > ${b}.imapsync_VERSION
	done
	)
}

echo "statistics_VERSION_monthly_ip 02 2012 # long"
statistics_VERSION_monthly_ip() {
	month=${1:-`date '+%m'`}
	year=${2:-`date '+%Y'`}
	(
	cd $HOME/imapsync_stats
	cut -d ' ' -f 1,12,13,18,19 access.log_${year}${month}??.imapsync_VERSION |sort -n |uniq -c|sort -n > stats_imapsync_${year}_${month}.ip
	)
}

echo "statistics_VERSION_monthly_os 08 2012 # short"
statistics_VERSION_monthly_os() {
	month=${1:-`date '+%m'`}
	year=${2:-`date '+%Y'`}
	(
	cd $HOME/imapsync_stats
	Linux=`grep -i linux stats_imapsync_${year}_${month}.ip | wc -l`
	Win32=`grep -i MSWin32 stats_imapsync_${year}_${month}.ip | wc -l`
	Darwin=`grep -i darwin stats_imapsync_${year}_${month}.ip | wc -l`
	FreeBSD=`grep -i freebsd stats_imapsync_${year}_${month}.ip | wc -l`
	Solaris=`grep -i solaris stats_imapsync_${year}_${month}.ip | wc -l`
	OpenBSD=`grep -i openbsd stats_imapsync_${year}_${month}.ip | wc -l`
	Other=`egrep -i -v 'linux|MSWin32|darwin|freebsd|solaris|openbsd'    stats_imapsync_${year}.ip |wc -l`
	Nb_All=`cat           stats_imapsync_${year}.ip | wc -l`
	for OS in Linux Win32 Darwin FreeBSD Solaris OpenBSD Other; do
		#echo $OS `eval "echo \\$$OS"` / $Nb_All
		Nb_OS=`eval "echo \\$$OS"`
		PerCent=`echo "scale=2; 100*$Nb_OS/$Nb_All" | bc -l`
		echo $OS $PerCent%
	done
	)
}

echo "statistics_VERSION_yearly_os 2012 # short"
statistics_VERSION_yearly_os() {
	year=${1:-`date '+%Y'`}
	(
	cd $HOME/imapsync_stats
	Linux=`grep -i linux     stats_imapsync_${year}.ip | wc -l`
	Win32=`grep -i MSWin32   stats_imapsync_${year}.ip | wc -l`
	Darwin=`grep -i darwin   stats_imapsync_${year}.ip | wc -l`
	FreeBSD=`grep -i freebsd stats_imapsync_${year}.ip | wc -l`
	Solaris=`grep -i solaris stats_imapsync_${year}.ip | wc -l`
	OpenBSD=`grep -i openbsd stats_imapsync_${year}.ip | wc -l`
	Other=`egrep -i -v 'linux|MSWin32|darwin|freebsd|solaris|openbsd'    stats_imapsync_${year}.ip |wc -l`
	Nb_All=`cat           stats_imapsync_${year}.ip | wc -l`
	for OS in Linux Win32 Darwin FreeBSD Solaris OpenBSD Other; do
		#echo $OS `eval "echo \\$$OS"` / $Nb_All
		Nb_OS=`eval "echo \\$$OS"`
		PerCent=`echo "scale=2; 100*$Nb_OS/$Nb_All" | bc -l`
		echo $OS $PerCent %
	done
	)
}


echo "statistics_VERSION_monthly_ip_wc 02 2012 # short"
statistics_VERSION_monthly_ip_wc() {
	month=${1:-`date '+%m'`}
	year=${2:-`date '+%Y'`}
	(
	cd $HOME/imapsync_stats
	test -f stats_imapsync_${year}_${month}.ip || statistics_VERSION_monthly_ip $month ${year}
	wc -l stats_imapsync_${year}_${month}.ip
	)
}

echo "statistics_VERSION_monthly_runs 02 2012 # short"
statistics_VERSION_monthly_runs() {
	month=${1:-`date '+%m'`}
	year=${2:-`date '+%Y'`}
	(
	cd $HOME/imapsync_stats
	wc -l access.log_${year}${month}??.imapsync_VERSION > stats_imapsync_${year}_${month}.runs
	cat stats_imapsync_${year}_${month}.runs
	)
}

echo
echo "statistics_VERSION_monthly 02 2012 # long, good choice"
statistics_VERSION_monthly() {
	month=${1:-`date '+%m'`}
	year=${2:-`date '+%Y'`}
	(
	statistics_VERSION_getstats
	statistics_VERSION_monthly_ip    $month $year
	statistics_VERSION_monthly_runs  $month $year
	statistics_VERSION_monthly_ip_wc $month $year
	)
}


echo "statistics_VERSION_yearly_ip 2012 # very long"
statistics_VERSION_yearly_ip() {
	year=${1:-`date '+%Y'`}
	(
	statistics_VERSION_getstats
	cd $HOME/imapsync_stats
	cut -d ' ' -f 1,12,13,18,19 access.log_${year}????.imapsync_VERSION |sort -n |uniq -c|sort -n > stats_imapsync_${year}.ip
	)
}

statistics_VERSION_yearly_runs() {
	year=${1:-`date '+%Y'`}
	(
	cd $HOME/imapsync_stats
	test -f stats_imapsync_${year}.ip && cat stats_imapsync_${year}.ip | awk '{ sum+=$1 } END {print sum}'
	)
}



echo "statistics_VERSION_synthesis 2012 # short"
statistics_VERSION_synthesis() {
	year=${1:-`date '+%Y'`}
	(
	cd $HOME/imapsync_stats
	echo Nb users each month
	wc -l stats_imapsync_${year}_??.ip
	echo Nb runs each month
	tail -n 1 stats_imapsync_${year}_??.runs
	echo % Operating systems
	statistics_VERSION_yearly_os $year
	echo Biggest users
	tail -n 5 stats_imapsync_${year}.ip
	echo Nb users each year
	wc -l stats_imapsync_????.ip
	echo "Nb runs each year : "
	for yyyy in `count_nice 2011 ${year}`; do
		echo -n "$yyyy: "
		statistics_VERSION_yearly_runs $yyyy
	done
	)
}




}


niouzes_compil() {
	(
	cd /g/public_html/www.linux-france.org/html/
	m4 niouzes.m4 > niouzes.xml
	python ./niouzes/getmynews.py --neuf niouzes.xml > niouzes-neuf.html
	python ./niouzes/getmynews.py --html niouzes.xml > niouzes-html.html
	)
}



lfo_announce() {
software_version
NEWS_FILE="/g/public_html/www.linux-france.org/html/niouzes/niouzes_imapsync.xml"
if ! newer VERSION $NEWS_FILE; then 
	echo "$VERSION already announced"
else
	cat > $NEWS_FILE << EOF

<news date="`date  '+%Y%m%d'`">
`LANG=fr_FR date  '+%A %d %B %Y'` : Synchronisez ou migrez vos boites 
aux lettres avec conomie et l'outil <A
HREF="prj/imapsync/">imapsync $VERSION</A> (Gilles LAMIRAL)
</news>
EOF
fi
niouzes_compil
}

#' nedit sucks with syntax color

fm_init() {
software_version
NEWS_FILE_FM="./freshmeat_submition"
NEWS_FILE_FM_INP=${NEWS_FILE_FM}.inp
NEWS_FILE_FM_OUT=${NEWS_FILE_FM}.json
}

fm_read_param() {
# read definitions
. $NEWS_FILE_FM_INP
}


fm_read_announce() {

  fm_init
  fm_read_param

  cat << EOF
{
  "release": {
    "tag_list": "stable, $RELEASE_FOCUS",
    "version": "$VERSION",
    "hidden_from_frontpage": false,
    "changelog": "$TEXT_BODY"
  }
}

EOF
}

fm_announce() {
  fm_init

  if ! newer VERSION $NEWS_FILE_FM_OUT; then 
	echo "$VERSION already submitted on freshmeat"
  else
    if newer VERSION $NEWS_FILE_FM_INP; then
	echo "Update $NEWS_FILE_FM_INP please"
	return 1
    fi

    fm_read_announce > $NEWS_FILE_FM_OUT 
    curl -X PUT -d @../../var/pass/secret.freshmeat -d @$NEWS_FILE_FM_OUT \
         -H "Content-Type: application/json" \
         http://freshmeat.net/projects/imapsync.json

  fi
}


