#!/bin/bash set -euo pipefail # === Settings === SERVER="91.92.155.224" REMOTE_PATH="/home/ubuntu/Sodistorehome/release-package.tar.gz" TARGET_DIR="$HOME/SodiStoreHome" SCRIPT_DIR="$TARGET_DIR/scripts" MODPOLL_DIR="$TARGET_DIR/ModpollDir" # Change this if your config file has a different name (e.g., config.json, config.yaml, etc.) CONFIG_FILE="config.json" SERVICE_NAME="SodiStoreHome.service" SERVICE_PATH="/etc/systemd/system/$SERVICE_NAME" echo "📦 Downloading release package from server..." mkdir -p "$TARGET_DIR" scp -i ~/.ssh/InnovEnergy.pem.priv "ubuntu@$SERVER:$REMOTE_PATH" "$TARGET_DIR/release-package.tar.gz" echo "📂 Extracting package..." tar -xzf "$TARGET_DIR/release-package.tar.gz" -C "$TARGET_DIR" echo "📁 Ensuring directories exist..." mkdir -p "$SCRIPT_DIR" "$MODPOLL_DIR" \ "$TARGET_DIR/csvFile" "$TARGET_DIR/FailedUploads" "$TARGET_DIR/JsonLogDirectory" echo "📥 Placing files (no bin/; put under $TARGET_DIR)" # Stuff that used to go to bin/ now goes directly under $TARGET_DIR if [ -f "$TARGET_DIR/libSystem.IO.Ports.Native.so" ]; then echo " - libSystem.IO.Ports.Native.so -> $TARGET_DIR/" # already in place after extract; nothing to do fi if [ -f "$TARGET_DIR/GrowattCommunication" ]; then echo " - Setting executable on GrowattCommunication" chmod +x "$TARGET_DIR/GrowattCommunication" fi # modpoll stays in ModpollDir if [ -f "$TARGET_DIR/modpoll" ]; then echo " - Moving modpoll -> $MODPOLL_DIR/" mv -f "$TARGET_DIR/modpoll" "$MODPOLL_DIR/" chmod +x "$MODPOLL_DIR/modpoll" fi # Move Python files to scripts/ (unchanged behavior) shopt -s nullglob PY_FILES=( "$TARGET_DIR"/*.py ) if [ ${#PY_FILES[@]} -gt 0 ]; then echo " - Moving Python files -> $SCRIPT_DIR/" mv -f "${PY_FILES[@]}" "$SCRIPT_DIR/" fi shopt -u nullglob # 2) Place systemd service under /etc/systemd/system/ if [ -f "$TARGET_DIR/$SERVICE_NAME" ]; then echo "🛠️ Installing systemd service to $SERVICE_PATH (requires sudo)..." sudo install -m 0644 "$TARGET_DIR/$SERVICE_NAME" "$SERVICE_PATH" echo " - Reloading systemd daemon..." sudo systemctl daemon-reload # Enable but don't start automatically; comment out if you don't want this: if ! systemctl is-enabled --quiet "$SERVICE_NAME"; then echo " - Enabling service $SERVICE_NAME" sudo systemctl enable "$SERVICE_NAME" fi else echo "⚠️ WARNING: $SERVICE_NAME not found in $TARGET_DIR. Skipping service install." fi # 3) Place the config file under $TARGET_DIR if [ -f "$TARGET_DIR/$CONFIG_FILE" ]; then echo "📝 Config file already in $TARGET_DIR: $CONFIG_FILE" elif [ -f "$TARGET_DIR/config" ]; then echo " - Moving 'config' -> $TARGET_DIR/$CONFIG_FILE" mv -f "$TARGET_DIR/config" "$TARGET_DIR/$CONFIG_FILE" else echo "⚠️ WARNING: Config file '$CONFIG_FILE' not found in extracted package." echo " If the filename differs, set CONFIG_FILE accordingly at the top of this script." fi # 4) csvFile/, FailedUploads/, JsonLogDirectory/ were created earlier. # 5) Place log, start, stop, restart under $TARGET_DIR and make them executable for f in log start stop restart; do if [ -f "$TARGET_DIR/$f" ]; then echo " - Ensuring $f is in $TARGET_DIR and executable" chmod +x "$TARGET_DIR/$f" elif [ -f "$TARGET_DIR/$f.sh" ]; then echo " - Moving $f.sh -> $TARGET_DIR/$f and making executable" mv -f "$TARGET_DIR/$f.sh" "$TARGET_DIR/$f" chmod +x "$TARGET_DIR/$f" else echo "⚠️ NOTE: '$f' script not found in extracted package." fi done # --- ModbusTCP Integration --- echo "Installing systemd Modbus TCP service..." MODBUS_TARGET_DIR="$TARGET_DIR/ModbusTCP" sudo cp "$MODBUS_TARGET_DIR/ModbusTCP.service" /etc/systemd/system/ echo "Preparing Python virtual environment..." cd "$TARGET_DIR" python3 -m venv venv source venv/bin/activate pip install watchdog pip install pymodbus==2.5.3 pip install pyinstaller deactivate echo "Granting permission to bind port 502..." sudo setcap 'cap_net_bind_service=+ep' "$MODBUS_TARGET_DIR/dist/modbus_tcp_server" # echo "Enabling ModbusTCP systemd service..." sudo systemctl daemon-reload sudo systemctl enable --now ModbusTCP.service # Remove existing timezone link (if it exists) if [ -L /etc/localtime ]; then echo "Removing existing timezone link..." sudo rm /etc/localtime fi # Create a symbolic link to the desired timezone echo "Creating symbolic link to timezone..." sudo ln -sf /usr/share/zoneinfo/Europe/Paris /etc/localtime # Starting the SodistoreHome service sudo systemctl restart SodiStoreHome.service