#!/usr/bin/runhaskell
-- On Debian systems, installing ghc6 should give you everything you need.
-- You can run this file directly, or compile it with
--    % ghc -o remove-spare-files remove-spare-files.hs
-- to make a binary.  You'll need to recompile it if you change the
-- actuallyDeleteFiles or verbose flags

import System.Directory
import System.Environment

import Control.Monad

import Data.List

-- Change to True to make the program actually delete the files
actuallyDeleteFiles :: Bool
actuallyDeleteFiles = False

-- Change to False to not print what's happening.
verbose :: Bool
verbose = True

removeSpareFiles :: FilePath -> FilePath -> IO ()
removeSpareFiles dirA dirB = do
    dirAContents <- getDirectoryContents dirA
    dirBContents <- getDirectoryContents dirB
    -- dirAContents minus the elements of dirBContents
    let missingFiles = dirAContents \\ dirBContents
    -- Add "dirA/" onto the start of the file names
    let missingFilePaths = map (\f -> dirA ++ "/" ++ f) missingFiles
    when verbose $ do
        putStrLn "Deleting the following files:"
        mapM_ putStrLn missingFilePaths
    when actuallyDeleteFiles $ do
        mapM_ removeFile missingFilePaths

main :: IO ()
main = do
    args <- getArgs
    case args of
        -- If two or more arguments are supplied, call the first two dirA, dirB
        (dirA:dirB:_) -> removeSpareFiles dirA dirB
        -- Otherwise,
        _             -> putStrLn "Usage: remove-spare-files dirA dirB"

{- Sample run:

 talkie /tmp
% ls aoeu
a  b  c  d  e  f  g  h  i  id  k  m  x
 talkie /tmp
% ls snth
a  c  e  f  i  k
 talkie /tmp
% ./remove-spare-files.hs aoeu snth
aoeu/g
aoeu/d
aoeu/b
aoeu/h
aoeu/m
aoeu/x
aoeu/id

-}
-- vim: tw=80
