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

int main(int argc, char** argv){
    // read helper user
    char user[128];
    size_t readed = 0;
    FILE *helperfile = fopen("/run/etap/helper-user", "r");
    if(helperfile == NULL){
        return 0;
    }
    readed = fread(user, sizeof(char), 128, helperfile);
    user[readed] = '\0';
    fclose(helperfile);
    unlink("/run/etap/helper-user");
    if(readed == 0){
        return 0;
    }
    // build  owner string
    char owner[strlen(user)*2 + 2];
    strcpy(owner, user);
    strcat(owner, ":");
    strcat(owner, user);
    // call command
    //printf("%s\n", owner);
    uid_t u = setuid(0);
    if(u == 0){
        // TODO: dont use command
        char* args[] = {"/bin/chown", "-R", owner, "/var/lib/wine-prefix", NULL};
        execv(args[0],args);
    }
    return 1;
}
