summaryrefslogtreecommitdiff
path: root/vcard_uid_generator.hs
blob: 403e5f0cd8a4760949d04e5b356cb006b7e8248f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env stack
-- stack --install-ghc runghc --package turtle
{- vcard_uid_generator script
 -
 - Copyright 2017 Félix Sipma <felix.sipma@no-log.org>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

{-# LANGUAGE OverloadedStrings #-}

import Turtle
import qualified Control.Foldl as Fold
import Prelude hiding (FilePath)


parser :: Parser FilePath
parser = argPath "src" "source directory containing the VCARD files"

main = sh $ do
    src <- options "A simple in-place UID generator for VCARD files" parser
    contacts <- inproc "rg" ["UID", "--files-without-match", format fp src] empty
    let file = fromText $ lineToText contacts
    lines <- fold (input file) Fold.list
    let (beginning, end) = splitAt 2 lines -- arbitrary split file between lines 2 and 3
    valrand <- rand
    let b = select (beginning ++ [valrand] ++ end)
    output file b
  where
    rand = inshell "cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 36 | head -n 1 | sed -e 's/^\\(.*\\)/UID:\\1\\r/'" empty