--- 300.statistics.orig	2006-10-01 01:00:52.000000000 +0200
+++ 300.statistics	2006-10-01 03:22:10.000000000 +0200
@@ -15,8 +15,10 @@
     if [ -r /etc/rc.conf.local ] 
     then
         . /etc/rc.conf.local
+        periodic_conf=/etc/rc.conf.local
+    else
+	periodic_conf=/etc/rc.conf  # You usually change this on NetBSD
     fi
-    periodic_conf=/etc/rc.conf.local
 fi
 
 oldmask=$(umask)
@@ -25,6 +27,7 @@
 version="4.0"
 checkin_server=${monthly_statistics_checkin_server:-"bsdstats.org"}
 id_token_file='/var/db/bsdstats'
+dmesg(){ cat /var/run/dmesg.boot; }    # NetBSD
 
 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin
 export PATH
@@ -46,10 +49,39 @@
 	      DRIVER=`echo $line | awk -F\@ '{print $1}'`
 	      DEV=`echo $line | awk '{print $4}' | cut -c8-15`
 	      CLASS=`echo $line | awk '{print $2}' | cut -c9-14`
-	      query_string=$query_string`echo \&dev[]=$DRIVER:$DEV:$CLASS`
+	      query_string="$query_string&dev[]=$DRIVER:$DEV:$CLASS"
 	    done
 
-	    do_fetch report_devices.php?token=$TOKEN\&key=$KEY$query_string
+	    do_fetch "report_devices.php?token=$TOKEN&key=$KEY$query_string"
+	    ;;
+	NetBSD )
+            for dev in /dev/pci[0-9]
+	    do
+	      # "pcictl list -n" is not available before NetBSD 5.0 - HF
+	      pcictl $dev list 2>/dev/null
+	    done \
+	    | while read pciline
+              do
+	        devbusfn=`echo $pciline | awk '{ print $1 }'`
+		bus=`echo $devbusfn | awk -F: '{print $1}' | sed -e 's/^0*//' -e 's,^$,0,'`
+		dev=`echo $devbusfn | awk -F: '{print $2}' | sed -e 's/^0*//' -e 's,^$,0,'`
+		fun=`echo $devbusfn | awk -F: '{print $3}' | sed -e 's/^0*//' -e 's,^$,0,'`
+
+		driver=`dmesg | grep " at pci${bus} dev ${dev} function ${fun}" | awk '{print $1}'`
+		if [ "$driver" = "" ]; then driver="?" ; fi
+
+	        device=`echo $pciline | sed -e 's,[0-9:]* \(.*\) (\([^(]*\))$,\1,' -e 's,:,,'`
+	        class=`echo $pciline | sed -e 's,[0-9:]* \(.*\) (\([^(]*\))$,\2,' -e 's@,.*@@' -e 's,:,,'`
+				
+		#echo bus=$bus, dev=$dev, fun=$fun: driver=$driver, device=$device, class=$class
+
+		DRIVER=$driver
+		DEV=$device
+		CLASS=$class
+  	        query_string="$query_string&dev[]=$DRIVER:$DEV:$CLASS"
+              done
+	      
+	      do_fetch "report_devices.php?token=$TOKEN&key=$KEY$query_string"
 	    ;;
 	* )
 	    # Not supported
@@ -61,7 +93,7 @@
 get_id_token () {
     if [ -f $id_token_file ]
     then
-      if [ `cat /var/db/bsdstats | wc -l` -lt 3 ] 
+      if [ `cat $id_token_file | wc -l` -lt 3 ] 
       then
         rm $id_token_file
       fi
@@ -71,12 +103,25 @@
     then
        IDTOKEN=$(uri_escape $( openssl rand -base64 32 ) )
        
-       idf=$( mktemp "$id_token_file.XXXXXX" )  && \
-       chown root:wheel $idf          && \
-       chmod 600 $idf
+       idf=$( mktemp "$id_token_file.XXXXXX" )
+       if [ "$idf" = "" ]; then
+           echo Cannot create temp file, aborting.
+	   exit 1
+       fi
+       
+       cmd="chown root:wheel $idf"
+       if ! eval $cmd ; then
+	   echo Cannot $cmd, aborting.
+	   exit 1
+       fi
+       cmd="chmod 600 $idf"
+       if ! eval $cmd ; then
+	   echo Cannot $cmd, aborting.
+	   exit 1
+       fi
 
        sleep `random`
-       do_fetch getid.php?key=$IDTOKEN | {
+       do_fetch "getid.php?key=$IDTOKEN" | {
           local IFS
           IFS='=
 '
@@ -98,11 +143,11 @@
        } > $idf                                  && \
 
        mv $idf $id_token_file
-    if [ ! -s $id_token_file ] ;
-    then
-	echo "Nothing returned from $checkin_server"
-	exit 1
-    fi
+       if [ ! -s $id_token_file ] ;
+       then
+  	   echo "Nothing returned from $checkin_server"
+	   exit 1
+       fi
 
        echo "To protect against abuse, the initial challenge/response phase"
        echo "contains a 15 minute pause.  Please be patient while this time"
@@ -170,8 +215,8 @@
       OS=`/usr/bin/uname -s`
       get_id_token
       sleep `random`
-      do_fetch enable_token.php?key=$TOKEN\&token=$KEY
-      do_fetch report_system.php?token=$TOKEN\&key=$KEY\&rel=$REL\&arch=$ARCH\&opsys=$OS
+      do_fetch "enable_token.php?key=$TOKEN&token=$KEY"
+      do_fetch "report_system.php?token=$TOKEN&key=$KEY&rel=$REL&arch=$ARCH&opsys=$OS"
       echo "Posting monthly OS statistics to $checkin_server"
       case "$monthly_statistics_report_devices" in
           [Yy][Ee][Ss])
@@ -181,19 +226,19 @@
               VEN=$( echo $line | cut -d ' ' -f 1 )
               DEV=$( uri_escape $( echo $line | cut -d ' ' -f 2- ) )
               count=$( sysctl -n hw.ncpu )
-              do_fetch report_cpu.php?token=$TOKEN\&key=$KEY\&cpus=$count\&vendor=$VEN\&cpu_type=$DEV
+              do_fetch "report_cpu.php?token=$TOKEN&key=$KEY&cpus=$count&vendor=$VEN&cpu_type=$DEV"
               echo "Posting monthly CPU statistics to $checkin_server"
              ;;
           *) 
              echo "Posting monthly device/CPU statistics disabled"
-             echo '    set monthly_statistics_report_devices="YES" in $periodic_conf'
+             echo "    set monthly_statistics_report_devices="YES" in $periodic_conf"
              ;;
       esac
-      do_fetch disable_token.php?key=$TOKEN\&token=$KEY
+      do_fetch "disable_token.php?key=$TOKEN&token=$KEY"
       ;;
     *) 
       echo "Posting monthly OS statistics disabled"
-      echo '    set monthly_statistics_enable="YES" in $periodic_conf'
+      echo "    set monthly_statistics_enable="YES" in $periodic_conf"
     ;;
 esac
 
