packages feed

himerge-0.20: src/Init.hs

{-
    Himerge initialization routines.
    Copyright (C) 2007, 2008 Luis Francisco Araujo <araujo@gentoo.org>

    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 of the License, 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; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-}

module Init
    where

import Util
    (removeOddDirs, makeconfpath, packagekeywords,
     packagemask, packageuse)
import System.Directory

himergecfg :: FilePath
himergecfg = "/.himerge/"

himergeDir :: IO FilePath
himergeDir = getHomeDirectory >>= return . (++ himergecfg)

getDirname :: FilePath -> FilePath
getDirname = reverse . takeWhile (/= '/') . reverse

initialize :: IO ()
initialize = initDirectory

initDirectory :: IO ()
initDirectory =
    do
      cfgdir <- himergeDir
      createDirectoryIfMissing True cfgdir
      mapM_ backUpFiles [makeconfpath, packagekeywords
                        , packagemask, packageuse]

backUpFiles :: FilePath -> IO ()
backUpFiles filepath =
    do
      isfile <- doesFileExist filepath
      case isfile of
        True -> himergeDir >>= copyFile filepath . (++ (getDirname filepath))
        False -> 
            do
              isdir <- doesDirectoryExist filepath
              case isdir of
                True -> himergeDir >>= return . (++ (getDirname filepath)) >>= 
                        makeCFGDirectory filepath
                False -> return ()

makeCFGDirectory :: FilePath -> FilePath -> IO ()
makeCFGDirectory destdir newdir =
    do
      createDirectoryIfMissing True newdir
      contents <- getDirectoryContents destdir
      mapM_ (\ f -> do
               let dir = destdir ++ "/" ++ f
               isdir <- doesDirectoryExist f
               case isdir of
                 True -> makeCFGDirectory dir (newdir ++ "/" ++ f)
                 False -> copyFile dir (newdir ++ "/" ++ f)) (removeOddDirs contents)