#!/usr/bin/env bash

function min-jpg {
    tmpfile=$(mktemp)
    ogfile="$1"
    jpegtran -optimize -perfect -outfile $tmpfile $ogfile
    mv $tmpfile $ogfile
}

function min-png {
    tmpfile=$(mktemp)
    ogfile="$1"
    pngcrush -rem alla -reduce -brute $ogfile $tmpfile
    mv $tmpfile $ogfile
}

function parse_git_status {
    if [[ $(git status 2> /dev/null | wc -l) -eq 0 ]]; then
        return
    fi

    if [[ $(git status 2> /dev/null | grep -E "working tree|directory clean" | wc -l) -eq 0 ]]; then
        echo ' ∓'
    fi
}

function parse_git_branch {
    git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}

function get-create-date {
    date -r "$1" +"%F"
}

function add-date-prefix {
    DIR=$(dirname "$1")
    FILE=$(basename "$1")
    DATE=$(date -r "$1" +"%F")
    mv "$1" "$DIR/$DATE_$FILE"
}

function get-bitrate {
    exiftool -AudioBitrate "$1" | awk '{print $4}'
}

function _hostname-color {
    case "$(whoami)" in
        william)   echo $blue;;
        buddy)     echo $blue;;
        root)      echo $bred;;
        *)         echo $purple;;
    esac
}

function _hostname-show {
    hostname -s | tr '[A-Z]' '[a-z]'
}