%  Copyright (C) 2002-2003 David Roundy
%
%  This program 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.
%
%  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.
%
%  You should have received a copy of the GNU General Public License
%  along with this program; see the file COPYING.  If not, write to
%  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
%  Boston, MA 02110-1301, USA.

\subsection{darcs initialize}
\begin{code}
module Darcs.Commands.Init ( initialize, initialize_cmd ) where
import System.Directory ( createDirectory )
import System.IO.Error ( isAlreadyExistsError )

import Darcs.Commands ( DarcsCommand(..), nodefaults )
import Darcs.Arguments ( DarcsFlag, pristine_tree, working_repo_dir,
                        inventory_choices )

import Darcs.Repository.Prefs ( write_default_prefs )
import Darcs.Repository.Format ( create_repo_format, writeRepoFormat,
                    RepoProperty ( Darcs1_0, HashedInventory ),
                    format_has_together )
import Darcs.Repository.Pristine ( createPristine, flagsToPristine )
import Darcs.Lock ( writeBinFile )
import Darcs.Repository.DarcsRepo ( write_inventory )
import Darcs.Repository ( amNotInRepository )
\end{code}

\options{initialize}

\haskell{initialize_description}

\begin{code}
initialize_description :: String
initialize_description = "Initialize a new source tree as a darcs repository."
\end{code}
Generally you will only call initialize once for each project you work on,
and calling it is just about the first thing you do.  Just make sure you
are in the main directory of the project, and initialize will set up all the
directories and files darcs needs in order to start keeping track of
revisions for your project.

\begin{code}
initialize_help :: String
initialize_help =
 "Generally you will only call initialize once for each project you work on,   \n"++
 "and calling it is just about the first thing you do.  Just make sure   \n"++
 "you are in the main directory of the project, and initialize will set up all \n"++
 "the directories and files darcs needs in order to start keeping track of   \n"++
 "revisions for your project.\n"
\end{code}

\begin{code}
initialize :: DarcsCommand
initialize = DarcsCommand {command_name = "initialize",
                         command_help = initialize_help,
                         command_description = initialize_description,
                         command_extra_args = 0,
                         command_extra_arg_help = [],
                         command_prereq = amNotInRepository,
                         command_command = initialize_cmd,
                         command_get_arg_possibilities = return [],
                         command_argdefaults = nodefaults,
                         command_darcsoptions = [pristine_tree] ++
                                                inventory_choices ++
                                                [working_repo_dir]}
\end{code}

The \verb|initialize| follows the following procedure:
It creates the directories \verb|_darcs|, \verb|_darcs/current| (or
\verb|_darcs/pristine|), \verb'_darcs/inventories',
\verb|_darcs/patches|, and \verb'_darcs/prefs',
and then creates the
empty files \verb'_darcs/prefs/motd' (see Section~\ref{motd})
and \verb|_darcs/inventory|.
It then fills the contents of
\verb'boring' and \verb'binaries' in the \verb'_darcs/prefs'
directory with useful default values as described in Sections \ref{boring} \emph{et seq.}
It is strongly
recommended that you use \verb|darcs initialize| to do this, as this
procedure may change in a future version of darcs.

\begin{options}
--no-pristine-tree
\end{options}
In order to save disk space, you can use \verb|initialize| with the
\verb|--no-pristine-tree| flag to create a repository with no pristine
tree.  Please see Section~\ref{disk-usage} for more information.

\begin{code}
initialize_cmd :: [DarcsFlag] -> [String] -> IO ()
initialize_cmd opts _ = do
    createDirectory "_darcs" `catch`
        (\e-> if isAlreadyExistsError e
              then fail "Tree has already been initialized!"
              else fail "Error creating directory `_darcs'.")
    createPristine $ flagsToPristine opts
    createDirectory "_darcs/patches"
    createDirectory "_darcs/prefs"
    write_default_prefs
    let rf = create_repo_format opts
    writeRepoFormat rf "_darcs/format"
    if format_has_together [Darcs1_0,HashedInventory] rf
       then do write_inventory "." [[]]
               writeBinFile "_darcs/hashed_inventory" ""
       else if format_has_together [HashedInventory] rf
            then writeBinFile "_darcs/hashed_inventory" ""
            else write_inventory "." [[]]
\end{code}

