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

const char * env_var = "SSH_CONNECTION";

int main (int argc, char * argv[]) {
  char * path = getenv(env_var);
  printf("%c[%d;%d;%dm", 0x1B, 1, 31, 40); // color output red
  
  if (path == NULL) {
    printf("This is NOT an SSH session.\n");
    fflush(stdout);
#ifdef FIREFOX
    printf("Automatically launching Mozilla Firefox...\n");
    system("add webbrowsers && firefox &");
#endif
    printf("%c[%d;%d;%dm", 0x1B, 0, 37, 40); // reset colors
    return EXIT_FAILURE;
  }

  char * ip_address = calloc (sizeof(char), 16);
  char * path_p = path; 
  char * ip_address_p = ip_address;
  while ( * path_p != ' ' && * path_p != '\0') {
    * ip_address_p = * path_p;
    ip_address_p++; path_p++;
  }

  printf("You are remotely logged in from %s.\n", ip_address); 
  fflush(stdout);
  free(ip_address);
  printf("%c[%d;%d;%dm", 0x1B, 0, 37, 40); // reset colors
  return EXIT_SUCCESS;
}
