#include <gtest/gtest.h>

#include "adb.h"

#include <iostream>
#include <string>


using namespace std;

static const string kGrepPattern = "grep -e \"PA_NWD_lib,version_[0-9]*.[0-9]*.[0-9]*,.*\"";

static const string kArmArchV5 = "armeabi";
static const string kArmArchV7 = "armeabi-v7a";
static const string kArmArchV8 = "arm64-v8a";

static const string kLibLocationPath = "/vendor/lib";
static const string kLibPaAArch32 = kLibLocationPath + "/" + "libpa.so";
static const string kLibPaAArch64 = kLibLocationPath + "64" + "/" + "libpa.so";

TEST(VersionCheck, PaNwdLib) {
  string cmd = "getprop | grep -e ro.product.cpu.abi]";
  string output;
  int res = Adb::Shell(cmd, output);
  ASSERT_EQ(0, res);

  string palib_ver;
  if (output.find(kArmArchV8) != string ::npos) {
    cmd = "strings " + kLibPaAArch64 + " | " + kGrepPattern;
    Adb::Shell(cmd, palib_ver);
  } else if (output.find(kArmArchV5) != string ::npos ||
      output.find(kArmArchV7) != string ::npos) {
    cmd = "strings " + kLibPaAArch32 + " | " + kGrepPattern;
    Adb::Shell(cmd, palib_ver);
  } else {
    FAIL() << "Unknown architecture";
  }

  ASSERT_FALSE(palib_ver.empty());
  cout << palib_ver << endl;
}
