#!/bin/sh

set -e

[ "$AUTOPKGTEST_TMP" ] || { echo "AUTOPKGTEST_TMP not set" >&2; exit 1; }

cd "$AUTOPKGTEST_TMP"

cat <<EOF > tlshtest.cxx
#include <iostream>
#include <tlsh.h>

// Quotes from Emma Goldman, now public domain
// https://theanarchistlibrary.org/library/emma-goldman-the-child-and-its-enemies
static const unsigned char data1[] = "Every institution of our day, the family, the State, our moral codes, sees in every strong, beautiful, uncompromising personality a deadly enemy; therefore every effort is being made to cramp human emotion and originality of thought in the individual into a straight-jacket from its earliest infancy; or to shape every human being according to one pattern; not into a well-rounded individuality, but into a patient work slave, professional automaton, tax-paying citizen, or righteous moralist. If one, nevertheless, meets with real spontaneity (which, by the way, is a rare treat,) it is not due to our method of rearing or educating the child: the personality often asserts itself, regardless of official and family barriers. Such a discovery should be celebrated as an unusual event, since the obstacles placed in the way of growth and development of character are so numerous that it must be considered a miracle if it retains its strength and beauty and survives the various attempts at crippling that which is most essential to it.";
static const unsigned char data2[] = "The terrible struggle of the thinking man and woman against political, social and moral conventions owes its origin to the family, where the child is ever compelled to battle against the internal and external use of force. The categorical imperatives: You shall! you must! this is right! that is wrong! this is true! that is false! shower like a violent rain upon the unsophisticated head of the young being and impress upon its sensibilities that it has to bow before the long established and hard notions of thoughts and emotions. Yet the latent qualities and instincts seek to assert their own peculiar methods of seeking the foundation of things, of distinguishing between what is commonly called wrong, true or false. It is bent upon going its own way, since it is composed of the same nerves, muscles and blood, even as those who assume to direct its destiny. I fail to understand how parents hope that their children will ever grow up into independent, self-reliant spirits, when they strain every effort to abridge and curtail the various activities of their children, the plus in quality and character, which differentiates their offspring from themselves, and by the virtue of which they are eminently equipped carriers of new, invigorating ideas. A young delicate tree, that is being clipped and cut by the gardener in order to give it an artificial form, will never reach the majestic height and the beauty as when allowed to grow in nature and freedom.";

int main() {
    Tlsh tlsh1;
    char tlsh1_hexdigest[TLSH_STRING_BUFFER_LEN];
    Tlsh tlsh2;

    tlsh1.update(data1, sizeof(data1));
    tlsh1.final();
    tlsh1.getHash(tlsh1_hexdigest, TLSH_STRING_BUFFER_LEN);
    std::cout << "tlsh1: " << tlsh1_hexdigest << std::endl;
    tlsh2.update(data2, sizeof(data2));
    tlsh2.final();
    std::cout << "tlsh2: " << tlsh2.getHash() << std::endl;
    std::cout << "distance score: " << tlsh1.totalDiff(&tlsh2) << std::endl;
}
EOF

g++ -Wall -Werror -o tlshtest tlshtest.cxx -ltlsh
echo "build: OK"
./tlshtest
echo "run: OK"
