/****************************************************************/
/* /commandline/usb-dingens					*/
/*								*/
/* Author: Tobias Weis						*/
/*								*/
/* This little helper is based on the usb-lcd-module 		*/
/* i built, check out 						*/
/* www.infolexikon.de/atmega-usb-dingens			*/
/*								*/
/* It also uses the LED-Notify-Plugin				*/
/* for pidgin, you can find this at				*/
/* http://simo.h.mattila.googlepages.com/led-notification	*/
/*								*/
/* What you have to do to get this running on your system:	*/
/* - build the usb-lcd-module, flash with the usb-lcd code	*/
/* - build the commandline-tool from usb-lcd-module-code	*/
/* - put + of 3 LEDs to PB3-PB5 of Atmega			*/
/* - download the pidgin-led-notifier, set path for led-file	*/
/* - change path in THIS file to your led-file			*/
/*								*/
/* get this at www.infolexikon.de				*/
/*								*/
/****************************************************************/

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

int main(int argc, char **argv) {
int i=0;
int j=0;
int pidgin=0;
int sekunde;
int tag=0;
int stunde=0;
int minute=0;

/*
    if (argc < 2) {
        exit(1);
    }
*/

while(1){
	for(i=0; i<4; i++){
		if(i == 0){
			system("usb-dingens-lcd set 1De_Prozi:");
			system("usb-dingens-lcd set 2`cat /proc/cpuinfo | grep cpu | grep MHz | awk '{printf \"%s_Mhz\",$4}'`");
		}
		if(i == 1){
			system("usb-dingens-lcd set 1Uptime:");
			char tag_str[1000];
			char stu_str[3];
			char min_str[3];
			char display[100];
			//WTF What a fuck, s/t i hate c!
			sprintf(tag_str, "%d",tag);
			sprintf(stu_str, "%02d",stunde);
			sprintf(min_str, "%02d",minute);
			sprintf(display, "usb-dingens-lcd set 2%s:%s:%s", tag_str, stu_str, min_str);
			system(display);
		}
		if(i == 2){
			system("usb-dingens-lcd set 1Festpladde:");
			system("usb-dingens-lcd set 2`df -h | grep /dev/sda1 | awk '{printf \"Used:_%d_von_%dG\",$3,$2}'`");
		}
		if(i == 3){
			system("usb-dingens-lcd set 1Motion:");
			system("usb-dingens-lcd set 2`ps -A | grep motion -c1| awk '{if($4==\"1\")print \"ON!\";else print \"OFF!\"}'`");
		}

//----------------------- UPTIME
		FILE *zeitdatei;
		int x=0;

		char time[20];
		zeitdatei = fopen("/proc/uptime", "r");
		
		if(zeitdatei != NULL){
			fscanf(zeitdatei, "%100c", time);
		}
		fclose(zeitdatei);

		//take first value, only whole seconds
		for(j=0;j<20;j++){
			if(time[j] == '.'){
				break;
			}
		}

		//cut the rest, wie dont need it
		for(x=j;x<20;x++){
			time[x] = 0;
		}
		time[j] = '\0';
	
		sekunde = atoi(time);
		tag = sekunde/60/60/24;
		stunde = sekunde/60/60 - tag*24;
		minute = sekunde/60 - stunde*60 - tag*60*24;

				
//--------------------------------------------------

//----------------------- LED-NOTIFY
		FILE *datei;
		char text[1];
	        // CHANGE THIS TO YOUR LED-FILE ! <-------------IMPORTANT!
		datei = fopen("/home/speyer/pidgin-stat", "r");

		if(datei != NULL){
			fscanf(datei, "%100c", text);
		}	
		fclose(datei);

		if(text[0] == '0' && pidgin == 1){
			system("usb-dingens-lcd led 1");
			pidgin = 0;
		}

		if(text[0] == '1' && pidgin == 0){
			system("usb-dingens-lcd led 1");
			pidgin = 1;
		}
//---------------------------------------------------------
		sleep(2);
	}//for
}//while
}

