/*
 * Random Selection
 *
 * Usage:
 *		rnd_select <cmd> <arg1> ... <argN>
 *
 * Randomly selects one of the given arguments and calls <cmd> using it.
 *
 * Stefan Haenssgen 08-jun-94
 *
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/timeb.h>

main(argc,argv)
int argc;
char **argv;
{
    struct timeb t;
    char   *cmd;
    char   *param;
    int    num;
    char   *selected_cmd;

    ftime(&t); srandom(t.time+t.millitm);
    if (argc<3) {
	printf("usage: %s <cmd> <arg1> ... <argN>\n",argv[0]);
	printf("       Randomly selects one of the given arguments and calls <cmd> using it.\n");
	exit(1);
    };

    cmd = argv[1];		/* Command to run			*/
    num = argc-2;		/* Number of parameters to choose from	*/

    if (num < 2) {
	param = argv[2];
    } else {
	param = argv[2 + (random() % num)];
    };
    
    selected_cmd = (char*) malloc (strlen(cmd) + strlen(param) + 3);
    sprintf(selected_cmd,"%s %s\n",cmd,param);
    system(selected_cmd);
}

