{-# LANGUAGE NoImplicitPrelude #-}
module Panda.Helper.ThumbHelper where

-- env
import Panda.Helper.Env hiding (match, body, quote)
import qualified Panda.Config.Global as G
import System.Cmd

convert_if_missing_thumb w x = do
  thumb_exists <- dir_exist (thumb_path)
  if thumb_exists
    then return ()
    else mkdir thumb_path >> convert w x 
  
  where
    thumb_path = x / G.thumb_id

convert w x = do
  images <- ls x ^ filter is_image
  images.map (convert_cmd w x).sequence >>= print
  return ()

quote x = "\"" ++ x ++ "\""

system_utf8 = u2b > system

convert_cmd w p x = do
  system_utf8 $ ["convert -resize", w.show, i, o].join " ".trace'
  where
    i = quote $ p / x 
    o = quote $ p / G.thumb_id / x