-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.License
-- Copyright   :  Isaac Jones 2003-2005
--                Duncan Coutts 2008
--
-- Maintainer  :  cabal-devel@haskell.org
-- Portability :  portable
--
-- The License datatype.  For more information about these and other
-- open-source licenses, you may visit <http://www.opensource.org/>.
--
-- The @.cabal@ file allows you to specify a license file. Of course you can
-- use any license you like but people often pick common open source licenses
-- and it's useful if we can automatically recognise that (eg so we can display
-- it on the hackage web pages). So you can also specify the license itself in
-- the @.cabal@ file from a short enumeration defined in this module. It
-- includes 'GPL', 'LGPL' and 'BSD3' licenses.

{- All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of Isaac Jones nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}

module Distribution.License (
    License(..),
    knownLicenses,
  ) where

import Distribution.Version (Version(Version))

import Distribution.Text (Text(..), display)
import qualified Distribution.Compat.ReadP as Parse
import qualified Text.PrettyPrint as Disp
import Text.PrettyPrint ((<>))
import qualified Data.Char as Char (isAlphaNum)

-- |This datatype indicates the license under which your package is
-- released.  It is also wise to add your license to each source file
-- using the license-file field.  The 'AllRightsReserved' constructor
-- is not actually a license, but states that you are not giving
-- anyone else a license to use or distribute your work.  The comments
-- below are general guidelines.  Please read the licenses themselves
-- and consult a lawyer if you are unsure of your rights to release
-- the software.
--
data License =

--TODO: * remove BSD4

    -- | GNU Public License. Source code must accompany alterations.
    GPL (Maybe Version)

    -- | Lesser GPL, Less restrictive than GPL, useful for libraries.
  | LGPL (Maybe Version)

    -- | 3-clause BSD license, newer, no advertising clause. Very free license.
  | BSD3

    -- | 4-clause BSD license, older, with advertising clause. You almost
    -- certainly want to use the BSD3 license instead.
  | BSD4

    -- | The MIT license, similar to the BSD3. Very free license.
  | MIT

    -- | Holder makes no claim to ownership, least restrictive license.
  | PublicDomain

    -- | No rights are granted to others. Undistributable. Most restrictive.
  | AllRightsReserved

    -- | Some other license.
  | OtherLicense

    -- | Not a recognised license.
    -- Allows us to deal with future extensions more gracefully.
  | UnknownLicense String
  deriving (D:Read ::
  (Int -> ReadS a)
  -> ReadS [a]
  -> ReadPrec a
  -> ReadPrec [a]
  -> T:Read aRead, D:Show ::
  (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> T:Show aShow, D:Eq :: (a -> a -> Bool) -> (a -> a -> Bool) -> T:Eq aEq)

knownLicenses :: [License]
knownLicenses = [ GPL :: Maybe Version -> LicenseGPL  unversioned :: Maybe aunversioned, GPL :: Maybe Version -> LicenseGPL  (version :: Maybe Versionversion [2]),   GPL :: Maybe Version -> LicenseGPL  (version :: Maybe Versionversion [3])
                , LGPL :: Maybe Version -> LicenseLGPL unversioned :: Maybe aunversioned, LGPL :: Maybe Version -> LicenseLGPL (version :: Maybe Versionversion [2,1]), LGPL :: Maybe Version -> LicenseLGPL (version :: Maybe Versionversion [3])
                , BSD3 :: LicenseBSD3, MIT :: LicenseMIT
                , PublicDomain :: LicensePublicDomain, AllRightsReserved :: LicenseAllRightsReserved, OtherLicense :: LicenseOtherLicense]
 where
   unversioned = Nothing :: Maybe aNothing
   version   v = Just :: a -> Maybe aJust (Version :: [Int] -> [String] -> VersionVersion v :: [Int]v [] :: [a][])

instance D:Text :: (a -> Doc) -> (forall r. ReadP r a) -> T:Text aText License where
  disp (GPL  version)         = text :: String -> DocDisp.text "GPL"  (<>) :: Doc -> Doc -> Doc<> dispOptVersion :: Maybe Version -> DocdispOptVersion version :: Maybe Versionversion
  disp (LGPL version)         = text :: String -> DocDisp.text "LGPL" (<>) :: Doc -> Doc -> Doc<> dispOptVersion :: Maybe Version -> DocdispOptVersion version :: Maybe Versionversion
  disp (UnknownLicense other) = text :: String -> DocDisp.text other :: Licenseother
  disp other                  = text :: String -> DocDisp.text (show :: Show a => a -> Stringshow other :: Licenseother)

  parse = do
    name    <- munch1 :: (Char -> Bool) -> ReadP r StringParse.munch1 (\c -> isAlphaNum :: Char -> BoolChar.isAlphaNum c :: Charc (&&) :: Bool -> Bool -> Bool&& c :: Charc (/=) :: Eq a => a -> a -> Bool/= '-')
    version <- option :: a -> ReadP r a -> ReadP r aParse.option Nothing :: Maybe aNothing (char :: Char -> ReadP r CharParse.char '-' (>>) :: Monad m => forall a b. m a -> m b -> m b>> fmap :: Functor f => forall a b. (a -> b) -> f a -> f bfmap Just :: a -> Maybe aJust parse :: Text a => forall r. ReadP r aparse)
    return :: Monad m => forall a. a -> m areturn ($!) :: (a -> b) -> a -> b$! case (name :: Stringname, version :: Maybe Versionversion :: Maybe Version) of
      ("GPL",               _      ) -> GPL :: Maybe Version -> LicenseGPL  version :: Maybe Versionversion
      ("LGPL",              _      ) -> LGPL :: Maybe Version -> LicenseLGPL version :: Maybe Versionversion
      ("BSD3",              Nothing) -> BSD3 :: LicenseBSD3
      ("BSD4",              Nothing) -> BSD4 :: LicenseBSD4
      ("MIT",               Nothing) -> MIT :: LicenseMIT
      ("PublicDomain",      Nothing) -> PublicDomain :: LicensePublicDomain
      ("AllRightsReserved", Nothing) -> AllRightsReserved :: LicenseAllRightsReserved
      ("OtherLicense",      Nothing) -> OtherLicense :: LicenseOtherLicense
      _                              -> UnknownLicense :: String -> LicenseUnknownLicense ($) :: (a -> b) -> a -> b$ name :: Stringname
                                     (++) :: [a] -> [a] -> [a]++ maybe :: b -> (a -> b) -> Maybe a -> bmaybe "" (('-'(:) :: a -> [a] -> [a]:) (.) :: (b -> c) -> (a -> b) -> a -> c. display :: Text a => a -> Stringdisplay) version :: Maybe Versionversion

dispOptVersion :: Maybe Version -> Disp.Doc
dispOptVersion Nothing  = empty :: DocDisp.empty
dispOptVersion (Just v) = char :: Char -> DocDisp.char '-' (<>) :: Doc -> Doc -> Doc<> disp :: Text a => a -> Docdisp v :: [Int]v