#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <valtools.h>
#include <locale.h>
#include <libintl.h>
#include <pkginfo.h>
#include "install.h"
#include <pkglib.h>
#include "libadm.h"
#include "libinst.h"
#include "pkginstall.h"
#include "messages.h"
extern struct admin adm;
extern char *pkgarch, *pkgvers, *msgtext, *pkgabrv;
extern int maxinst;
static char newinst[PKGSIZ];
static char *nextinst(void);
static char *prompt(struct pkginfo *info, int npkgs);
static int same_pkg;
char *
getinst(int *updatingExisting, struct pkginfo *info, int npkgs,
boolean_t a_preinstallCheck)
{
char *inst;
char *sameinst;
int i;
int nsamearch;
int samearch;
same_pkg = 0;
if (npkgs == 0) {
return (pkgabrv);
}
if (ADM(instance, "newonly") || ADM(instance, "quit")) {
msgtext = MSG_NEWONLY;
if (a_preinstallCheck == B_FALSE) {
ptext(stderr, msgtext, pkgabrv);
} else {
(void) fprintf(stdout, "install-new-only=true\n");
(void) fprintf(stdout, "ckinstance=4\n");
}
quit(4);
}
samearch = nsamearch = 0;
sameinst = NULL;
for (i = 0; i < npkgs; i++) {
if (strcmp(info[i].arch, pkgarch) == 0) {
samearch = i;
nsamearch++;
if (strcmp(info[i].version, pkgvers) == 0) {
sameinst = info[i].pkginst;
}
}
}
if (sameinst) {
if (a_preinstallCheck == B_FALSE) {
ptext(stderr, MSG_SAME);
} else {
(void) fprintf(stdout, "install-same-instance=true\n");
(void) fprintf(stdout, "ckinstance=0\n");
}
inst = sameinst;
same_pkg++;
(*updatingExisting)++;
return (inst);
}
if (ADM(instance, "overwrite")) {
if (npkgs == 1) {
samearch = 0;
} else if (nsamearch != 1) {
msgtext = MSG_OVERWRITE;
if (a_preinstallCheck == B_FALSE) {
ptext(stderr, msgtext);
} else {
(void) fprintf(stdout,
"install-ovewrite=true\n");
(void) fprintf(stdout, "ckinstance=4\n");
}
quit(4);
}
inst = info[samearch].pkginst;
(*updatingExisting)++;
return (inst);
}
if (ADM(instance, "unique")) {
if (maxinst <= npkgs) {
msgtext = MSG_UNIQ1;
if (a_preinstallCheck == B_FALSE) {
ptext(stderr, msgtext, pkgabrv);
} else {
(void) fprintf(stdout,
"install-too-many-instances=true\n");
(void) fprintf(stdout, "ckinstance=4\n");
}
quit(4);
}
inst = nextinst();
return (inst);
}
if (a_preinstallCheck == B_FALSE) {
if (echoGetFlag() == B_FALSE) {
msgtext = MSG_NOINTERACT;
ptext(stderr, msgtext);
quit(5);
}
} else {
(void) fprintf(stdout, "install-new-instance=true\n");
(void) fprintf(stdout, "ckinstance=1\n");
}
inst = prompt(info, npkgs);
if (strcmp(inst, "new") == 0) {
inst = nextinst();
return (inst);
}
(*updatingExisting)++;
return (inst);
}
int
is_samepkg(void) {
return (same_pkg);
}
static char *
nextinst(void)
{
struct pkginfo info;
int n;
n = 2;
info.pkginst = NULL;
(void) strcpy(newinst, pkgabrv);
while (pkginfo(&info, newinst, NULL, NULL) == 0) {
(void) snprintf(newinst, sizeof (newinst),
"%s.%d", pkgabrv, n++);
}
return (newinst);
}
static char *
prompt(struct pkginfo *info, int npkgs)
{
CKMENU *menup;
char *inst;
char ans[MAX_INPUT];
char header[256];
char temp[256];
int i;
int n;
if (maxinst > npkgs) {
n = ckyorn(ans, NULL, NULL, MSG_GETINST_HELP1,
MSG_GETINST_PROMPT1);
if (n != 0) {
quit(n);
}
if (strchr("yY", *ans) != NULL) {
return ("new");
}
}
(void) snprintf(header, sizeof (header), MSG_GETINST_HEADER, pkgabrv);
menup = allocmenu(header, CKALPHA);
for (i = 0; i < npkgs; i++) {
(void) snprintf(temp, sizeof (temp),
"%s %s\n(%s) %s", info[i].pkginst,
info[i].name, info[i].arch, info[i].version);
if (setitem(menup, temp)) {
progerr("no memory");
quit(99);
}
}
if (npkgs == 1) {
printmenu(menup);
if (n = ckyorn(ans, NULL, NULL, NULL, MSG_GETINST_PROMPT0))
quit(n);
if (strchr("yY", *ans) == NULL)
quit(3);
(void) strcpy(newinst, info[0].pkginst);
} else {
if (n = ckitem(menup, &inst, 1, NULL, NULL, MSG_GETINST_HELP2,
MSG_GETINST_PROMPT2))
quit(n);
(void) strcpy(newinst, inst);
}
(void) setitem(menup, 0);
free(menup);
return (newinst);
}