/*
 * tools/vgexport.c
 *
 * Copyright (C) 1997 - 2000  Heinz Mauelshagen, Sistina Software
 *
 * May 1997
 * May,June,September 1998
 * October 1999
 * February 2000
 *
 * LVM is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 * 
 * LVM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LVM; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA. 
 *
 */

/*
 * Changelog
 *
 *    02/05/1998 - don't make LV name change in LV structs to avoid
 *                 usage of lv_create_name() because of free logical volume
 *                 name support
 *    03/05/1998 - enhanced error checking with lvm_tab_vg_remove()
 *    16/05/1998 - added lvmtab checking
 *    10/06/1998 - added -a option
 *    27/06/1998 - changed lvm_tab_* calling convention
 *    05/09/1998 - corrected some messages
 *               - used lvm_tab_vg_check_exist() instead of vg_check_exist()
 *               - checked for volume group activity first for
 *                 better performance
 *    06/10/1999 - implemented support for long options
 *    15/02/2000 - use lvm_error()
 *
 */

#include <lvm_user.h>

char *cmd = NULL;

#ifdef DEBUG
int opt_d = 0;
#endif

int main ( int argc, char **argv) {
   int c = 0;
   int opt_a = 0;
   int opt_v = 0;
   int p = 0;
   int ret = 0;
   char *vg_name = NULL;
#ifdef DEBUG
   char *options = "adh?v";
#else
   char *options = "ah?v";
#endif
   struct option long_options[] = {
      { "all",     no_argument, NULL, 'a'},
#ifdef DEBUG
      { "debug",   no_argument, NULL, 'd'},
#endif
      { "help",    no_argument, NULL, 'h'},
      { "verbose", no_argument, NULL, 'v'},
      { NULL,      0,           NULL, 0},
   };
   char vg_name_exp[NAME_LEN] = { 0, };
   vg_t *vg = NULL;

   cmd = basename ( argv[0]);

   SUSER_CHECK;
   LVMTAB_CHECK;

   while ( ( c = getopt_long ( argc, argv, options,
                               long_options, NULL)) != EOF) {
      switch ( c) {

         case 'a':
            if ( opt_a > 0) {
               fprintf ( stderr, "%s -- a option already given\n\n", cmd);
               return LVM_EINVALID_CMD_LINE;
            }
            opt_a++;
            break;

#ifdef DEBUG
         case 'd':
            if ( opt_d > 0) {
               fprintf ( stderr, "%s -- d option already given\n\n", cmd);
               return LVM_EINVALID_CMD_LINE;
            }
            opt_d++;
            break;
#endif

         case 'h':
         case '?':
            printf ( "\n%s  (IOP %d)\n\n%s -- Volume Group Export\n\n"
                     "Synopsis:\n"
                     "---------\n\n"
                     "%s\n"
                     "\t[-a/--all]\n"
#ifdef DEBUG
                     "\t[-d/--debug]\n"
#endif
                     "\t[-h/-?/--help]\n"
                     "\t[-v/--verbose]\n"
                     "\tVolumeGroupName [VolumeGroupName...]\n\n",
                     lvm_version, LVM_LIB_IOP_VERSION,  cmd, cmd);
            return 0;
            break;

         case 'v':
            if ( opt_v > 0) {
               fprintf ( stderr, "%s -- v option already given\n\n", cmd);
               return LVM_EINVALID_CMD_LINE;
            }
            opt_v++;
            break;

         default:
            fprintf ( stderr, "%s -- invalid command line option \"%c\"\n\n",
                      cmd, c);
            return LVM_EINVALID_CMD_LINE;
      }
   }
  
   CMD_MINUS_CHK;
   LVM_CHECK_IOP;
   LVM_LOCK ( 0);

   if ( opt_a > 0) {
      argv = lvm_tab_vg_check_exist_all_vg ();
      argc = optind = 0;
      if ( argv != NULL) while ( argv[argc] != NULL) argc++;
   
      if ( optind == argc) {
         printf ( "%s -- no volume groups found\n\n", cmd);
         return LVM_EVGEXPORT_NO_VG;
      }
   } else if ( optind == argc) {
      fprintf ( stderr, "%s -- please give a volume group name\n\n", cmd);
      return LVM_EVGEXPORT_VG_MISSING;
   }


   for ( ; optind < argc; optind++) {
      vg_name = argv[optind];

      if ( opt_v > 0) printf ( "%s -- checking volume group name\n", cmd);
      if ( vg_check_name ( vg_name) < 0) {
         fprintf ( stderr, "%s -- invalid volume group name \"%s\"\n",
                   cmd, vg_name);
         continue;
      }
   
      if ( opt_v > 0) printf ( "%s -- checking volume group activity\n", cmd);
      if ( vg_check_active ( vg_name) == TRUE) {
         fprintf ( stderr, "%s -- can't export active volume group \"%s\"\n",
                   cmd, vg_name);
         continue;
      }

      if ( opt_v > 0) printf ( "%s -- checking volume group existence\n", cmd);
      if ( ( ret = lvm_tab_vg_check_exist ( vg_name, NULL)) != TRUE) {
         fprintf ( stderr, "%s -- volume group \"%s\" doesn't exist\n",
                   cmd, vg_name);
         continue;
      }

      sprintf ( vg_name_exp, "%s%s%c", vg_name, EXPORTED, 0);
      if ( ( ret = vg_check_exist ( vg_name_exp)) != TRUE) {
         if ( ret == -LVM_EVG_READ_VG_EXPORTED) {
            fprintf ( stderr, "%s -- a volume group named \"%s\" is "
                              "already exported\n",
                              cmd, vg_name);
            continue;
         }
      }

      if ( opt_v > 0) printf ( "%s -- reading data of volume group \"%s\" "
                               "from disk(s)\n", cmd, vg_name);
      if ( ( ret = lvm_tab_vg_read_with_pv_and_lv ( vg_name, &vg)) != 0) {
         fprintf ( stderr, "%s -- ERROR \"%s\" reading data of volume "
                           "group \"%s\"\n\n",
                           cmd, lvm_error ( ret), vg_name);
         continue;
      }

      if ( opt_v > 0) printf ( "%s -- checking for physical volumes in "
                               "volume group \"%s\"\n", cmd, vg_name);
      if ( vg->pv_cur == 0) {
         fprintf ( stderr, "%s -- no physical volumes in volume group \"%s\"\n",
                   cmd, vg_name);
         continue;
      }
   
      if ( opt_v > 0) printf ( "%s -- checking volume group \"%s\" "
                               "consistency\n", cmd, vg_name);
      if ( ( ret = vg_check_consistency_with_pv_and_lv ( vg)) != 0) {
         fprintf ( stderr, "%s -- ERROR \"%s\" volume group \"%s\" is "
                           "inconsistent\n\n",
                           cmd, lvm_error ( ret), vg_name);
         continue;
      }

      /* set my export marker */
      if ( opt_v > 0) printf ( "%s -- setting up volume group \"%s\" "
                               "for export\n",
                               cmd, vg_name);
      system_id_set_exported ( vg->pv[0]->system_id);
      strcat ( vg->pv[0]->vg_name, EXPORTED);

      /* correct it in the rest of the PVs */
      for ( p = 1; vg->pv[p] != NULL; p++) {
         strcpy ( vg->pv[p]->system_id, vg->pv[0]->system_id);
         strcpy ( vg->pv[p]->vg_name, vg->pv[0]->vg_name);
      }

      vg->vg_status |= VG_EXPORTED;
      strcpy ( vg->vg_name, vg->pv[0]->vg_name);

      if ( opt_v > 0) printf ( "%s -- exporting volume group \"%s\" with "
                               "%u physical and %u logical volumes\n",
                               cmd, vg_name, vg->pv_cur, vg->lv_cur);
   
      lvm_dont_interrupt ( 0);

      if ( ( ret = vg_write_with_pv_and_lv ( vg)) < 0) {
         fprintf ( stderr, "%s -- ERROR \"%s\" storing volume group "
                           "data on disk(s)\n",
                           cmd, lvm_error ( ret));
      }

      if ( opt_v > 0) printf ( "%s -- removing special files of volume "
                               "group \"%s\"\n",
                               cmd, vg_name);
      vg_remove_dir_and_group_and_nodes ( vg_name);
   
      if ( ( ret = lvm_tab_vg_remove ( vg_name)) < 0) {
         fprintf ( stderr, "%s -- ERROR \"%s\" removing volume group \"%s\" "
                           "from \"%s\"\n",
                           cmd, lvm_error ( ret), vg_name, LVMTAB);
         continue;
      }

      lvm_interrupt ();

      printf ( "%s -- volume group \"%s\"%s sucessfully exported\n",
               cmd, vg_name, ret < 0 ? " NOT" : "");
      if ( ret < 0) continue;

   }

   LVM_UNLOCK ( 0);

   printf ( "\n");
   return 0;
}
