#include <stdbool.h>
#include <sys/resource.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>

#include <argp.h>

#include <sys/auxv.h>

/* workarround bug on arm see #1086502*/
#ifdef __arm__
#ifndef HWCAP_CRC32
#include <asm/hwcap.h>
#endif
#endif

#if defined(__x86_64__) || defined(__i386__)
#include <sys/platform/x86.h>
#endif

#if defined(__riscv)
#if __riscv_xlen==64
#include <sys/hwprobe.h>

static inline bool risv_test_flag(uint64_t flag)
{
    struct riscv_hwprobe pairs = {
        .key = RISCV_HWPROBE_KEY_IMA_EXT_0
    };
    if(!__riscv_hwprobe(&pairs, 1, 0, NULL, 0) == 0)
        return false;
    return !!(pairs.value & flag);
}
#endif
#endif

const char *argp_program_version = "test-@NAME@ version @VERSION@";
static char doc[] = "return success if @NAME@\n"
                    "\n"
                    "@CDESCRIPTION@";
static struct argp argp = { 0, 0, 0, doc };

#ifdef AT_PLATFORM
#if  defined(__arm__) || defined(__aarch64__)
/* detect ARM ABI version will be optimized away if not used */
static inline bool need_armv_version(int atleastversion)
{
    int version;
    const char * platform = (const char *)getauxval(AT_PLATFORM);
    if (platform == NULL)
        return false;
    const size_t len = strnlen(platform, strlen("v10") + 1);
    /* at least v5 */
    if (len < strlen("v5"))
        return false;

    if (len > strlen("v10"))
        return false;

    if (*(platform++) != 'v')
        return false;

    char *endstr;
    errno = 0;
    version = strtol(platform,&endstr,10);
    if (errno != 0)
        return false;
    if (endstr == platform)
        return false;

    return (version >= atleastversion);

}
#endif

#if defined(__s390__) || defined(__s390x__)
/* return arch version according to Princople of Operatior
   off by two compared to product name */
static int s390computelevel (void)
{
    const char *platform = (const char *) getauxval (AT_PLATFORM);
    if(platform == NULL)
        return 0;

    if(strnlen(platform, strlen("z17") + 1) > strlen("z17"))
        return 0;

    if (strcmp (platform, "z13") == 0)
        return 11;
    if (strcmp (platform, "z14") == 0)
        return 12;
    if (strcmp (platform, "z15") == 0)
        return 13;
    if (strcmp (platform, "z16") == 0)
        return 14;
    if (strcmp (platform, "z17") == 0)
        return 15;
    return 0;
}
#endif

#if defined(__powerpc__) || defined(__powerpc64__)
static int ppcpowerlevel (void)
{
    const char *platform = (const char *) getauxval (AT_PLATFORM);
    if(platform == NULL)
        return 0;

    if(strnlen(platform, strlen("power10") + 1) > strlen("power10"))
        return 0;

    if (strcmp (platform, "power8") == 0)
        return 8;
    if (strcmp (platform, "power9") == 0)
        return 9;
    if (strcmp (platform, "power10") == 0)
        return 10;
    if (strcmp (platform, "power11") == 0)
        return 11;
    return 0;
}
#endif

#endif

static const struct rlimit nocore = { 0, 0 };

/* emulate kill by signal */
static void
termination_handler (int signum)
{
    _exit(128+signum);
}

static bool test_cpu_feature(void) {
    /* now test */
    @TEST@;
    return false;
}

int main(int argc,char **argv)
{
    /* no core */
    (void) setrlimit(RLIMIT_CORE, &nocore);
    /* return instead */
    struct sigaction new_action;
    new_action.sa_handler = termination_handler;
    (void) sigemptyset (&new_action.sa_mask);
    new_action.sa_flags = 0;
    (void) sigaction (SIGILL, &new_action, NULL);
    (void) sigaction (SIGBUS, &new_action, NULL);
    (void) sigaction (SIGSEGV, &new_action, NULL);
    argp_parse (&argp, argc, argv, 0, 0, 0);
    return !test_cpu_feature();
}
