
#include <stdio.h>
#if defined(__EMX__)
#include <stdlib.h>
#endif
#include <string.h>
#define  INCL_WIN
#define  INCL_WINHELP
#include <os2.h>


#define RC5DES_FOLDOBJ_NAME "<RC5DES-CLI>"
#define RC5DES_FULLNAME     "RC5DES Client for OS/2"

int os2CliUninstallClient(int quiet)
{
  HOBJECT hObject;
  ULONG mbflag = MB_ERROR,
        WindowId = 0;
  char  *msg;

  hObject = WinQueryObject((PSZ)RC5DES_FOLDOBJ_NAME);

  if (hObject == NULLHANDLE)
    msg = "The RC5DES client was not found in the Startup Folder.\n";
  else if (WinDestroyObject(hObject) != TRUE )
    msg = "The RC5DES client could not be removed from in the Startup Folder.";
  else
    {
    msg = "The RC5DES client was successfully removed from the Startup Folder.";
    mbflag = 0;
    }

  if (!quiet)
    WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, (PSZ)msg, (PSZ)RC5DES_FULLNAME,
                 WindowId , MB_OK | mbflag );
  return 0;
}

// from common/console.cpp (also initialized there):
static struct
{
  int initlevel;
  int runhidden;
  int conisatty;
  int doingmodes;
} constatics;


int os2CliInstallClient(int quiet)
{

  #define PSZSTRINGSIZE 4068
  int rc;
  #if defined(__EMX__)
  int   posexec;
  char * endname, *pospath;
  #else
  const  int len = PSZSTRINGSIZE;
  char exepath[CCHMAXPATH];
  char* exename = "rc5des.exe";
  char* drive = "C:";
  ULONG curdisk, alldisks, pathlen = CCHMAXPATH;
  #endif
  ULONG  ulFlags = 0;
  char   pszClassName[] = "WPProgram",
         pszTitle[] = "RC5-DES Cracking Client",
         pszLocation[] = "<WP_START>";    // Startup Folder
  char pszSetupString[PSZSTRINGSIZE] = "OBJECTID=" RC5DES_FOLDOBJ_NAME ";"
                               "MINIMIZED=YES;"
                               "PROGTYPE=WINDOWABLEVIO;";
  char *msg = "The RC5DES client is already installed in the startup folder.";
  ULONG mbflag = MB_ERROR;

  HOBJECT hObject = WinQueryObject((char *)RC5DES_FOLDOBJ_NAME);
  if (hObject == NULLHANDLE)
    {
    // Add full path of the program
    strcat(pszSetupString, "EXENAME=");
    #if defined(__EMX__)
    if (constatics.runhidden == 1)   // Run detached
    #else
    if (quiet == 1)   // Run detached
    #endif
      {
      strcat(pszSetupString, "CMD.EXE;");     // command processor
      strcat(pszSetupString, "PARAMETERS=/c detach ");   // detach
      }

    // Add exepath and exename
    #if !defined(__EMX__)
    rc = DosQueryCurrentDisk(&curdisk, &alldisks);
    drive[0] = 'A'+curdisk;
    drive[1] = ':';
    drive[2] = 0;
    strcat(pszSetupString, drive);
    rc = DosQueryCurrentDir(0, exepath, &pathlen);
    if (exepath[0] != '\\')
       {
       strcat(pszSetupString, "\\");
       }
    strcat(pszSetupString, exepath);
    if (pszSetupString[strlen(pszSetupString)-1] != '\\')
       {
       strcat(pszSetupString, "\\");
       }
    strcat(pszSetupString, exename);
    strcat(pszSetupString, ";");

    // Add on Working Directory
    strcat(pszSetupString, "STARTUPDIR=");
    strcat(pszSetupString, drive);
    strcat(pszSetupString, exepath);
    strcat(pszSetupString, ";");
    #else       // EMX
    posexec = strlen(pszSetupString);
    _execname( pszSetupString+strlen(pszSetupString),
                PSZSTRINGSIZE-strlen(pszSetupString) );

    endname = _getname(pszSetupString+posexec);
    strncat(pszSetupString, ";STARTUPDIR=", PSZSTRINGSIZE);
    pospath=pszSetupString+strlen(pszSetupString);
    memcpy(pospath, pszSetupString+posexec,
                        endname-pszSetupString-posexec);
    *(pospath+(endname-pszSetupString-posexec))= '\0';

    #endif

    rc = WinCreateObject(pszClassName, pszTitle, (PSZ)pszSetupString,
               pszLocation, ulFlags);
    if (rc == NULLHANDLE)
      msg = "The RC5DES client could not be added to your Startup Folder\n";
    else
      {
      msg = "THE RC5DES client has been added to your Startup Folder\n";
      mbflag = 0;
      }
    }

  if (!quiet)
    WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, (PSZ)msg, (PSZ)RC5DES_FULLNAME,
                0, MB_OK | mbflag );
  return 0;
}
