#!/bin/ch
#include <unistd.h>   // for access() 
#include <dlfcn.h>    // for dlopen() 
#include <chshell.h>  // for chinfo() 

int addChdlHeader(char *headerfile, char *macro, char *chdlheader);

string_t pkgname="chsample";// define package name
chinfo_t info;              // for Ch version number
string_t cwd = _cwd;        // the current working directory
string_t debugFile;         // compilation debug information in Windows
string_t debug;
string_t makecmd;
#if defined(_WIN32_)
    debug=">nul 2>nul";         // surpress messages during cleaning  in Windows
    debugFile =">logfile 2>&1"; // compilation debug information in 'logfile'
    makecmd = "nmake -f Makefile.win";
#else
    makecmd = "make -f Makefile";
#endif

//make sure pgkcreate.ch is run from the current working directory
if(access("pkgcreate.ch", R_OK)) {
    echo Run ./pkgcreate.ch in the current directory.
    exit(-1);
}
// run this script in proper Ch version
chinfo(&info);
if ((info.vermajor*100+ info.verminor*10 +  info.vermicro) < 501) {
   echo "To run this script, you need to install Ch version 5.0.1.12201 or higher"
   echo "You can download the latest version from http://www.softintegration.com/download"
   exit(-1);
}

//echo clean up existing directory and create new ones
if (!access(pkgname, F_OK))
  rm -rf $pkgname
mkdir $pkgname 
mkdir $pkgname/dl $pkgname/lib $pkgname/include $pkgname/demos $pkgname/bin

//echo copying demo programs ...
cp -rf demos/* $pkgname/demos

//echo copying and modify header files ...
cp include/sample.h $pkgname/include
char *macro ="#define SAMPLE_H";
char *chdlheader =
   "\n\n"
   "#ifdef _CH_\n"
   "#pragma package <chsample> \n"
   "#include <chdl.h> \n"
   "LOAD_CHDL(sample);\n"
   "#endif \n\n";
addChdlHeader(stradd(pkgname, "/include/sample.h"), macro, chdlheader);

//echo extracting function prototypes ... 
processhfile("EXPORT", 0,  ";",  stradd(pkgname, "/include/sample.h"), "chfcreate/sample.h", NULL);
//echo removing special functions
removeFuncProto("chfcreate/sample.h", "func3", 0);
//echo generating sample_chdl.c in c/sample and *.chf in $pkgname/lib
c2chf chfcreate/sample.h -h _Chsample_handle -r chfcreate/sample_err -o c c -o chf $pkgname/lib 
//echo patching special .chf files
cp -f chfhandmade/*.chf $pkgname/lib 
chmod 644 $pkgname/lib/*.chf

//echo making .dl files
cd c
$makecmd clean $debug
$makecmd $debugFile

cd $cwd
//echo .DLL path and testing .dl file 
//#pragma exec _path=stradd(_path, "chsample/bin");
if (dlopen("./c/libsample.dl", RTLD_LAZY) == NULL) {
    printf("Error: test of loading libsample.dl: %s\n", dlerror());
    exit(-1);
}
cd c
mv -f libsample.dl $cwd/$pkgname/dl
$makecmd clean $debug
echo package $pkgname created successfully!

/*******************************************************************
 * * This function will add the required preprocessing directives
 * * needed by Ch to a header file.
 * * headerfile:   header file to be changed
 * * macro:        token to be recoganized for insertion of chdlheader
 * * chdlheader:   preprocessing directives to be inserted
 * *******************************************************************/
int addChdlHeader(char *headerfile, char *macro, char *chdlheader)
{
   char *stop_ptr;
   string_t tempfile, token;
   string_t result;
   FILE *stream;

   tempfile = ``cat $headerfile``;
   token = strstr( tempfile, macro);
   stop_ptr = strstr(token, "\n");
   strncpy(result, tempfile, strlen(tempfile)-strlen(stop_ptr));
   result=stradd(result, chdlheader);
   result=stradd(result, stop_ptr);
   if(!(stream = fopen("_tmpfile", "w")))
   {
      perror("_tmpfile");
      return -1;
   }
   fprintf(stream, "%s", result);
   fclose(stream);
   mv -f _tmpfile $(headerfile)
   return 0;
}
