#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <ctype.h>
#include <assert.h>
#include <math.h>
#include <sysexits.h>

static struct option long_opts[] = 
{
	{"urgency", 2, 0, 'u'},
	{"complexity", 2, 0, 'c'},
	{"importance", 2, 0, 'i'},
	{"skill", 2, 0, 's'},
	{"frequency", 2, 0, 'f'},
	{"help", 0, 0, 'h'},
	{0, 0, 0, 0}
};

double murphy (int u, int c, int i, int s, int f)
{
	double a = 0.7;
	return (((u+c+i) * (10-s))/20 * a * (1/(1-sin((double)f/10))));
}

void usage (void)
{
	fprintf (stderr, "Usage: murphy -u NUM -c NUM -i NUM -f NUM -s NUM\n");
	fprintf (stderr, "\twhere NUM is a number between 1 and 9\n");
	return;
}
			

int main (int argc, char **argv) 
{
	int g;
        int optnum = 0;
	int u, c, i, s, f = 0;
	double result;
	int options = 0;

	if (argc != 6 && argc != 11)
	{
		usage();
		exit(EX_USAGE);
	}

   	while (1) 
	{
        	int this_opt = optind ? optind : 1;

        	g = getopt_long (argc, argv, "u:c:i:s:f:h",
            		long_opts, &options);
        	if (g == -1)
            		break;

        	switch (g) 
		{
        	case 'u':
			assert(optarg && "assert u");
			u = atoi(optarg);
			if (u < 1 || u > 9)
			{
				usage();
				exit(EX_USAGE);
			}
            		break;

       	 	case 'c':
			assert(optarg && "assert c");
			c = atoi(optarg);
			if (c < 1 || c > 9)
			{
				usage();
				exit(EX_USAGE);
			}
            		break;

       	 	case 'i':
			assert(optarg && "assert i");
			i = atoi(optarg);
			if (i < 1 || i > 9)
			{
				usage();
				exit(EX_USAGE);
			}
            		break;

        	case 's':
			assert(optarg && "assert s");
			s = atoi(optarg);
			if (s < 1 || s > 9)
			{
				usage();
				exit(EX_USAGE);
			}
            		break;

        	case 'f':
			assert(optarg && "assert f");
			f = atoi(optarg);
			if (f < 1 || f > 9)
			{
				usage();
				exit(EX_USAGE);
			}
            		break;

		case 'h':
        	case '?':
			usage();
			exit(EX_OK);

        	default:
			usage();
			exit(EX_USAGE);
        	}
    	}
	if (optind < argc) 
	{
		usage();
		exit(EX_USAGE);
	}

	result = murphy (u, c, i, s, f);
	printf ("%3.3f\n", result);
	
	exit(EX_OK);
}
