#!/bin/ch

/*********************************************************************** 
 
========================================================================

                      Ch Package Creator Script
                          Stephen Nestinger
			  January 28th 2004

========================================================================

   createpkg.ch -- Create Ch package

   Usage:  ch  ./createpkg.ch package_name version
   Description: This script creates a Ch package

***********************************************************************/

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

int main(int argc, char* argv[])
{
   if(argc < 3 || argc > 3)
   {
      printf("\nUsage: createpkg.ch \"PACKAGE_NAME\" \"VERSION\"\n\n");
      exit(-1);
   }
   
   string_t str_package_path = argv[1];
   string_t str_temp;
   string_t str_package;

   foreach(str_temp;str_package_path;"\0" ; "/")
   {
       if(str_temp == NULL) 
          break;
       str_package = str_temp;
   }
   strncpy(str_package_path, str_package_path, strlen(str_package_path)-strlen(str_package));  

   string_t str_version = argv[2];
   string_t str_package_ver = stradd( str_package, "-", str_version );
   string_t str_package_ver_tar = stradd( str_package_ver, ".tar");
   string_t str_package_ver_tgz = stradd( str_package_ver_tar, ".gz");
   string_t str_package_bin = stradd( str_package,".bin" );
   string_t str_package_tar = stradd( str_package,".tar" );
   string_t str_package_tgz = stradd( str_package_tar,".gz" );
   string_t str_cmd;
   string_t str_chpkg_dir = "package";
   string_t str_installpkg_dir; 
   string_t str_chhome; 
   char postinstall_file = 0;
   char answer = 'n';
   FILE *fptr_readme;

   
   if(access(stradd(str_package_path,str_package), R_OK))
   {
	 printf("\nERROR: Package %s does not exist.\n\n", stradd(str_package_path,str_package));
	 exit(-1);
   } 

   printf("\n\n--------------------------------------------------------\n");
   printf("Creating Ch Package %s...\n", str_package_ver_tgz);

   if(!access(stradd(str_package_path,str_package_ver_tgz), R_OK))
   {
	  printf("\n%s already exists. Overwrite? (Y or N): ", stradd(str_package_path,str_package_ver_tgz) );
	  fflush(_stdin);
	  answer = getchar();
	  if( answer != 'y' && answer != 'Y' )
          {
             printf("\nExiting Createpkg...\n");
             printf("--------------------------------------------------------\n\n");
             exit(0);
          }
          while(getchar() != '\n');
	  rm -f $str_package_ver_tgz
   }

   if(!access(str_chpkg_dir, R_OK))
   {
	  printf("\n%s directory already exists. Overwrite? (Y or N): ", str_chpkg_dir);
	  answer = getchar();
	  if( answer != 'y' && answer != 'Y' )
		 exit(0);
          while(getchar() != '\n');
	  rm -rf $str_chpkg_dir
   }
   mkdir $str_chpkg_dir
   str_temp = stradd(str_package_path, str_package);
   cp -rf $str_temp $str_chpkg_dir
   
   str_temp = stradd(str_chpkg_dir, "/", str_package,  "/postinstall.ch");
   if(!access(str_temp, R_OK))
   {
      postinstall_file = 1; 
   }

   tar -cf $str_package_tar $str_chpkg_dir
   gzip $str_package_tar 

   rm -rf $str_chpkg_dir

   if(!access(str_package_ver, R_OK))
   {
	   rm -rf $str_package_ver
   }

   mkdir $str_package_ver

   str_chhome = getenv("CHHOME");
   str_installpkg_dir = stradd(str_chhome, "/bin/installpkg.ch"); 
   // The following has been removed since Ch 4.7, 
   // it comes with installpkg.ch
   /*
   if(!access(str_installpkg_dir, R_OK))
   {
      cp -f $str_installpkg_dir $str_package_ver
   }
   else
   {
	   fprintf(stderr, "\nInstallpkg was not found in %s\n");
	   exit(-1);
   }
   */
   
   if(!access(stradd(str_package_path, str_package, "/LICENSE"), R_OK))
   {
      str_temp = stradd(str_package_path, str_package, "/LICENSE");
	  cp -f $str_temp $str_package_ver
   }
   else if(!access(stradd(str_package_path, str_package, "/LICENSE.TXT"),R_OK))
   {
      str_temp = stradd(str_package_path, str_package, "/LICENSE.TXT");
	  cp -f $str_temp $str_package_ver
   }
   

   mv -f $str_package_tgz $str_package_bin
   mv -f $str_package_bin $str_package_ver
 
   if( (fptr_readme = fopen(stradd("./", str_package_ver,"/","README.TXT"), "w")) == 0 )
   {
	   printf("ERROR: fopen: Unable to open README.TXT file for writing.\n\n");
	   exit(-1);
   }

   fprintf(fptr_readme, 
      "Installation:\n"
      "(1) The Ch language environment must be installed first before\n"
      "    this package can be installed.\n"
      "(2) Install this Ch package with the following commands:\n\n"
      "    gzip -dc %s | tar -xvof -\n"
      "    ch installpkg.ch %s\n\n", str_package_ver_tgz, str_package);
   if(postinstall_file == 1)
   {
      fprintf(fptr_readme, 
      "(3) Post install with the following command:\n\n"
      "    %s/postinstall.ch\n", stradd(str_chhome, "/package/", str_package));
   } 
   fprintf(fptr_readme, 
      "\nUninstall:\n"
      "    To uninstall the package, run the following command:\n\n"
      "    ch installpkg.ch -u %s\n"
	  "\n\nPost Installation:\n"
	  "    Please go to the directory:\n"
	  "    %s\n"
	  "    where CHHOME is the directory, such as C:/Ch or /usr/local/ch,\n"
	  "    where Ch was installed and read the\n"
	  "    %s\n"
	  "    file before running any demos in demos directory\n"
	  "    %s.\n"
	  "    You may be required to set up some environment variables.\n"
		,str_package, 
		stradd("CHHOME/package/", str_package), 
		stradd("CHHOME/package/", str_package, "/README.TXT"), 
		stradd("CHHOME/package/", str_package, "/demos" )); 

   if( fclose(fptr_readme) )
   {
	   printf("ERROR: fopen: Unable to close README.TXT file for writing.\n\n");
	   exit(-1);
   }

   cp ./$str_package_ver/README.TXT ./$str_package_ver/README
   tar -cf $str_package_ver_tar $str_package_ver
   gzip $str_package_ver_tar

   if(strlen(str_package_path) > 2)
   {
      mv -f $str_package_ver_tgz $str_package_path 
   } 

   rm -rf $str_package_ver
   
   printf("\nCreate Ch Package %s Complete.\n", str_package_ver_tgz);
   printf("--------------------------------------------------------\n\n");
   return 0;
}
