//-----------------------------------------------------------------------------
// Copyright © 2006 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/helloworld
// homepage	http://phil.ipal.org/freeware/libh/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <libh/ftr.h>
#include <libh/string.h>

//-----------------------------------------------------------------------------
// program	ftrcount
//
// purpose	Scan one or more file trees and count the objects found.
//
// syntax	ftrcount  [ filename ... ]
//
// returns	(int) >= 0 : success
//		(int) == EOF : error
//-----------------------------------------------------------------------------
int
main (
    int		argc
    ,
    char * *	argv
    ,
    char * *	envp
    )
{
    FTR		this_ftr	;
    char *	pgm		;
    int		ftype		;

    unsigned long long	count_b	;
    unsigned long long	count_c	;
    unsigned long long	count_d	;
    unsigned long long	count_f	;
    unsigned long long	count_l	;
    unsigned long long	count_p	;
    unsigned long long	count_s	;

    count_b = 0;
    count_c = 0;
    count_d = 0;
    count_f = 0;
    count_l = 0;
    count_p = 0;
    count_s = 0;

    pgm = str_tail_one( argv[0], '/' );
    if ( -- argc > 0 && * ++ argv ) {
	this_ftr = ftr_new_ca( argc, argv );
	if ( ! this_ftr ) {
	    fprintf( stderr, "%s: ftr_new_ca( %d, %p ) fails: %s\n", pgm, argc, argv, strerror( errno ) );
	    return 1;
	}
    } else {
	this_ftr = ftr_new( NULL );
	if ( ! this_ftr ) {
	    fprintf( stderr, "%s: ftr_new( NULL ) fails: %s\n", pgm, strerror( errno ) );
	    return 1;
	}
    }
    while ( ( ftype = ftr_get( this_ftr ) ) > 0 ) {
	switch ( ftype ) {
	case 'b': ++ count_b; break;
	case 'c': ++ count_c; break;
	case 'd': ++ count_d; break;
	case 'f': ++ count_f; break;
	case 'l': ++ count_l; break;
	case 'p': ++ count_p; break;
	case 's': ++ count_s; break;
	}
    }

    fprintf( stderr, "object count: %16llu\n", ftr_object_count( this_ftr ) );
    fprintf( stderr, "return count: %16llu\n", ftr_return_count( this_ftr ) );

    ftr_end( this_ftr );

    if ( count_b ) printf( "%16llu block device%s\n",		count_b, count_b==1 ? "" : "s" );
    if ( count_c ) printf( "%16llu character device%s\n",	count_c, count_c==1 ? "" : "s" );
    if ( count_d ) printf( "%16llu director%s\n",		count_d, count_d==1 ? "y" : "ies" );
    if ( count_f ) printf( "%16llu regular file%s\n",		count_f, count_f==1 ? "" : "s" );
    if ( count_l ) printf( "%16llu symbolic link%s\n",		count_l, count_l==1 ? "" : "s" );
    if ( count_p ) printf( "%16llu pipe%s\n",			count_p, count_p==1 ? "" : "s" );
    if ( count_s ) printf( "%16llu socket%s\n",			count_s, count_s==1 ? "" : "s" );

    count_b += count_c + count_d + count_f + count_l + count_p + count_s;
    printf( "%16llu total names%s\n", count_b, count_b ? "" : "s" );

    fflush( stdout );

    return 0;
}

