1*2264Sjacobs /*
2*2264Sjacobs  * CDDL HEADER START
3*2264Sjacobs  *
4*2264Sjacobs  * The contents of this file are subject to the terms of the
5*2264Sjacobs  * Common Development and Distribution License (the "License").
6*2264Sjacobs  * You may not use this file except in compliance with the License.
7*2264Sjacobs  *
8*2264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*2264Sjacobs  * or http://www.opensolaris.org/os/licensing.
10*2264Sjacobs  * See the License for the specific language governing permissions
11*2264Sjacobs  * and limitations under the License.
12*2264Sjacobs  *
13*2264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14*2264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*2264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16*2264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17*2264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18*2264Sjacobs  *
19*2264Sjacobs  * CDDL HEADER END
20*2264Sjacobs  */
21*2264Sjacobs 
22*2264Sjacobs /*
23*2264Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*2264Sjacobs  * Use is subject to license terms.
25*2264Sjacobs  *
26*2264Sjacobs  */
27*2264Sjacobs 
28*2264Sjacobs /* $Id: lpmove.c 146 2006-03-24 00:26:54Z njacobs $ */
29*2264Sjacobs 
30*2264Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*2264Sjacobs 
32*2264Sjacobs #include <stdio.h>
33*2264Sjacobs #include <stdlib.h>
34*2264Sjacobs #include <unistd.h>
35*2264Sjacobs #include <string.h>
36*2264Sjacobs #include <locale.h>
37*2264Sjacobs #include <libintl.h>
38*2264Sjacobs #include <papi.h>
39*2264Sjacobs #include "common.h"
40*2264Sjacobs 
41*2264Sjacobs static void
42*2264Sjacobs usage(char *program)
43*2264Sjacobs {
44*2264Sjacobs 	char *name;
45*2264Sjacobs 
46*2264Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
47*2264Sjacobs 		name = program;
48*2264Sjacobs 	else
49*2264Sjacobs 		name++;
50*2264Sjacobs 
51*2264Sjacobs 	fprintf(stdout,
52*2264Sjacobs 		gettext("Usage: %s [request-id] (destination)\n"
53*2264Sjacobs 			"       %s (source) (destination)\n"), name);
54*2264Sjacobs 	exit(1);
55*2264Sjacobs }
56*2264Sjacobs 
57*2264Sjacobs static int
58*2264Sjacobs move_job(papi_service_t svc, char *src, int32_t id, char *dest)
59*2264Sjacobs {
60*2264Sjacobs 	int result = 0;
61*2264Sjacobs 	papi_status_t status;
62*2264Sjacobs 	char *mesg = gettext("moved");
63*2264Sjacobs 
64*2264Sjacobs 	status = papiJobMove(svc, src, id, dest);
65*2264Sjacobs 	if (status != PAPI_OK) {
66*2264Sjacobs 		mesg = (char *)verbose_papi_message(svc, status);
67*2264Sjacobs 		result = -1;
68*2264Sjacobs 	}
69*2264Sjacobs 	fprintf(stderr, gettext("%s-%d to %s: %s\n"), src, id, dest, mesg);
70*2264Sjacobs 
71*2264Sjacobs 	return (result);
72*2264Sjacobs }
73*2264Sjacobs 
74*2264Sjacobs int
75*2264Sjacobs main(int ac, char *av[])
76*2264Sjacobs {
77*2264Sjacobs 	int exit_code = 0;
78*2264Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
79*2264Sjacobs 	char *destination = NULL;
80*2264Sjacobs 	int c;
81*2264Sjacobs 
82*2264Sjacobs 	(void) setlocale(LC_ALL, "");
83*2264Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
84*2264Sjacobs 
85*2264Sjacobs 	while ((c = getopt(ac, av, "E:")) != EOF)
86*2264Sjacobs 		switch (c) {
87*2264Sjacobs 		case 'E':
88*2264Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
89*2264Sjacobs 			break;
90*2264Sjacobs 		default:
91*2264Sjacobs 			usage(av[0]);
92*2264Sjacobs 		}
93*2264Sjacobs 
94*2264Sjacobs 	if (optind >= ac - 1)
95*2264Sjacobs 		usage(av[0]);
96*2264Sjacobs 
97*2264Sjacobs 	destination = av[--ac];
98*2264Sjacobs 
99*2264Sjacobs 	for (c = optind; c < ac; c++) {
100*2264Sjacobs 		papi_status_t status;
101*2264Sjacobs 		papi_service_t svc = NULL;
102*2264Sjacobs 		papi_job_t *jobs = NULL;
103*2264Sjacobs 		char *printer = NULL;
104*2264Sjacobs 		int32_t id = -1;
105*2264Sjacobs 
106*2264Sjacobs 		(void) get_printer_id(av[c], &printer, &id);
107*2264Sjacobs 
108*2264Sjacobs 		status = papiServiceCreate(&svc, printer, NULL, NULL,
109*2264Sjacobs 					cli_auth_callback, encryption, NULL);
110*2264Sjacobs 		if (status != PAPI_OK) {
111*2264Sjacobs 			fprintf(stderr, gettext(
112*2264Sjacobs 				"Failed to contact service for %s: %s\n"),
113*2264Sjacobs 				printer, verbose_papi_message(svc, status));
114*2264Sjacobs 			exit(1);
115*2264Sjacobs 		}
116*2264Sjacobs 
117*2264Sjacobs 		if (id != -1) {	/* it's a job */
118*2264Sjacobs 			if (move_job(svc, printer, id, destination) < 0)
119*2264Sjacobs 				exit_code = 1;
120*2264Sjacobs 		} else {	/* it's a printer */
121*2264Sjacobs 			char message[128];
122*2264Sjacobs 			int count = 0;
123*2264Sjacobs 
124*2264Sjacobs 			snprintf(message, sizeof (message), "moved jobs to %s",
125*2264Sjacobs 					destination);
126*2264Sjacobs 			status = papiPrinterDisable(svc, printer, message);
127*2264Sjacobs 			if (status != PAPI_OK) {
128*2264Sjacobs 				fprintf(stderr, gettext("Disable %s: %s\n"),
129*2264Sjacobs 					printer,
130*2264Sjacobs 					verbose_papi_message(svc, status));
131*2264Sjacobs 				exit_code = 1;
132*2264Sjacobs 			} else
133*2264Sjacobs 				printf(gettext(
134*2264Sjacobs 				"destination %s is not accepting requests\n"),
135*2264Sjacobs 					printer);
136*2264Sjacobs 
137*2264Sjacobs 			status = papiPrinterListJobs(svc, printer, NULL,
138*2264Sjacobs 							0, 0, &jobs);
139*2264Sjacobs 			if (status != PAPI_OK) {
140*2264Sjacobs 				fprintf(stderr, gettext("Jobs %s: %s\n"),
141*2264Sjacobs 					printer,
142*2264Sjacobs 					verbose_papi_message(svc, status));
143*2264Sjacobs 				exit_code = 1;
144*2264Sjacobs 			}
145*2264Sjacobs 
146*2264Sjacobs 			printf(gettext("move in progress ...\n"));
147*2264Sjacobs 			while ((jobs != NULL) && (*jobs != NULL)) {
148*2264Sjacobs 				id = papiJobGetId(*jobs++);
149*2264Sjacobs 				if (move_job(svc, printer, id, destination) < 0)
150*2264Sjacobs 					exit_code = 1;
151*2264Sjacobs 				else
152*2264Sjacobs 					count++;
153*2264Sjacobs 			}
154*2264Sjacobs 			printf(gettext(
155*2264Sjacobs 			    "total of %d requests moved from %s to %s\n"),
156*2264Sjacobs 			    count, printer, destination);
157*2264Sjacobs 
158*2264Sjacobs 			papiJobListFree(jobs);
159*2264Sjacobs 		}
160*2264Sjacobs 
161*2264Sjacobs 		papiServiceDestroy(svc);
162*2264Sjacobs 	}
163*2264Sjacobs 
164*2264Sjacobs 	return (exit_code);
165*2264Sjacobs }
166