//-----------------------------------------------------------------------------
// 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/io
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// program	test_ft_copy
//
// purpose	Test the ft_copy function by copying one tree to another.
//
// syntax	test_ft_copy  <source_tree>  <target_tree>
//
// note		The target tree must not exist.
//-----------------------------------------------------------------------------
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

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

//-----------------------------------------------------------------------------
// function	main
//-----------------------------------------------------------------------------
int
main (
    int		argc
,
    char * *	argv
)
{
    struct stat		stat_buf	;
    const char *	program_name	;
    int			cwd_fd		;


    program_name = str_tail( argv[0], '/', 1 );

    //-- Make sure we have enough arguments.
    if ( argc <= 2 ) {
	fprintf( stderr, "%s: missing arguments\n", program_name );
	fprintf( stderr, "syntax:  %s  <source_tree>  <target_tree>\n", program_name );
	return 1;
    }

    //-- Get current directory as a file descriptor.
    cwd_fd = open( ".", O_RDONLY );
    if ( cwd_fd < 0 ) {
	fprintf( stderr, "%s: open( \".\", O_RDONLY ) failed: %s\n",
		 program_name, strerror( errno ) );
	return 1;
    }

    //-- Make sure the source exists.
    if ( access( argv[1], R_OK ) != 0 ) {
	fprintf( stderr, "%s: access( \"%s\", R_OK ) failed: %s\n",
		 program_name, argv[1], strerror( errno ) );
    }

    //-- Make sure the target is absent.
    if ( lstat( argv[2], & stat_buf ) == 0 ) {
	fprintf( stderr, "%s: target \"%s\" already exists.\n",
		 program_name, argv[2] );
    }

    //-- Set options.
    ft_copy_set_continue_off();
    ft_copy_set_list_off();
    ft_copy_set_mmap_on();
    ft_copy_set_sort_on();

    //-- Do the whole copy.
    return !! ft_copy( cwd_fd, argv[1], cwd_fd, argv[2] );
}

