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

/*
***************************************************************************************************
**   touch /home/root/app_run/bin/app_CCU_V1R2B20250512
**   ln -s /home/root/app_run/bin/app_CCU_V1R2B20250512 /home/root/app_run/bin/app_CCU
**   touch /home/root/app_run/bin/app_TCU_V1R2B20250611
**   ln -s /home/root/app_run/bin/app_TCU_V1R2B20250611 /home/root/app_run/bin/app_TCU
**   touch /home/root/app_run/bin/app_PCU_V1R2B20250710
**   ln -s /home/root/app_run/bin/app_PCU_V1R2B20250710 /home/root/app_run/bin/app_PCU 
**   touch /home/root/app_run/bin/YT800KW_V1R2B20250407
**   ln -s /home/root/app_run/bin/YT800KW_V1R2B20250407 /home/root/app_run/bin/HWVersion 
***************************************************************************************************
*/


/*
**  int major;
**  int minor;
**  int build;
**  char linkname[256];
**  int ret = get_App_Version("app_CCU", linkname, sizeof(linkname), &major, &minor, &build)
**  if (ret == 0)
**  {
**      
**  }
*/
int get_App_Version(const char* appname, char* linkname, int linkname_size, int *major, int *minor, int *build)
{
    if (appname == NULL) return -1;
    if (linkname == NULL) return -1;
    if (linkname_size < 256) return -1;
    if (major == NULL) return -1; *major=0;
    if (minor == NULL) return -1; *minor=0;
    if (build == NULL) return -1; *build=0;
    
    char fullname[256];
    memset(linkname, 0, linkname_size);
    sprintf(fullname, "/home/root/app_run/bin/%s", appname);
    int ret = readlink(fullname, linkname, linkname_size);
    printf("%s.linkname=%s\n", appname, linkname);
    if (0 <= ret && ret < linkname_size)
    {
        char* basename = strstr(linkname, appname);
        if (strncmp(basename, "app_CCU", 7)==0)
        {
            sscanf(basename, "app_CCU_V%dR%dB%d", major, minor, build);
            sprintf(linkname, "%s", basename);
            // printf("%s_%d_%d_%d\n", appname, *major, *minor, *build);
            return 0;
        }
        if (strncmp(basename, "app_TCU", 7)==0)
        {
            sscanf(basename, "app_TCU_V%dR%dB%d", major, minor, build);
            sprintf(linkname, "%s", basename);
            // printf("%s_%d_%d_%d\n", appname, *major, *minor, *build);
            return 0;
        }
        if (strncmp(basename, "app_PCU", 7)==0)
        {
            sscanf(basename, "app_PCU_V%dR%dB%d", major, minor, build);
            sprintf(linkname, "%s", basename);
            // printf("%s_%d_%d_%d\n", appname, *major, *minor, *build);
            return 0;
        }
        return -1;
    }
    return -1;
}
/*
**  char HWString[256];
**  int ret = get_HW_Version(HWString, sizeof(HWString))
**  if (ret == 0)
**  {
**      
**  }
*/
int get_HW_Version(char* version, int version_size)
{
    if (version == NULL) return -1;
    if (version_size < 256) return -1;
    memset(version, 0, version_size);
    int ret = readlink("/home/root/app_run/bin/HWVersion", version, version_size);
    if (0 <= ret && ret < version_size)
    {
        version[ret] = '\0';
        // printf("HW.LinkString=%s, ret=%d\n", version, ret);
        return 0;
    }
    return -1;
}


int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        printf("\n\n\tUsage:\n");
        printf("\tgetVersion app_CCU\n");
        printf("\tgetVersion app_TCU\n");
        printf("\tgetVersion app_PCU\n");
        printf("\tgetVersion HW\n");
        return -1;
    }
    if (strncmp(argv[1], "app_CCU", 7) == 0)
    {
        int major;
        int minor;
        int build;
        char linkname[256];
        int ret = get_App_Version(argv[1], linkname, sizeof(linkname), &major, &minor, &build);
        if (ret == 0)
        {
            printf("linkname=%s, major:%d, minor:%d, build:%d\n", linkname, major, minor, build);
        }else{
            printf("failed\n");
        }
    }
    if (strncmp(argv[1], "app_TCU", 7) == 0)
    {
        int major;
        int minor;
        int build;
        char linkname[256];
        int ret = get_App_Version(argv[1], linkname, sizeof(linkname), &major, &minor, &build);
        if (ret == 0)
        {
            printf("linkname=%s, major:%d, minor:%d, build:%d\n", linkname, major, minor, build);
        }else{
            printf("failed.\n");
        }
    }
    if (strncmp(argv[1], "app_PCU", 7) == 0)
    {
        int major;
        int minor;
        int build;
        char linkname[256];
        int ret = get_App_Version(argv[1], linkname, sizeof(linkname), &major, &minor, &build);
        if (ret == 0)
        {
            printf("linkname=%s, major:%d, minor:%d, build:%d\n", linkname, major, minor, build);
        }else{
            printf("failed\n");
        }
    }
    if (strncmp(argv[1], "HW", 2) == 0)
    {
        char linkname[256];
        int ret = get_HW_Version(linkname, sizeof(linkname));
        if (ret == 0)
        {
            printf("HW.linkname=%s\n", linkname);
        }else{
            printf("failed.\n");
        }
    }
    return 0;
}
