|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Function to print help |
| 4 | +print_help() { |
| 5 | + echo "Usage: ./clone-vehicle.sh [OPTIONS]" |
| 6 | + echo "" |
| 7 | + echo "Clones a Neptus vehicle definition file (.nvcl) and updates specific parameters." |
| 8 | + echo "" |
| 9 | + echo "Options:" |
| 10 | + echo " --source <path> Path to source file. Default: vehicles-defs/09-autonaut-01.nvcl" |
| 11 | + echo " --id <string> (Required) All lowercase vehicle name, no spaces (e.g., autonaut-02)" |
| 12 | + echo " --name <string> (Required) Full name, mix case and spaces allowed (e.g., Autonaut 02)" |
| 13 | + echo " --imei <string> (Required) Primary IMEI number" |
| 14 | + echo " --imei1 <string> (Optional) Secondary IMEI number" |
| 15 | + echo " --ip <string> (Required) IP address of the vehicle" |
| 16 | + echo " --imcid <string> (Required) IMC id in hex format (e.g., 08:06)" |
| 17 | + echo " --icon-color <rgb> (Optional) RGB icon color (e.g., '255,51,153')" |
| 18 | + echo " --dest <path> (Optional) Path for the output file. Defaults to creating a new file alongside the source." |
| 19 | + echo " -h, --help Show this help message" |
| 20 | + echo "" |
| 21 | + echo "Example:" |
| 22 | + echo " ./clone-vehicle.sh --source vehicles-defs/09-autonaut-01.nvcl \\" |
| 23 | + echo " --id autonaut-02 \\" |
| 24 | + echo " --name \"Autonaut 02\" \\" |
| 25 | + echo " --imei 300534068640999 \\" |
| 26 | + echo " --ip 10.1.0.2 \\" |
| 27 | + echo " --imcid 08:06 \\" |
| 28 | + echo " --icon-color \"255,0,0\"" |
| 29 | +} |
| 30 | + |
| 31 | +# Dependency check |
| 32 | +if ! command -v xmlstarlet &> /dev/null; then |
| 33 | + echo "Error: 'xmlstarlet' is not installed." |
| 34 | + echo "Please install it to continue (e.g., 'sudo apt update && sudo apt install xmlstarlet')." |
| 35 | + exit 1 |
| 36 | +fi |
| 37 | + |
| 38 | +# Default values |
| 39 | +SOURCE_FILE="vehicles-defs/09-autonaut-01.nvcl" |
| 40 | +ID="" |
| 41 | +NAME="" |
| 42 | +IMEI="" |
| 43 | +IMEI1="" |
| 44 | +IP="" |
| 45 | +IMCID="" |
| 46 | +ICON_COLOR="" |
| 47 | +DEST_FILE="" |
| 48 | + |
| 49 | +# Parse arguments |
| 50 | +while [[ "$#" -gt 0 ]]; do |
| 51 | + case $1 in |
| 52 | + --source) SOURCE_FILE="$2"; shift ;; |
| 53 | + --id) ID="$2"; shift ;; |
| 54 | + --name) NAME="$2"; shift ;; |
| 55 | + --imei) IMEI="$2"; shift ;; |
| 56 | + --imei1) IMEI1="$2"; shift ;; |
| 57 | + --ip) IP="$2"; shift ;; |
| 58 | + --imcid) IMCID="$2"; shift ;; |
| 59 | + --icon-color) ICON_COLOR="$2"; shift ;; |
| 60 | + --dest) DEST_FILE="$2"; shift ;; |
| 61 | + -h|--help) print_help; exit 0 ;; |
| 62 | + *) echo "Unknown parameter passed: $1"; exit 1 ;; |
| 63 | + esac |
| 64 | + shift |
| 65 | +done |
| 66 | + |
| 67 | +# 1. Check if mandatory fields are provided |
| 68 | +if [[ -z "$ID" || -z "$NAME" || -z "$IMEI" || -z "$IP" || -z "$IMCID" ]]; then |
| 69 | + echo "Error: Missing required arguments." |
| 70 | + echo "" |
| 71 | + print_help |
| 72 | + exit 1 |
| 73 | +fi |
| 74 | + |
| 75 | +# 2. Validations using regex |
| 76 | +if [[ ! "$ID" =~ ^[a-z0-9-]+$ ]]; then |
| 77 | + echo "Error: --id must be lowercase and contain no spaces (e.g., autonaut-02)" |
| 78 | + exit 1 |
| 79 | +fi |
| 80 | + |
| 81 | +if [[ ! "$IMCID" =~ ^[0-9a-fA-F]{2}:[0-9a-fA-F]{2}$ ]]; then |
| 82 | + echo "Error: --imcid must be in hex format dd:dd (e.g., 08:06)" |
| 83 | + exit 1 |
| 84 | +fi |
| 85 | + |
| 86 | +if [[ -n "$ICON_COLOR" && ! "$ICON_COLOR" =~ ^[[:space:]]*[0-9]{1,3}[[:space:]]*,[[:space:]]*[0-9]{1,3}[[:space:]]*,[[:space:]]*[0-9]{1,3}[[:space:]]*$ ]]; then |
| 87 | + echo "Error: --icon-color must be an RGB string (e.g., '255,51,153')" |
| 88 | + exit 1 |
| 89 | +fi |
| 90 | + |
| 91 | +# 3. Resolve source path and check if it exists |
| 92 | +if [[ ! -f "$SOURCE_FILE" ]]; then |
| 93 | + echo "Error: Source file not found at '$SOURCE_FILE'" |
| 94 | + exit 1 |
| 95 | +fi |
| 96 | + |
| 97 | +# 4. Determine Destination File if not provided |
| 98 | +if [[ -z "$DEST_FILE" ]]; then |
| 99 | + PARENT_DIR=$(dirname "$SOURCE_FILE") |
| 100 | + EXTENSION="${SOURCE_FILE##*.}" |
| 101 | + DEST_FILE="$PARENT_DIR/$ID.$EXTENSION" |
| 102 | +fi |
| 103 | + |
| 104 | +echo -e "\e[36mReading source file: $SOURCE_FILE\e[0m" |
| 105 | + |
| 106 | +# Copy source to destination to work on it in-place |
| 107 | +cp "$SOURCE_FILE" "$DEST_FILE" |
| 108 | + |
| 109 | +# 5. Modify the required fields using xmlstarlet |
| 110 | + |
| 111 | +# Update primary fields |
| 112 | +xmlstarlet ed -L \ |
| 113 | + -u "/system/properties/id" -v "$ID" \ |
| 114 | + -u "/system/properties/name" -v "$NAME" \ |
| 115 | + -u "/system/communication-means/comm-mean/host-address" -v "$IP" \ |
| 116 | + -u "/system/communication-means/comm-mean/protocols-args/imc/imc-id" -v "$IMCID" \ |
| 117 | + -u "/system/protocols-supported/protocols-args/iridium/imei" -v "$IMEI" \ |
| 118 | + "$DEST_FILE" |
| 119 | + |
| 120 | +# -> properties > appearance > icon-color (Optional) |
| 121 | +if [[ -n "$ICON_COLOR" ]]; then |
| 122 | + # Split color string by comma and remove spaces |
| 123 | + IFS=',' read -r r g b <<< "$ICON_COLOR" |
| 124 | + r=$(echo "$r" | xargs); g=$(echo "$g" | xargs); b=$(echo "$b" | xargs) |
| 125 | + |
| 126 | + xmlstarlet ed -L \ |
| 127 | + -u "/system/properties/appearance/icon-color/r" -v "$r" \ |
| 128 | + -u "/system/properties/appearance/icon-color/g" -v "$g" \ |
| 129 | + -u "/system/properties/appearance/icon-color/b" -v "$b" \ |
| 130 | + "$DEST_FILE" |
| 131 | +fi |
| 132 | + |
| 133 | +# -> protocols-supported > protocols-args > iridium > imei1 |
| 134 | +if [[ -n "$IMEI1" ]]; then |
| 135 | + # Check if imei1 node exists |
| 136 | + if xmlstarlet sel -Q -t -c "/system/protocols-supported/protocols-args/iridium/imei1" "$DEST_FILE"; then |
| 137 | + # Exists, update it |
| 138 | + xmlstarlet ed -L -u "/system/protocols-supported/protocols-args/iridium/imei1" -v "$IMEI1" "$DEST_FILE" |
| 139 | + else |
| 140 | + # Doesn't exist, create it inside <iridium> |
| 141 | + xmlstarlet ed -L -s "/system/protocols-supported/protocols-args/iridium" -t elem -n "imei1" -v "$IMEI1" "$DEST_FILE" |
| 142 | + fi |
| 143 | +else |
| 144 | + # If not provided, ensure it is removed |
| 145 | + xmlstarlet ed -L -d "/system/protocols-supported/protocols-args/iridium/imei1" "$DEST_FILE" |
| 146 | +fi |
| 147 | + |
| 148 | +# 6. Formatting completion |
| 149 | +echo -e "\e[32mSuccessfully cloned vehicle definition to: $DEST_FILE\e[0m" |
0 commit comments