# Pantry can be much easier to use when you have shell functions to
# help you. These are the functions I use. They might be usable
# as-is, but I encourage you to modify them for your needs. These
# are not fully documented--that is, you'll need to examine the
# functions themselves to know exactly what they do and how they
# work. The interface to these functions is quite non-standard--for
# instance, the pq function's first non-option argument is required,
# while the second two are optional. This works, but is hacky.
#
# These functions are written for the Z Shell. Every major Linux
# distribution makes zsh available in its package repository, and it
# is a standard part of recent releases of Mac OS X. You can also
# get zsh from its home page, http://www.zsh.org
#
# These functions would work in bash, with modifications. Most
# importantly, zsh and bash have different word-splitting behavior.
#
#
function pzhelp {
echo \
"ENVIRONMENT VARIABLES
all: all goals
macro: macronutrients only goals
summary: --print name --print info --print goals

ADDING TO MEALS
Options:
-g Look up by group rather than by name
-r use percent refuse
-k change by calories
-q change qty
-u change unit
pos args: search terms

ab Add to Breakfast
al Add to Lunch
ad Add to Dinner

REPORT
ptoday - get report of today's food intake"
}

# 2600 calories
#              Carbs      Fat  Protein
#Percent          70       20       10
#Calories       1680      480      240
#Grams           420       53       60

summary=(--print name --print info --print goals)
all=(
        --goal "Calories"               2600
        --goal "Total Fat, g"           53
        --goal "Saturated Fat, g"       20
        --goal "Trans Fat, g"           0
        --goal "Cholesterol, mg"        300
        --goal "Sodium, mg"             2400
        --goal "Total Carbohydrate, g"  420
        --goal "Dietary Fiber, g"       45
        --goal "Sugars, g"              0
        --goal "Protein, g"             60
        --goal "Vitamin A, IU"          3000
        --goal "Vitamin C, mg"          90
        --goal "Calcium, mg"            1000
        --goal "Iron, mg"               8
       )

macro=(
        --goal "Calories"               2600
        --goal "Total Fat, g"           53
        --goal "Total Carbohydrate, g"  420
        --goal "Protein, g"             60
    )

function addFoods
{
    zsh /usr/local/share/pantry/scripts/addFoods $@
}

function addRecipes
{
    zsh /usr/local/share/pantry/scripts/addRecipes $@
}

function pq {
    # This function is used to quickly add files to a diary. It
    # looks up foods by name in your quick file, and then adds the
    # food to your diary file, with the date trait modified to
    # today's date. The meal trait is modified to the argument of
    # the -M option, if you used it.
    #
    # This function is easily used with aliases, so that you can
    # type (for example) 'ab milk' to invoke 'pq -M Breakfast milk'.
    #
    # non-option arguments are the search terms.
    # 
    # options:
    # -q change food quantity
    # -u change food units
    # -g look up by group name, not by food name
    # -r use refuse
    # -m change meal 
    # -k change by calories
    local arg args searchArgs whatToSearch meal matches qty unit
    args=( --ignore-case 
          --add diary 
          --change date $(date +%F)
	  --print name
          --print info
          --print goals $macro
          --print blank
          --auto-order)
    whatToSearch=name
    while getopts 'grm:k:q:u:' arg; do
	case $arg in
	    (g) 
		whatToSearch=group;;
	    (r)
		args+=--refuse;;
	    (m)
                args+=(--change meal $OPTARG);;
            (k)
                args+=(--by-nut Calories $OPTARG);;
            (q)
                args+=(--change quantity $OPTARG);;
            (u)
                args+=(--change unit $OPTARG);;
	esac
    done

    shift $(( $OPTIND - 1 ))
    if [[ -z $1 ]]; then
	echo 'pq: at least one search argument required.' 1>&2
	return
    fi

    for arg in $@
    do
        searchArgs+=(--search $whatToSearch $arg)
    done
    args=($args $searchArgs)

    if [[ $whatToSearch == name ]]; then
        matches=$(pantry --ignore-case $searchArgs --print name quick)
        matches=(${(f)matches})
	if [[ -z ${matches} ]]; then
	    echo "No matches." 1>&2
	    return
	elif [[ ${#matches} -ne 1 ]]; then
	    echo "More than one match. Matches:" 1>&2
	    echo ${(F)matches} 1>&2
	    return
	fi
    fi

    pantry $args quick
    pantry --search date $(date +%F) --print Goals $macro diary
}

# Aliases to use with the pq function.
# to use these aliases with the -g, -r, or -K options, just
# type them in on invocation, e.g. 'ab -r apples 2 large'
# or 'ab -g yogurt'
alias ab="pq -m Breakfast"
alias al="pq -m Lunch"
alias ad="pq -m Dinner"

# Alias for abbreviation for grams. This way I type G instead of
# '^g$' when I need to change a food's unit trait to g.
# This is a global alias. zsh has this feature; bash does not.
# Perhaps you could do this in bash using shell parameters.
alias -g G='\^g\$'

# This binds ctrl-t to insert today's date; very helpful when
# getting reports about today's food intake, or if I make a mistake
# and need to delete or edit a food, or...
#
# No idea how you'd do this in bash.
function today {
    RBUFFER="$(date +%F)${RBUFFER}"
    (( CURSOR += 10 ))
}
zle -N today
bindkey '\C-t' today

function ptoday {
    # get report of today's food intake.
    pantry --search date $(date +%F) \
           --print name \
           --print info \
           --print goals \
           --print blank \
           --print Goals \
           $macro \
           --key meal \
           --key order \
           --list meal Breakfast \
           --list meal Lunch \
           --list meal Dinner \
           diary |
    less
}
