#!/bin/bash

[ $# -ne 1 ] && echo "$0 {command}" && exit 1


# 二重起動チェック
SHELLNAME=${0##*/}  # 最後の/より後ろを取り出す
SHELLBASE=${SHELLNAME%\.*}
SHELLEXT=${SHELLNAME##*\.}
test "${SHELLBASE}" != "${SHELLEXT}" && SHELLEXT=".${SHELLEXT}"
PROCESSNUM=$(ps aux|grep -v "sh -c "|grep "${SHELLBASE}"|grep "${SHELLEXT}"|wc -l)
if test ${PROCESSNUM} -ge 3; then
echo "${SHELLNAME} now running."
exit
fi


send_cmd()
{
line=${1}
#echo -e ${line}\\r > ${DEV}
set -x
echo ${line} > ${DEV}
}



DEV=/dev/ttyAMA0


# 日立エアコン操作用
case "${1}" in
cool_on)
send_cmd "0B080001100040BFFF00CC33926D13EC788700FF00FF00FF00FF00FF53ACD12E00FF00FF";;
cool_off)
send_cmd "0B080001100040BFFF00CC33926D13EC788700FF00FF00FF00FF00FF53ACC13E00FF00FF";;
hot_on)
send_cmd "0B080001100040BFFF00CC33926D13EC4CB300FF00FF00FF00FF00FF56A9D12E00FF00FF";;
hot_off)
send_cmd "0B080001100040BFFF00CC33926D13EC4CB300FF00FF00FF00FF00FF56A9C13E00FF00FF";;
*)
echo "Error: Illegal command."
exit 1
;;
esac
