/*****************************************************************************/
/* File name: ActionCreate.java                                              */
/* Purpose: to create a tar archive                                          */
/* Last modified: 11.08.99                                                   */
/* Author: Victor Klimov, 1999                                               */
/*                                                                           */
/* This program 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.                                          */
/*                                                                           */
/* This software is distributed 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.                                                        */
/* See file COPYING for your rights (GNU GPL)                                */
/*****************************************************************************/

package jtar;


import java.io.*;
import java.util.*;
import general.*;
import io.*;

class

ActionCreate

                   extends Action

     implements jtar.Constants,
                general.Constants,
                Serializable

{

  private final static String RCSID =
"$Header: /d/NFS/jtar/RCS/ActionCreate.java,v 1.5 1999/08/13 20:04:34 vklimov Exp $";


  private final static String FILE_ARCHIVING_DIALOG_TITLE   = "Archiving";
  private final static int    FILE_ARCHIVING_DIALOG_ROWS    = 23;
  private final static int    FILE_ARCHIVING_DIALOG_COLUMNS = 77;


  String  dirNameToPrepend = null;
  boolean currentlyPrepending = false;
  int nameIndex = 0;
  int nNames = 0;

  ActionCreate( char    option,
                boolean flag   )
/*****************************************************************************/
/* Function name: ActionCreate                                               */
/* File name: ActionCreate.java                                              */
/* Purpose: constructor for the ActionCreate object                          */
/* Parameters: option - 't', 'x', 'c' etc                                    */
/*             flag - corresponding flag                                     */
/* Returns:                                                                  */
/* Last modified: 11.12.98                                                   */
/* Author: Victor Klimov, 1998                                               */
/*****************************************************************************/
    {
                                                    /* ActionCreate */
/*---------------------------------------------------------------------------*/

      super( option, flag );

    }
/*end of ActionCreate*/


  void
  collectAndSortNames()
/*****************************************************************************/
/* Function name: collectAndSortNames                                        */
/* File name: ActionCreate.java                                              */
/* Purpose: to collect and sort file names                                   */
/* Parameters:                                                               */
/* Returns:                                                                  */
/* Last modified: 12.12.98                                                   */
/* Author of the Java translation: Victor Klimov, 1998                       */
/*****************************************************************************/
    {
                                              /* collectAndSortNames */
/*---------------------------------------------------------------------------*/

    }
/*end of collectAndSortNames*/


  void
  blankNameList()
/*****************************************************************************/
/* Function name: blankNameList                                              */
/* File name: ActionCreate.java                                              */
/* Purpose: to init the file name list to its original form                  */
/* Parameters:                                                               */
/* Returns:                                                                  */
/* Last modified: 12.12.98                                                   */
/* Author of the Java translation: Victor Klimov, 1998                       */
/*****************************************************************************/
    {
                                                    /* blankNameList */
/*---------------------------------------------------------------------------*/

    }
/*end of blankNameList*/


  String
  nameFromList()
/*****************************************************************************/
/* Function name: nameFromList                                               */
/* File name: ActionCreate.java                                              */
/* Purpose: to return a new file name to dump                                */
/* Parameters:                                                               */
/* Returns: the file name                                                    */
/* Last modified: 12.12.98                                                   */
/* Author of the Java translation: Victor Klimov, 1998                       */
/*****************************************************************************/
    {
      String fileName = null;
                                                     /* nameFromList */
/*---------------------------------------------------------------------------*/

      return fileName;

    }
/*end of nameFromList*/


  String
  nameNext( Vector  commandLineOptionsVector,
            boolean changeDirs                   )
/*****************************************************************************/
/* Function name: nameNext                                                   */
/* File name: ActionCreate.java                                              */
/* Purpose: to get next file name from the list of options                   */
/* Parameters: commandLineOptionsVector - vector, containig strings          */
/*                                 of command line arguments to Jtar         */
/*             changeDirs                                                    */
/* Returns: the file name or null if no more names left                      */
/* Last modified: 11.08.99                                                   */
/* Author of the java version: Victor Klimov, 1999                           */
/*****************************************************************************/
    {
      NamesFilesInputOutput namesInputOutput = null;
      String source = null;
      StringBuffer fileNameWithPrefix;
      File   dirFile;
      boolean beginToPrependDirName = false;

                                                         /* nameNext */
/*---------------------------------------------------------------------------*/

      nNames = commandLineOptionsVector.size();

      while( nameIndex < nNames )
        {
          namesInputOutput = (NamesFilesInputOutput)
            commandLineOptionsVector.
              elementAt(nameIndex++);

          source = namesInputOutput.getInputFileName();

          source =
            util.String.
              removeTrailingPathSeparator(
                source                     );

          if (beginToPrependDirName)
            {
              try
                {
                  dirFile = new File( source );
                  if ( dirFile.exists() )
                    {
                      dirNameToPrepend =
                        new String( source );
                      beginToPrependDirName = false;
                      currentlyPrepending = true;
                    }
                  else
                    ;
                /*end of if*/

                }
              catch( NullPointerException e)
                {
                  System.out.println(
                      e.getMessage() +
                      "\nnull directory name given as an option" );
                }
              catch( SecurityException e)
                {
                  System.out.println(
                      e.getMessage() );
                }
            /*end of try-catch*/
            }
          else /* previous vector element was not "-C" */
            {
              if ( changeDirs &&
                   source.equals(
                     COMMAND_LINE_OPTION_CHDIR_STRING )
                 )
                {
                  beginToPrependDirName = true;
                  currentlyPrepending = false;
                }
              else /* don't change dirs */
                {
                  source =
                    util.QuoteString.
                      unquoteString( source );
                  if (currentlyPrepending)
                    {
                      fileNameWithPrefix = new
                        StringBuffer( dirNameToPrepend );
                      fileNameWithPrefix.
                        append( File.separator );
                      fileNameWithPrefix.
                        append( source );
                      return fileNameWithPrefix.toString();     /* return ! */
                    }
                  else
                    return source;                              /* return ! */
                /*end of if*/
                }
            /*end of if on changeDirs*/
            }
        /*end of if on beginToPrependDirName*/
        }
    /*end of while-loop*/

      return source;                                 /* return ! */

    }
/*end of nameNext*/


  boolean
  action( Vector commandLineOptionsVector,
          Header dummyTarHeader,
          BufferRead dummyReadBuffer,
          AllActions allActions            )
/*****************************************************************************/
/* Function name: action to create an archive                                */
/* File name: ActionCreate.java                                              */
/* Purpose: to create an archive                                             */
/* Parameters: commandLineOptionsVector - vector, containig strings          */
/*                                 of command line arguments to Jtar         */
/*             dummyTarHeader                                                */
/*             dummyReadBuffer                                               */
/*             allActions                                                    */
/* Returns: true - if everything is all right, false - otherwise             */
/* Last modified: 26.07.99                                                   */
/* Author of the Java translation: Victor Klimov, 1999                       */
/*****************************************************************************/
    {
      int i;
      boolean readOK = true;
      String fileToDumpName;

      BufferWrite writeBuffer = new BufferWrite( allActions );
      Header tarHeader = new Header();
                                                             /* action */
/*---------------------------------------------------------------------------*/

      if (commandLineOptionsVector == null)
        {
          return false;                                  /* return ! */
        }
      else
        ;
    /*end of if on null*/

      readOK =
        writeBuffer.open( allActions );

      if (!readOK)
        {
          return false;                                  /* return ! */
        }
      else
        ;
    /*end of if on readOK*/

      if (allActions.incremental.flag)
        {
          /* to implement in the future */
        }
      else /* not incremental */
        {
          nameIndex = 0;
          fileToDumpName =
            nameNext( commandLineOptionsVector,
                      true /* change dirs */        );

          i=-1;
          while( fileToDumpName != null )
            {
              i++;

              if (allActions.verbose.flag)
                {
                }
              else
                ;
            /*end of if on verbose*/

              writeBuffer.
                dumpFile( fileToDumpName,
                          true, /*top level call*/
                          writeBuffer,
                          tarHeader,
                          allActions               );
              fileToDumpName =
                nameNext( commandLineOptionsVector,
                          true /* change dirs */        );
            }
        /*end of while-loop*/
        }
    /*end of if on incremental*/

      writeBuffer.writeEot();
      writeBuffer.close();

      return true;

    }
/*end of action*/


} /*end of class ActionCreate */

/*****************************************************************************/
/* End of file: ActionCreate.java                                            */
/*****************************************************************************/
