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


void die(char *str)
{
        perror(str);
        exit(1);
}


char *strrep(char ch, int len)
{
        static char foo[2][100];
        static int bar = 0;


        if(bar == 2)
                bar = 0;

        if(len > (int)sizeof(foo[0]) - 1)
                len = (int)sizeof(foo[0]) - 1;

        memset(foo[bar], ch, len);
        foo[bar][len] = 0;

        return foo[bar++];
}


double get_uptime()
{
        FILE *fd;
        double uptime;
        char buf[64];


        fd = fopen("/proc/uptime", "r");
        if(fd == NULL)
                die("fopen");

        if(!fgets(buf, sizeof(buf), fd))
                die("fgets");

        fclose(fd);

        if(sscanf(buf, "%lf", &uptime) != 1)
                die("sscanf");

        return uptime;
}


char *get_uptime_str()
{
        FILE *fd;
        static char buf[128];


        fd = popen("uptime", "r");
        if(fd == NULL)
                die("fopen");

        if(!fgets(buf, sizeof(buf), fd))
                die("fgets");

        fclose(fd);

        if(buf[ strlen(buf) - 1 ] == '\n')
                buf[ strlen(buf) - 1 ] = 0;

        return buf;
}


int main()
{
        int penis_length;
        int penis_thickness = 0;
        int n;
        char *uptime_str;


        /* calculate penis length based on uptime */
        penis_length = get_uptime() / 86400;

        if(penis_length > 7)
        {
                penis_length /= 28;
                penis_thickness = 1;
        }

        /* exec /bin/uptime and return output */
        uptime_str = get_uptime_str();

        /* left trim spaces */
        while(*uptime_str == ' ')
                uptime_str++;


        /* print head */
        if(penis_thickness > 0)
        {
                printf("%s%s\n",
                        strrep(' ', penis_length + 2),
                        strrep('_', penis_thickness)
                        );
        }
        if(penis_length > 0)
        {
                printf("%s/%s)\n",
                        strrep(' ', penis_length + 1),
                        strrep(' ', penis_thickness)
                        );
        }
        else
        {
                printf(" /%s)_ %s\n",
                        strrep(' ', penis_thickness),
                        uptime_str
                        );
        }

        /* print shaft */
        for(n=0; n<penis_length; n++)
        {
                char *space = strrep(' ', penis_length - n);
                char *thick = strrep(' ', penis_thickness);

                if(n == penis_length - 1)
                {
                        printf("%s/%s/_ %s\n", space, thick, uptime_str);
                }
                else
                {
                        printf("%s/%s/\n", space, thick);
                }
        }
        /* print balls */
        printf("(%s)_)\n",
                strrep('_', penis_thickness + 1)
                );


        return 0;
}

