#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "sbs.h"

/**************************************************
 * sbs_init creates a directory in the user's home directory to be
 * used for backups.  The name of the directory is given in ROOTDIR.
 **************************************************/
void
sbs_init() 
{
    char *home;
    char path[PATH_MAX];  /* defined in standard header files */

    /* find the path to the user's home directory */
    if( (home = getenv("HOME")) == NULL ) {
	fprintf(stderr, "No environment variable for HOME\n");
	exit(1);
    }

    if(strlen(home) + strlen(ROOTDIR) > PATH_MAX) {
	fprintf(stderr, "Error: Not enough room in path buffer.");
	exit(1);
    }
    strncpy(path, home, sizeof(path));
    strncat(path, ROOTDIR, strlen(ROOTDIR) + 1);

    /* Create ROOTDIR */
    if( (mkdir(path, 0700)) == -1 ) {
	if( errno == EEXIST ) {
	    fprintf(stdout, "~/SBS already exists\n");
	} else {
	    perror("mkdir");
	    exit(1);
	}
    }
}
