74 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
#
APP_TARGET_PATH=/opt/zigbee2mqtt
my_print()
{
echo `date` $HOST [$$]: $1
}
fail_loop()
{
my_print "Start script failed..."
for (( ; ; ))
do
sleep 60
done
}
check_environment()
{
fail=0
# returns 0 if environment is not okay
if [[ -z $APP_GIT_VERSION ]]; then
my_print "Environment variable APP_GIT_VERSION does not exist!"
fail=1
fi
if [[ -z $APP_MQTT_SERVER ]]; then
my_print "Environment variable APP_MQTT_SERVER does not exist!"
fail=1
fi
#
if [[ $fail -ne 0 ]]; then
fail_loop
fi
}
if [[ ! -d $APP_TARGET_PATH ]]; then
my_print "Directory \"$APP_TARGET_PATH\" does not exist."
fi
if [[ ! -d $APP_TARGET_PATH/.git ]]; then
check_environment
my_print "Cloning zigbee2mqtt repository..."
git clone https://github.com/Koenkk/zigbee2mqtt.git $APP_TARGET_PATH
my_print "Changing to directory $APP_TARGET_PAT"
cd $APP_TARGET_PATH
my_print "Changing to git version $APP_GIT_VERSION"
git checkout $APP_GIT_VERSION
my_print "Replacing MQTT servername in $APP_TARGET_PATH/data/configuration.yaml"
sed -i "s|mqtt://localhost|mqtt://$APP_MQTT_SERVER|g" $APP_TARGET_PATH/data/configuration.yaml
if [[ ! -z $APP_MQTT_USER ]]; then
my_print "Adding MQTT username in $APP_TARGET_PATH/data/configuration.yaml"
sed -i "s|# user: my_user|user: $APP_MQTT_USER|g" $APP_TARGET_PATH/data/configuration.yaml
fi
if [[ ! -z $APP_MQTT_PASSWORD ]]; then
my_print "Adding MQTT password in $APP_TARGET_PATH/data/configuration.yaml"
sed -i "s|# password: my_password|password: $APP_MQTT_PASSWORD|g" $APP_TARGET_PATH/data/configuration.yaml
fi
my_print "Preparing application (npm ci)"
npm ci
fi
cd $APP_TARGET_PATH
my_print "Starting zigbee2mqtt..."
npm start
my_print "**********************************************************"
my_print "* Start of zigbee2mqtt failed. *"
my_print "* Check running npm ci and npm start in /opt/zigbee2mqtt *"
my_print "**********************************************************"
fail_loop