12264Sjacobs /*
22264Sjacobs  * CDDL HEADER START
32264Sjacobs  *
42264Sjacobs  * The contents of this file are subject to the terms of the
52264Sjacobs  * Common Development and Distribution License (the "License").
62264Sjacobs  * You may not use this file except in compliance with the License.
72264Sjacobs  *
82264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92264Sjacobs  * or http://www.opensolaris.org/os/licensing.
102264Sjacobs  * See the License for the specific language governing permissions
112264Sjacobs  * and limitations under the License.
122264Sjacobs  *
132264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
142264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
162264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
172264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
182264Sjacobs  *
192264Sjacobs  * CDDL HEADER END
202264Sjacobs  */
212264Sjacobs 
222264Sjacobs /*
23*9116SNagaraj.Yedathore@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
242264Sjacobs  * Use is subject to license terms.
252264Sjacobs  *
262264Sjacobs  */
272264Sjacobs 
282264Sjacobs /* $Id: lpmove.c 146 2006-03-24 00:26:54Z njacobs $ */
292264Sjacobs 
302264Sjacobs #include <stdio.h>
312264Sjacobs #include <stdlib.h>
322264Sjacobs #include <unistd.h>
332264Sjacobs #include <string.h>
342264Sjacobs #include <locale.h>
352264Sjacobs #include <libintl.h>
362264Sjacobs #include <papi.h>
372264Sjacobs #include "common.h"
382264Sjacobs 
392264Sjacobs static void
402264Sjacobs usage(char *program)
412264Sjacobs {
422264Sjacobs 	char *name;
432264Sjacobs 
442264Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
452264Sjacobs 		name = program;
462264Sjacobs 	else
472264Sjacobs 		name++;
482264Sjacobs 
492264Sjacobs 	fprintf(stdout,
50*9116SNagaraj.Yedathore@Sun.COM 	    gettext("Usage: %s [request-id] (destination)\n"
51*9116SNagaraj.Yedathore@Sun.COM 	    "       %s (source) (destination)\n"), name, name);
522264Sjacobs 	exit(1);
532264Sjacobs }
542264Sjacobs 
552264Sjacobs static int
562264Sjacobs move_job(papi_service_t svc, char *src, int32_t id, char *dest)
572264Sjacobs {
582264Sjacobs 	int result = 0;
592264Sjacobs 	papi_status_t status;
602264Sjacobs 	char *mesg = gettext("moved");
612264Sjacobs 
622264Sjacobs 	status = papiJobMove(svc, src, id, dest);
632264Sjacobs 	if (status != PAPI_OK) {
642264Sjacobs 		mesg = (char *)verbose_papi_message(svc, status);
652264Sjacobs 		result = -1;
662264Sjacobs 	}
672264Sjacobs 	fprintf(stderr, gettext("%s-%d to %s: %s\n"), src, id, dest, mesg);
682264Sjacobs 
692264Sjacobs 	return (result);
702264Sjacobs }
712264Sjacobs 
722264Sjacobs int
732264Sjacobs main(int ac, char *av[])
742264Sjacobs {
752264Sjacobs 	int exit_code = 0;
762264Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
772264Sjacobs 	char *destination = NULL;
782264Sjacobs 	int c;
792264Sjacobs 
802264Sjacobs 	(void) setlocale(LC_ALL, "");
812264Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
822264Sjacobs 
832264Sjacobs 	while ((c = getopt(ac, av, "E:")) != EOF)
842264Sjacobs 		switch (c) {
852264Sjacobs 		case 'E':
862264Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
872264Sjacobs 			break;
882264Sjacobs 		default:
892264Sjacobs 			usage(av[0]);
902264Sjacobs 		}
912264Sjacobs 
922264Sjacobs 	if (optind >= ac - 1)
932264Sjacobs 		usage(av[0]);
942264Sjacobs 
952264Sjacobs 	destination = av[--ac];
962264Sjacobs 
972264Sjacobs 	for (c = optind; c < ac; c++) {
982264Sjacobs 		papi_status_t status;
992264Sjacobs 		papi_service_t svc = NULL;
1002264Sjacobs 		papi_job_t *jobs = NULL;
1012264Sjacobs 		char *printer = NULL;
1022264Sjacobs 		int32_t id = -1;
1032264Sjacobs 
1042264Sjacobs 		(void) get_printer_id(av[c], &printer, &id);
1052264Sjacobs 
1062264Sjacobs 		status = papiServiceCreate(&svc, printer, NULL, NULL,
107*9116SNagaraj.Yedathore@Sun.COM 		    cli_auth_callback, encryption, NULL);
1082264Sjacobs 		if (status != PAPI_OK) {
1092264Sjacobs 			fprintf(stderr, gettext(
110*9116SNagaraj.Yedathore@Sun.COM 			    "Failed to contact service for %s: %s\n"),
111*9116SNagaraj.Yedathore@Sun.COM 			    printer, verbose_papi_message(svc, status));
1122264Sjacobs 			exit(1);
1132264Sjacobs 		}
1142264Sjacobs 
1152264Sjacobs 		if (id != -1) {	/* it's a job */
1162264Sjacobs 			if (move_job(svc, printer, id, destination) < 0)
1172264Sjacobs 				exit_code = 1;
1182264Sjacobs 		} else {	/* it's a printer */
1192264Sjacobs 			char message[128];
1202264Sjacobs 			int count = 0;
1212264Sjacobs 
1222264Sjacobs 			snprintf(message, sizeof (message), "moved jobs to %s",
123*9116SNagaraj.Yedathore@Sun.COM 			    destination);
1242264Sjacobs 			status = papiPrinterDisable(svc, printer, message);
1252264Sjacobs 			if (status != PAPI_OK) {
1262264Sjacobs 				fprintf(stderr, gettext("Disable %s: %s\n"),
127*9116SNagaraj.Yedathore@Sun.COM 				    printer, verbose_papi_message(svc, status));
1282264Sjacobs 				exit_code = 1;
1292264Sjacobs 			} else
1302264Sjacobs 				printf(gettext(
131*9116SNagaraj.Yedathore@Sun.COM 				    "destination %s is not accepting "
132*9116SNagaraj.Yedathore@Sun.COM 				    "requests\n"), printer);
1332264Sjacobs 
1342264Sjacobs 			status = papiPrinterListJobs(svc, printer, NULL,
135*9116SNagaraj.Yedathore@Sun.COM 			    0, 0, &jobs);
1362264Sjacobs 			if (status != PAPI_OK) {
1372264Sjacobs 				fprintf(stderr, gettext("Jobs %s: %s\n"),
138*9116SNagaraj.Yedathore@Sun.COM 				    printer, verbose_papi_message(svc, status));
1392264Sjacobs 				exit_code = 1;
1402264Sjacobs 			}
1412264Sjacobs 
1422264Sjacobs 			printf(gettext("move in progress ...\n"));
1432264Sjacobs 			while ((jobs != NULL) && (*jobs != NULL)) {
1442264Sjacobs 				id = papiJobGetId(*jobs++);
1452264Sjacobs 				if (move_job(svc, printer, id, destination) < 0)
1462264Sjacobs 					exit_code = 1;
1472264Sjacobs 				else
1482264Sjacobs 					count++;
1492264Sjacobs 			}
1502264Sjacobs 			printf(gettext(
1512264Sjacobs 			    "total of %d requests moved from %s to %s\n"),
1522264Sjacobs 			    count, printer, destination);
1532264Sjacobs 
1542264Sjacobs 			papiJobListFree(jobs);
1552264Sjacobs 		}
1562264Sjacobs 
1572264Sjacobs 		papiServiceDestroy(svc);
1582264Sjacobs 	}
1592264Sjacobs 
1602264Sjacobs 	return (exit_code);
1612264Sjacobs }
162