#!/bin/bash
cd "$(dirname "$0")"

# exit on error
set -e

function svg2png(){
    in_file="$1"
    out_file="$2"
    size="96x96"
    size="${3:-$size}"
    color="$4"
    bg_color="$5"
    [ -n "$bg_color" ] && bg_color="-background "$bg_color" -flatten"

    x=$(echo "$size" | sed 's/x.*$//')
    y=$(echo "$size" | sed 's/^.*x//')


    #convert -background none -resize 400x530\! ./wheat.svg ../www/assets/graphics/wheat-imagemagick.png # blurry
    #inkscape --export-background-opacity=0 --without-gui -w 400 -h 530 ./wheat.svg --export-png=../www/assets/graphics/wheat-inkscape.png # crisp

    inkscape --export-background-opacity=0 --without-gui -w "$x" -h "$y" "$in_file" --export-png="$out_file"
    [ -n "$color" ] && convert -background none "$out_file" -fill "$color" +opaque "$color" $bg_color "$out_file" || true

    # http://stackoverflow.com/questions/12165304/change-the-color-of-an-image-with-imagemagick
    #convert -background none -resize "$size"\! "$in_file" -fill "$color" +opaque "$color" $bg_color "$out_file"
    #convert \( "$in_file" -alpha extract \) -background "$color" -alpha shape "$out_file" # never worked
}

function svg2splash(){
    in_file="$1"
    out_file="$2"
    size="480x800"
    size="${3:-$size}"
    #gradient="gradient:gray-white"
    #gradient="radial-gradient:#555555-#222222"
    gradient="gradient:#A7A7A7-#E4E4E4"
    gradient="${4:-$gradient}"

    x=$(echo "$size" | sed 's/x.*$//')
    y=$(echo "$size" | sed 's/^.*x//')

    svg_size=$(($x/3))
    pnt_size=$(($x/10)) # was 36

    #convert -background none "$in_file" -resize "${svg_size}x${svg_size}"\! \
    inkscape --export-background-opacity=0 --without-gui -w "$svg_size" -h "$svg_size" "$in_file" --export-png="$out_file"
    convert -background none "$out_file" \
    \( -size 1x10 xc:none \) \
    \( -size "${x}x" -background none -font Arial -pointsize "$pnt_size" -fill "black" -gravity center caption:"Points Watcher" \) \
    -gravity center -append \
    \( -size "${x}x${y}" "$gradient" \) \
    +swap -gravity center -compose over -composite "$out_file"
}

# the about page, rendered from README.md, wrapping URL's with javascript function, for now
echo '<!-- DO NOT EDIT THIS FILE DIRECTLY! IT IS AUTOMATICALLY GENERATED WITH MARKDOWN FROM "README.md" IN THE "images/convert.sh" SCRIPT -->' > ../www/views/aboutViewTemplate.html
echo '<div id="aboutView">' >> ../www/views/aboutViewTemplate.html
markdown ../README.md | sed 's/a href="\(.*\)"/a href="\javascript:openExternalURL('"'\1'"')"/' >> ../www/views/aboutViewTemplate.html
echo '</div>' >> ../www/views/aboutViewTemplate.html

# first our resource images
svg2png ./notebook.svg       ../www/assets/graphics/notebook.png       45x45   "orange"
svg2png ./bread.svg          ../www/assets/graphics/bread.png          45x45   "#8C6900"
svg2png ./calculator.svg     ../www/assets/graphics/calculator.png     45x45   "blue"
svg2png ./settings.svg       ../www/assets/graphics/settings.png       45x45   "red"
svg2png ./info.svg           ../www/assets/graphics/info.png           45x45   "#0085B2"
svg2png ./search.svg         ../www/assets/graphics/search.png         45x45   "#00B200"
# arrows
svg2png ./backArrow.svg      ../www/assets/graphics/backArrow.png      35x29   "#444444"
svg2png ./moreDisclosure.svg ../www/assets/graphics/moreDisclosure.png 23x20   "#666666"
# backgrounds
svg2png ./wheat.svg          ../www/assets/graphics/wheatBg.png        400x530 "#CFCF76" "#EDEDED"
svg2png ./wheat.svg          ../www/assets/graphics/wheatBgNoColor.png 400x530 "#DDDDDD" "#EDEDED"

# create directory for market images
mkdir -p ./markets
#convert -background none -resize 512x512\! ./icon.svg ./markets/512x512.png
svg2png ./icon.svg ./markets/512x512.png 512x512

# icons
# create required directories
mkdir -p ../www/res/icon/android ../www/res/icon/blackberry ../www/res/icon/ios ../www/res/icon/webos ../www/res/icon/windows-phone
# default
svg2png   ./icon.svg ../www/icon.png                          96x96
# android
svg2png ./icon.svg ../www/res/icon/android/icon-96-xhdpi.png  96x96
svg2png ./icon.svg ../www/res/icon/android/icon-72-hdpi.png   72x72
svg2png ./icon.svg ../www/res/icon/android/icon-48-mdpi.png   48x48
svg2png ./icon.svg ../www/res/icon/android/icon-36-ldpi.png   36x36
# blackberry
svg2png ./icon.svg ../www/res/icon/blackberry/icon-80.png     80x80
# ios
svg2png ./icon.svg ../www/res/icon/ios/icon-57.png            57x57
svg2png ./icon.svg ../www/res/icon/ios/icon-72.png            72x72
svg2png ./icon.svg ../www/res/icon/ios/icon-57-2x.png         114x114
svg2png ./icon.svg ../www/res/icon/ios/icon-72-2x.png         144x144
# webos
svg2png ./icon.svg ../www/res/icon/webos/icon-64.png          64x64
# windows phone
svg2png ./icon.svg ../www/res/icon/windows-phone/icon-48.png  48x48
svg2png ./icon.svg ../www/res/icon/windows-phone/icon-173.png 173x173

# splash screens
# create required directories
mkdir -p ../www/res/screen/android ../www/res/screen/blackberry ../www/res/screen/ios ../www/res/screen/windows-phone
# default
svg2splash ./icon.svg ../www/screen.png                                   480x800
# android
svg2splash ./icon.svg ../www/res/screen/android/screen-ldpi-portrait.png  200x320
svg2splash ./icon.svg ../www/res/screen/android/screen-mdpi-portrait.png  320x480
svg2splash ./icon.svg ../www/res/screen/android/screen-hdpi-portrait.png  480x800
svg2splash ./icon.svg ../www/res/screen/android/screen-xhdpi-portrait.png 720x1280
svg2splash ./icon.svg ../www/res/screen/android/screen-ldpi-portrait.png  200x320
# blackberry (appears to be just the icon)
#svg2splash ./icon.svg ../www/res/screen/blackberry/screen-225.png         225x225
svg2png    ./icon.svg ../www/res/screen/blackberry/screen-225.png         225x225
# ios
svg2splash ./icon.svg ../www/res/screen/ios/screen-iphone-portrait.png    320x480
svg2splash ./icon.svg ../www/res/screen/ios/screen-iphone-portrait-2x.png 640x960
svg2splash ./icon.svg ../www/res/screen/ios/screen-ipad-portrait.png      768x1024
svg2splash ./icon.svg ../www/res/screen/ios/screen-ipad-landscape.png     1024x768
# windows phone
svg2splash ./icon.svg ../www/res/screen/windows-phone/screen-portrait.jpg 480x800

# screen shots
#convert -resize 480x854\! ./ss1.png ./markets/ss1.png
