/****************************************************************************/
/***                                                                      ***/
/*** : NEWPIC.C                                Last Update: April 25/92   ***/
/*** : Chris Herborth (GEnie: C.HERBORTH)                                 ***/
/***                                                                      ***/
/*{{{  Program Description*/
/*** : This program is designed to copy a picture file from a folder to   ***/
/***   a specific file.  I've been using it with the Desk Picture desk    ***/
/***   accesory (which loads nicely into MultiDesk Deluxe!), and Ed       ***/
/***   Krimen's been using a modified version to pick random background   ***/
/***   pictures for NeoDesk...  With slight modification (and possibly    ***/
/***   a name change) you could use this to pick random samples to play   ***/
/***   at startup (with Dave Baggett's PLAY.TTP), or random MOD files     ***/
/***   for use with any of those background MOD players for the STe/TT.   ***/
/*}}}  */
/***                                                                      ***/
/*{{{  How to modify the source.*/
/*** : To change it, modify THE_PATH, THE_SPEC, and THE_FILE.  This is    ***/
/***   what they do:                                                      ***/
/***            THE_PATH -> Source of the random files.                   ***/
/***            THE_SPEC -> The Filespec for the files (ex, *.SPL for     ***/
/***                        sample files).  This has to include the       ***/
/***                        directory in THE_PATH.                        ***/
/***            THE_FILE -> The destination path/filename for the file.   ***/
/***   Be sure to use \\ instead of \ in the path specifications so that  ***/
/***   the C compiler doesn't eat them.                                   ***/
/*}}}  */
/***                                                                      ***/
/*{{{  This is a folded program!*/
/*** : This file looks a little weird, full of {{{ and }}} lines...       ***/
/***   These are the folding fences used by Origami, one of the best      ***/
/***   programmers text editors around.  The keys can be bound quite a    ***/
/***   few ways too...  Very flexible!                                    ***/
/*}}}  */
/***                                                                      ***/
/****************************************************************************/

/*{{{  Include files*/
#include <osbind.h>
#include <stdio.h>
#include <fileinfo.h>
#include <gemd_err.h>

#ifdef SOZOBON
#include <malloc.h>
#endif
/*}}}  */

/*{{{  Definitions*/
#define ATTRS ( NO_ATTR | READONLY | ARCHIVE )
#define THE_PATH "C:\\PIC"
#define THE_SPEC "C:\\PIC\\*.PC3"
#define THE_FILE "C:\\STHIGH.PC3"
/*}}}  */

/*{{{  main()*/
main()
{
/*{{{  variables*/
char path[128];
int  numb,
     ret,
     pic_num,
     count,
     file_in,
     file_out;
FILE_INFO *info;
char *buff;
/*}}}  */

/*{{{  Intro*/
    puts( "\33p NewPic!                                   \33q" );
    puts( " \275 1992 Chris Herborth" );
/*}}}  */

/*{{{  Get the DTA*/
    info = (FILE_INFO *)Fgetdta();

/*}}}  */

/*{{{  Look for the first file.*/
    ret = Fsfirst( THE_SPEC, ATTRS );
    if( ret != 0 )
    {
        printf( "No pictures in " );
        printf( THE_PATH );
        printf( "!\n" );
        exit();
    }
    else
        count = 1;
/*}}}  */

/*{{{  Get the files.*/
    while( ret == E_OK )
    {
        ret = Fsnext();
        if( ret == E_OK )
            count++;
    }
/*}}}  */

/*{{{  Mouth off.*/
    printf( "%d pictures found...\n", count );
/*}}}  */

/*{{{  Choose a file.*/
    numb = ( rand() ^ rand() ) % 255;

    for( ret = 0; ret < numb; ret++ )
        pic_num = ( rand() ^ rand() ) % count;

    ret = Fsfirst( THE_SPEC, ATTRS );
    while( pic_num > 0 )
    {
        ret = Fsnext();
        pic_num--;
    }
/*}}}  */

/*{{{  Got the file!*/
    printf( "%s chosen! (%ld bytes)\n", info->fname, info->flen );

    ret = Fdelete( THE_FILE );
    strcpy( path, THE_PATH );
    strcat( path, "\\\0" );
    strcat( path, info->fname );

    buff = (char *)Malloc( info->flen );
    if( buff == 0L )
    {
        puts( "Unable to allocate memory!" );
        exit();
    }

    file_in = Fopen( path, 0 );
    Fread( file_in, info->flen, buff );
    Fclose( file_in );

    strcpy( path, THE_FILE );
    ret = strlen( path );
    path[ret] = 0x00;

    file_out = Fcreate( path, NO_ATTR );
    Fwrite( file_out, info->flen, buff );
    Fclose( file_out );

    Mfree( buff );
/*}}}  */

/*{{{  We're done.*/
    exit();
/*}}}  */
}
/*}}}  */
