/*
 * tools/vgcfgrestore.c
 *
 * Copyright (C) 1997 - 2000  Heinz Mauelshagen, Sistina Software
 *
 * June,October 1997
 * May 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
 *
 *    12/10/1997  - removed vg_check_exist
 *                - added pv_read() to check againts overwrite of
 *                  physical volume belonging to a different volume group
 *                - added check for physical volume size
 *    08/11/1997  - added message for malloc errors
 *                - moved restore code to lib/vg_cfgrestore.c to use
 *                  it with lvm_tab code
 *                - added -n option to support test runs
 *    02/05/1998 - enhanced error checking
 *    02/05/1998 - used pv_check_new() instead of str-function
 *    16/05/1998 - added lvmtab checking
 *    21/05/1998 - implemented option -o
 *    06/10/1999 - implemented support for long options
 *    12/02/2000 - avoided catch 22 situation by avoiding lvmtab check 
 *    15/02/2000 - use lvm_error()
 *    23/01/2001 - added call to lvm_init (JT)
 *    06/04/2001 - Tidy usage message and command-line options.
 *    09/04/2001 - Fix spelling of argument in error message
 *
 */

#include <lvm_user.h>

char *cmd = NULL;

#ifdef DEBUG
int opt_d = 0;
#endif

void usage ( int ret)
{
   FILE *stream = stderr;

   if ( ret == 0) {
      stream = stdout;
      printf ( "%s (IOP %d)\n\n", lvm_version, LVM_LIB_IOP_VERSION);
      printf ( "%s -- restore volume group data to a disk\n", cmd);
   }

   fprintf ( stream, "%s "
#ifdef DEBUG
             "[-d|--debug] "
#endif
             "[-f|--file VGConfPath] "
             "[-l[l]|--list [--list]]\n\t"
             "[-n|--name VolumeGroupName] "
             "[-h|--help]\n\t"
             "[-o|--oldpath OldPhysicalVolumePath] "
             "[-t|--test] "
             "[-v|--verbose]\n\t"
             "[--version] "
             "[PhysicalVolumePath]\n",
             cmd);
   exit ( ret);
}

