#!/bin/bash

set -e

#DEBHELPER#

if [ "$1" = "configure" ]; then
  if ! grep -q 'MAILTO=' /etc/popularity-contest.conf; then
    cat <<-EOF

	PACKAGE POPULARITY CONTEST
        
	You can have your system anonymously e-mail the Debian developers
	with statistics about your most used Debian packages.  (NOTE: e-mail
	servers add their own tracing information and your data won't be
	completely anonymous while in transit.)
	
	This information helps us make decisions such as which packages
	should go on the first Debian CD.  Also, we can improve future
	versions of Debian so that the most popular packages are the ones
	which are installed automatically for new users.
	
	If you choose to participate, the automatic submission script will
	run once every week automatically.
	
	EOF
    
    yesno=""
    
    while [ "$yesno" != 'y' -a "$yesno" != 'n' ]; do
    	echo -e "Do you want to participate (y/n)? "\\c
    	read yesno
    done
    
    echo
    
    if [ "$yesno" = "y" ]; then
    	echo "Great!  We appreciate your contribution."
    	cat <<-EOF >/etc/popularity-contest.conf
		# Config file for Debian's popularity-contest package.
		#
		# MAILTO specifies the address to e-mail statistics to each
		# week.  If you comment it out, the cron job will be disabled.
		#
		MAILTO="apenwarr-survey@debian.org"
		
		EOF
    else
    	echo "Okay.  No statistics will be sent."
    
    	cat <<-EOF >/etc/popularity-contest.conf
		# Config file for Debian's popularity-contest package.
		#
		# MAILTO specifies the address to e-mail statistics to each
		# week.  If you comment it out, the cron job will be disabled.
		#
		#MAILTO="apenwarr-survey@debian.org"
		
		EOF
    fi
    
    echo "If you change your mind, just edit /etc/popularity-contest.conf."
  fi
  
  if ! grep -q '^MY_HOSTID=' /etc/popularity-contest.conf; then
  	echo
  	echo Adding a new MY_HOSTID line to /etc/popularity-contest.conf...
  
  	cat <<-EOF >>/etc/popularity-contest.conf
  	
	# MY_HOSTID is a secret number that the popularity-contest receiver
	# uses to keep track of your submissions.  Whenever you send in a
	# new entry, it overwrites the last one that had the same HOSTID.
	#
	# This key was generated automatically so you should normally just
	# leave it alone.
	#
	EOF
  	
  	dd if=/dev/urandom bs=1k count=1 2>/dev/null | md5sum \
		| (echo -n 'MY_HOSTID='; cat) >>/etc/popularity-contest.conf
  fi
fi
