#!/bin/bash

DARKTABLE_LIBRARY_LOCATION=~/.config/darktable/library.db
SQLITE_BIN=`which sqlite3`

#
# check for sqlite3 binary
#
if [ $SQLITE_BIN == "" ]; then
    echo "Unable to locate sqlite3 binary. Make sure it is installed!"
    echo "apt-get install sqlite3 might help?!?"
    exit 1
fi

#
# check for darktable library
#
if [ ! -f $DARKTABLE_LIBRARY_LOCATION ]; then
    echo "Unable to locate the darktable library file."
    echo "I was looking at ${DARKTABLE_LIBRARY_LOCATION} and found nothing"
    exit 1
fi

#
# FUNCTION
# show help
#
show_help ()
{
    echo "Usage: ./dt-stat.sh [-h|--help] [OPTION]"
    echo "List the number of Images in the darktable library."
    echo "The darktable library is assumed to be located in ~/.config/darktable/library.db"
    echo ""
    echo "-h|--help         show help"
    echo ""
    echo "Possible [OPTION]s"
    echo "  model           filter by the Camera Model"
    echo "  lens            filter by lenses used"
    echo "  exposure        filter by the exposure"
    echo "  aperture        filter by aperture"
    echo "  iso             filter by ISO used"
    echo "  focal_length    filter by lens focal length"
    #echo "  year           filter by the year the images where taken"
    #echo "  year_month     filter by year+month combination the images where taken"
    echo ""
    echo "dt-stat.sh is written by Christian Rößler"
    echo "http://www.christianroessler.net    christian@christianroessler.net"
    echo ""
    echo "dt-stat.sh 1.0, Licensed under GPLv2"
    echo ""
}

#
# fetch all Parameters into a variable
#
PARAMETER=$@

#
# check for given parameter, MUST BE ONE, fail otherwise
#
if [ "$#" != 1 ]; then
    show_help
    echo "Invalid number of parameters"
    exit 1
fi

#
# check if the given parameter is -h or --help
#
if [ $PARAMETER == "-h" ]; then
    show_help
    exit 1
fi
if [ $PARAMETER == "--help" ]; then
    show_help
    exit 1
fi



#
# fixup parameters for sql query
# model lens exposure aperture iso focal_length year year_month
#
COLUMN=""
GROUPBY=""

if [ $PARAMETER == "model" ]; then
    COLUMN="(maker || ' ' || model) AS MODEL"
    GROUPBY="model"
elif [ $PARAMETER == "lens" ]; then
    COLUMN="lens AS LENS"
    GROUPBY="lens"
elif [ $PARAMETER == "exposure" ]; then
    COLUMN="'1/' || CAST(ROUND((1/exposure),1) AS INTEGER) AS EXPOSURE"
    GROUPBY="CAST(ROUND((1/exposure),1) AS INTEGER)"
elif [ $PARAMETER == "aperture" ]; then
    COLUMN="'f/' || ROUND(aperture,1) AS APERTURE"
    GROUPBY="ROUND(aperture,1)"
elif [ $PARAMETER == "iso" ]; then
    COLUMN="CAST(iso AS INTEGER) AS ISO"
    GROUPBY="iso"
elif [ $PARAMETER == "focal_length" ]; then
    COLUMN="CAST(focal_length AS INTEGER) || 'mm' AS FOCAL_LENGTH"
    GROUPBY="CAST(focal_length AS INTEGER)"
#elif [ $PARAMETER == "year" ]; then
#    COLUMN="YEAR(datetime_taken) AS YEAR"
#    GROUPBY="YEAR(datetime_taken)"
#elif [ $PARAMETER == "year_month" ]; then
#    COLUMN="YEAR(datetime_taken) AS YEAR"
#    GROUPBY="YEAR(datetime_taken)"
else
    show_help
    echo "Invalid Parameter [${PARAMETER}]"
    exit 1
fi


echo -e ".width 70\nSELECT ${COLUMN}, COUNT(*) AS COUNT FROM images GROUP BY ${GROUPBY};" | $SQLITE_BIN -header -column $DARKTABLE_LIBRARY_LOCATION




