#!/bin/bash

# redshift-adjust — Script for manually adjusting the colour temperature with hotkeys
# Copyright © 2014, 2015  Mattias Andrée (maandree@member.fsf.org)
# Copyright © 2015        "tPenguinLTG"
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


file="/tmp/.${UID}.${DISPLAY}.redshift-adjust"


if [ ! $# = 1 ] || [ "${1}" = "-h" ] || [ "${1}" = "--help" ]; then
    cat <<EOF
$0 =TEMP    Set colour temperature to TEMP K
$0 +TEMP    Increase colour temperature by TEMP K
$0 -TEMP    Decrease colour temperature by TEMP K
$0 -p       Print the current temperature
EOF
    if [ $# = 1 ]; then
	exit 0
    fi
    exit 1
fi

if [ ! -e "${file}" ]; then
    echo 6500 > "${file}"
fi

if [ "${1}" = "-p" ] || [ "${1}" = "--print" ]; then
    cat "${file}"
    exit 0
fi


if [ "${1::1}" = "=" ]; then
    temperature="${1:1}"
else
    temperature=$(( $(cat "${file}") ${1::1} ${1:1} ))
fi


redshift -P -O $temperature || redshift -O $temperature
echo $temperature > "${file}"

