%  Copyright (C) 2003,2005 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 repair}
\begin{code}
module Darcs.Commands.Repair ( repair ) where
import Workaround ( getCurrentDirectory )
import System.Directory (setCurrentDirectory )
import System.IO
import System ( exitWith, ExitCode(..) )
import Control.Monad ( when, )

import Darcs.Commands
import Darcs.Arguments ( DarcsFlag( Verbose, Quiet ),
                        any_verbosity, working_repo_dir, umask_option,
                      )
import Darcs.Patch ( patch2patchinfo )
import Darcs.Hopefully ( actually )
import Darcs.Repository ( withRepoLock, amInRepository, unsafe_fast_read_repo )
import Darcs.Repository.Checkpoint ( get_checkpoint_by_default )
import Darcs.Repository.ApplyPatches ( apply_patches_with_feedback, simple_feedback )
import Darcs.Repository.Pristine ( identifyPristine, checkPristine, replacePristine )
import Darcs.Patch.Depends ( get_patches_beyond_tag )
import Darcs.Lock( withTempDir )
import Darcs.Commands.Check ( check_uniqueness )
import Printer ( putDocLn, text )
#include "impossible.h"
\end{code}

\options{repair}
\begin{code}
repair_description :: String
repair_description = "Repair the corrupted repository."
\end{code}
\haskell{repair_help}

\begin{code}
repair_help :: String
repair_help =
 "Repair attempts to fix corruption that may have entered your\n"++
 "repository.\n"
\end{code}

\begin{code}
repair :: DarcsCommand
repair = DarcsCommand {command_name = "repair",
                       command_help = repair_help,
                       command_description = repair_description,
                       command_extra_args = 0,
                       command_extra_arg_help = [],
                       command_command = repair_cmd,
                       command_prereq = amInRepository,
                       command_get_arg_possibilities = return [],
                       command_argdefaults = nodefaults,
                       command_darcsoptions =
                           [any_verbosity, working_repo_dir, umask_option]}
\end{code}

Repair currently will only repair damage to the pristine tree.
Fortunately this is just the sort of corruption that is most
likely to happen.

\begin{code}
repair_cmd :: [DarcsFlag] -> [String] -> IO ()
repair_cmd opts _ =
  let putVerbose s = when (Verbose `elem` opts) $ putDocLn s
      putInfo s = when (not $ Quiet `elem` opts) $ putDocLn s
      feedback = simple_feedback opts "Applying"
  in withRepoLock opts $ \repository -> do
  check_uniqueness putVerbose putInfo repository
  patches <- unsafe_fast_read_repo repository
  maybe_chk <- get_checkpoint_by_default opts "."
  formerdir <- getCurrentDirectory
  withTempDir (formerdir++"/_darcs/newpristine") $ \newcur -> do
    putVerbose $ text "Applying patches..."
    case maybe_chk of
        Just chk ->
            case patch2patchinfo chk of
            Just chtg -> do
                putVerbose $ text "I am repairing from a checkpoint."
                apply_patches_with_feedback [] feedback putInfo $
                                                (chtg, actually chk)
                    : reverse (concat $ get_patches_beyond_tag chtg patches)
            Nothing -> impossible -- checkpoint is always a tag
        Nothing -> apply_patches_with_feedback [] feedback putInfo
                   $ reverse $ concat patches
    -- withTempDir ignores error on delete -- hence the hack below.
    setCurrentDirectory formerdir
    cur <- identifyPristine
    is_same <- checkPristine newcur cur
    if is_same
      then do putStrLn "The repository is already consistent, no changes made."
              exitWith ExitSuccess
      else do putStrLn "Fixing pristine tree..."
              replacePristine newcur cur
              exitWith ExitSuccess
\end{code}