int main ( int argc, char **argv)
{
   int c = 0;
   int opt_f = 0;
   int opt_l = 0;
   int opt_n = 0;
   int opt_o = 0;
   int opt_t = 0;
   int opt_v = 0;
   int index = -1;
   int pv_index = -1;
   int pv_index_old = -1;
   int ret = 0;
#ifdef DEBUG
   char *options = "df:h?ln:o:tv";
#else
   char *options = "f:h?ln:o:tv";
#endif
   struct option long_options[] = {
#ifdef DEBUG
      { "debug",      no_argument,       NULL, 'd'},
#endif
      { "file",       required_argument, NULL, 'f'},
      { "help",       no_argument,       NULL, 'h'},
      { "list",       no_argument,       NULL, 'l'},
      { "name",       required_argument, NULL, 'n'},
      { "oldpath",    required_argument, NULL, 'n'},
      { "test",       no_argument,       NULL, 't'},
      { "verbose",    no_argument,       NULL, 'v'},
      { "version",    no_argument,       NULL, 22},
      { NULL, 0, NULL, 0}
   };
   char *pv_name = NULL;
   char *pv_name_old = NULL;
   char *vg_name = NULL;
   char *vg_conf_path = NULL;
   char vg_backup_path[NAME_LEN] = { 0, };
   pv_t *pv = NULL;
   vg_t vg;
   vg_t vg_write;

   /* lvm_init(argc, argv); */
   cmd = basename ( argv[0]);

   memset ( &vg, 0, sizeof ( vg));
   memset ( &vg_write, 0, sizeof ( vg_write));

   SUSER_CHECK;

   while ( ( c = getopt_long ( argc, argv, options,
                               long_options, NULL)) != EOF) {
      switch ( c) {
#ifdef DEBUG
         case 'd':
            opt_d++;
            break;
#endif

         case 'f':
            vg_conf_path = optarg;
            opt_f++;
            break;

         case 'h':
            usage(0);

         case 'l':
            opt_l++;
            break;

         case 'n':
            vg_name = optarg;
            if ( vg_check_name ( vg_name) < 0) {
               fprintf ( stderr, "%s -- invalid volume group name \"%s\"\n",
                                 cmd, vg_name);
               usage ( LVM_EINVALID_CMD_LINE);
            }
            opt_n++;
            break;

         case 'o':
            pv_name_old = optarg;
            if ( pv_check_name ( pv_name_old) < 0) {
               fprintf ( stderr, "%s -- invalid physical volume path \"%s\"\n",
                                 cmd, pv_name_old);
               usage ( LVM_EINVALID_CMD_LINE);
            }
            opt_o++;
            break;

         case 't':
            opt_t++;
            break;

         case 'v':
            opt_v++;
            break;

         case 22:
            printf ("%s: %s (IOP %d)\n", cmd, lvm_version, LVM_LIB_IOP_VERSION);
            return 0;

         default:
            fprintf ( stderr, "%s -- invalid command line option \"%c\"\n\n",
                      cmd, c);
         case '?':
            usage ( LVM_EINVALID_CMD_LINE);
      }
   }

   /* not until all of the commands are converted
   CMD_MINUS_CHK;
   */
   if ( optind < argc && *argv[optind] == '-') {
      fprintf(stderr, "%s -- invalid argument %s\n\n", cmd, argv[optind]);
      usage(LVM_EINVALID_CMD_LINE);
   }

   if ( opt_f > 0 && opt_n == 0) {
      fprintf ( stderr, "%s -- please give option n with option f\n", cmd);
      usage ( LVM_EINVALID_CMD_LINE);
   }

   if ( opt_l == 0) {
      if ( optind != argc - 1) {
         printf ( "%s -- please enter a physical volume name\n", cmd);
         usage ( LVM_EVGCFGRESTORE_PV_MISSING);
      }
      pv_name = argv[optind];
      if ( pv_check_name ( pv_name) < 0) {
         fprintf ( stderr, "%s -- invalid physical volume \"%s\"\n",
                           cmd, pv_name);
         usage ( LVM_EVGCFGRESTORE_PV_CHECK_NAME);
      }
      if ( opt_f == 0 && opt_n == 0) {
         fprintf ( stderr, "%s -- please enter a volume group name or a "
                           "volume group backup\n", cmd);
         usage ( LVM_EINVALID_CMD_LINE);
      }
   }

   if ( opt_t == 0 && opt_l == 0 && vg_check_active ( vg_name) == TRUE) {
      fprintf ( stderr,
                "%s -- can't restore part of active volume group \"%s\"\n",
                        cmd, vg_name);
      usage ( LVM_EVGCFGRESTORE_VG_CHECK_ACTIVE);
   }

   if ( opt_f == 0)
      sprintf ( vg_backup_path, "%s/%s.conf", VG_BACKUP_DIR, vg_name);
   else strcpy ( vg_backup_path, vg_conf_path);


   LVM_CHECK_IOP;
   LVM_LOCK ( 0);
   lvm_dont_interrupt ( 0);

   if ( opt_v > 0) printf ( "%s -- restoring volume group \"%s\" from \"%s\"\n",
                            cmd, vg_name, vg_backup_path);
   if ( ( ret = vg_cfgrestore ( vg_name, vg_backup_path, opt_v, &vg)) < 0) {
      if ( ret == -LVM_EVG_CFGRESTORE_OPEN) {
         fprintf ( stderr,
                   "%s -- a backup for volume group %s is not available\n\n",
                   cmd, vg_name);
      } else {
         fprintf ( stderr,
                   "%s -- ERROR \"%s\" restoring volume group \"%s\"\n\n",
                   cmd, lvm_error ( ret), vg_name);
      }
      return LVM_EVGCFGRESTORE_VG_CFGRESTORE;
   }

   if ( opt_n == 0) {
      vg_name = vg.vg_name;
      printf ( "%s -- this is a backup of volume group \"%s\"\n", cmd, vg_name);
   } else if ( strcmp ( vg_name, vg.vg_name) != 0) {
      fprintf ( stderr, "%s -- this is not a backup of volume group \"%s\"\n\n",
                cmd, vg_name);
      return LVM_EVGCFGRESTORE_INVALID;
   }

   if ( opt_v > 0) printf ( "%s -- checking volume group consistency"
                            " of \"%s\"\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);
      return LVM_EVGCFGRESTORE_VG_CHECK_CONSISTENCY;
   } else if ( opt_t > 0) printf ( "%s -- backup of volume group \"%s\" "
                                   " is consistent\n",
                                   cmd, vg_name);

   pv_index = pv_index_old = -1;
   if ( opt_o > 0) {
      if ( pv_check_in_vg ( &vg, pv_name_old) == FALSE) {
         fprintf ( stderr, "%s -- physical volume \"%s\" doesn't belong "
                           "to volume group \"%s\" backup\n\n",
                           cmd, pv_name_old, vg_name);
         return LVM_EVGCFGRESTORE_PV_CHECK_IN_VG;
      } else pv_index_old = pv_get_index_by_name ( &vg, pv_name_old);
      if ( pv_check_in_vg ( &vg, pv_name) == TRUE) {
         fprintf ( stderr, "%s -- \"%s\" may not be an actual physical volume "
                           "of volume group \"%s\"\n\n", cmd, pv_name, vg_name);
         return LVM_EVGCFGRESTORE_PV_CHECK_IN_VG;
      } else pv_index = pv_get_index_by_name ( &vg, pv_name);
   } else {
      if ( pv_check_in_vg ( &vg, pv_name) == FALSE) {
         fprintf ( stderr, "%s -- physical volume \"%s\" doesn't belong "
                           "to volume group \"%s\"\n\n",
                           cmd, pv_name, vg_name);
         return LVM_EVGCFGRESTORE_PV_CHECK_IN_VG;
      } else pv_index = pv_get_index_by_name ( &vg, pv_name);
   }

   if ( opt_l > 0) {
      if ( opt_l == 1) vg_show ( &vg);
      else             vg_show_with_pv_and_lv ( &vg);
      printf ( "\n");
   } else {
      /* no test run */
      if ( opt_t == 0) {
         /* check given physical volume */
         if ( opt_v > 0) printf ( "%s -- reading physical volume \"%s\"\n",
                                  cmd, pv_name);
         if ( ( ret = pv_read ( pv_name, &pv, NULL)) < 0 &&
              ret != -LVM_EPV_READ_MD_DEVICE) {
            fprintf ( stderr, "%s -- ERROR \"%s\" reading physical "
                              "volume \"%s\"\n\n",
                              cmd, lvm_error ( ret), pv_name);
            return LVM_EVGCFGRESTORE_PV_READ;
         }

         if ( opt_v > 0) printf ( "%s -- checking for new physical "
                                  "volume \"%s\"\n",
                                  cmd, pv_name);
         if ( pv_check_new ( pv) == FALSE) {
            if ( vg_check_name ( pv->vg_name) == 0 &&
                 strcmp ( pv->vg_name, vg_name) != 0) {
               fprintf ( stderr, "%s -- physical volume \"%s\" belongs "
                                 "to volume group \"%s\"\n\n",
                                 cmd, pv->pv_name, pv->vg_name);
               return LVM_EVGCFGRESTORE_PV_CHECK_IN_VG;
            }
         }

         if ( pv_index_old > -1) index = pv_index_old;
         else                    index = pv_index;

         if ( pv_get_size ( pv_name, NULL) !=  vg.pv[index]->pv_size) {
            fprintf ( stderr,
                      "%s -- size of physical volume %s differs from backup\n",
                      cmd, pv_name);
            return LVM_EVGCFGRESTORE_PV_GET_SIZE;
         }

         memcpy ( &vg_write, &vg, sizeof ( vg));
         vg_write.pv[0] = vg.pv[index];
         vg_write.pv[1] = NULL;

         /* store VGDA from backup on physical volume */
         if ( opt_v)
            printf ( "%s -- writing VGDA of \"%s\" to physical volume \"%s\"\n",
                     cmd, vg_name, vg_write.pv[0]->pv_name);
         if ( ( ret = vg_write_with_pv_and_lv ( &vg_write)) < 0) {
            fprintf ( stderr,
                      "%s -- ERROR \"%s\" storing VGDA of \"%s\" on disks\n\n",
                      cmd, lvm_error ( ret), vg_name);
            return LVM_EVGCFGRESTORE_VG_WRITE;
         }
   
         if ( opt_v)
            printf ( "%s -- removing any special files for \"%s\"\n",
                     cmd, vg_name);
         vg_remove_dir_and_group_and_nodes ( vg_name);
   
         if ( ( ret = vg_create_dir_and_group_and_nodes ( &vg, opt_v)) < 0) {
            fprintf ( stderr, "%s -- %d errors creating volume group "
                              "directory and special files\n\n",
                              cmd, -ret);
            return LVM_EVGCFGRESTORE_VG_REMOVE_DIR;
         }
   
         if ( opt_v > 0) printf ( "%s -- inserting volume group \"%s\" "
                                  "into \"%s\"\n",
                                  cmd, vg_name, LVMTAB);
         if ( ( ret = lvm_tab_vg_insert ( vg_name)) < 0 &&
              ret != -LVM_ELVM_TAB_VG_INSERT_VG_EXISTS) {
            fprintf ( stderr, "%s -- ERROR \"%s\" inserting volume group into "
                              "\"%s\"\n\n",
                              cmd, lvm_error ( ret), LVMTAB);
            return LVM_EVGCFGRESTORE_LVM_TAB_VG_INSERT;
         }
         
         if ( ( ret = vg_cfgbackup ( vg_name, LVMTAB_DIR,
                                     opt_v, &vg)) < 0) {
            fprintf ( stderr, "%s -- ERROR \"%s\" writing lvmtab\n\n",
                              cmd, lvm_error ( ret));
            return LVM_EVGCFGRESTORE_VG_CFGBACKUP;
         }

         printf ( "%s -- VGDA for \"%s\" successfully restored "
                  "to physical volume \"%s\"\n",
                  cmd, vg_name, pv_name);
         if ( opt_f > 0)
            printf ( "%s -- you may not have an actual backup of "
                     "restored volume group \"%s\"\n", cmd, vg_name);
      } else printf ( "%s -- test run for volume group \"%s\" end\n",
                      cmd, vg_name);
   }

   printf ( "\n");

   lvm_interrupt ();
   LVM_UNLOCK ( 0);

   return 0;
}
