#include <syslog.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <systemd/sd-bus.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
    syslog(LOG_INFO, "copy");
    FILE *file = fopen("/etc/restore.txt", "r");
    if (file == NULL)
    {
        printf("无法打开文件");
        return 1;
    }

    char line[256];
    char **result_etc = malloc(100 * sizeof(char *));
    char **result_var = malloc(100 * sizeof(char *));
    int count_etc= 0;
    int count_var= 0;
    while (fgets(line, sizeof(line), file))
    {    
        // 去除换行符
        syslog(LOG_INFO, "27");
        line[strcspn(line, "\r")] = 0;
        char *copy_data_etc = strstr(line, "COPY_ETC=");
        if (copy_data_etc != NULL)
        {
            char *token = strtok(copy_data_etc + strlen("COPY_ETC="), ";");
            while (token != NULL && count_etc < 100)
            {
                result_etc[count_etc] = strdup(token);
                count_etc++;
                token = strtok(NULL, ";");
            }
        }
        
        char *copy_data = strstr(line, "COPY_VAR=");
        if (copy_data != NULL)
        {
            char *token_var = strtok(copy_data + strlen("COPY_VAR="), ";");
            while (token_var != NULL && count_var < 100)
            {
                result_var[count_var] = strdup(token_var);
                count_var++;
                token_var = strtok(NULL, ";");
            }
        }
    }
    fclose(file);
    // 输出结果
    for (int i = 0; i < count_etc; i++)
    {
        char command1[255] = {};
        // snprintf(command,sizeof(command),"%s %s %s %s", "/etc/ostree-extra-developer/developer.sh", value,config,log);
        snprintf(command1, sizeof(command1), "%s %s %s", "/usr/ost-ex.d/copy.sh","etc" ,result_etc[i]);
        if (system(command1) == 0)
        {
            syslog(LOG_INFO, "copy success");
        }
    }
      for (int j = 0; j < count_var; j++)
     {
           printf("%s\n",result_var[j]);
        char command[255] = {};
        // snprintf(command,sizeof(command),"%s %s %s %s", "/etc/ostree-extra-developer/developer.sh", value,config,log);
        snprintf(command, sizeof(command), "%s %s %s", "/usr/ost-ex.d/copy.sh","var",result_var[j]);
        if (system(command) == 0)
        {
            syslog(LOG_INFO, "copy success");
        }
    }
//    printf("------restort end-----");
    return 0;
}