/**
 * Copyright (c) 2008, Ilya Sukhanov aka dotCOMmie
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * This is a simple program which takes a string and converts it to a ANSI
 * terminal color code. It is intended to be used on terminal prompts so that
 * one can quickly differentiate between machines without having to read the
 * hostname:
 *
 * INSTALL
 * 		$ mkdir ~/.bash
 * 		$ gcc t2cc.c -o ~/.bash/t2cc
 *
 * 		copy line below to ~/.bashrc
 * 			PS1="\e[`~/.bash/t2cc $HOSTNAME`m\u@\h\e[0m:\e[`~/.bash/t2cc $HOSTNAME -2`m\w\e[0m\$ "
 *
 * 		If you are using a terminal with bright background add the -b flag
 *		
 **/

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

int main(int argc, char *argv[] ){
	char *tmp;
	long sum=0;
	int len, i;
	int bright=0, backward=0;
	if(argc >= 2 && argc<=4 ){
		if(argc >= 3){
			if(strcmp(argv[2],"-2")==0) backward=1;
			if(strcmp(argv[2],"-b")==0) bright=1;
			if(argc >= 4){
				if(strcmp(argv[3],"-2")==0) backward=1;
				if(strcmp(argv[3],"-b")==0) bright=1;
			}
		}

		len=strlen(argv[1]);
		if(!backward){
			tmp=argv[1];
			for(i=0; i<len; i++){
				sum+=(i+1)*(*tmp);
				*tmp++;
			}
		}else{
			tmp=argv[1]+len;
			for(i=0; i<len; i++){
				sum+=(i+1)*(*tmp);
				*tmp--;
			}
		
		}

		if(!bright){
			sum=sum%13;
			if(sum<=3)
				printf("%i", 31+sum);
			else if(sum<=5)
				printf("%i", 32+sum);
			else
				printf("1;%i", 24+sum);
		}else{
			sum=sum%11;
			if(sum<=7)
				printf("%i", 30+sum);
			else if(sum<9)
				printf("1;%i", 25+sum);
			else
				printf("1;%i", 27+sum);
		}
	}else{
		printf("Usage: %s STRING [-b] [-2]\n\t-b\tUse colors compatible with bright backgrounds.\n\t-2\tReturn a color based on a backwards hash.\n", argv[0]);
	}
}

