summaryrefslogtreecommitdiff
path: root/inventory.sh
blob: ed7a03777f23325c65bf7b6d16f67cb1bc7d912d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash

# Function to get MAC address of the default network interface
get_mac_address() {
    INTERFACE=$(ip route | awk '/default/ {print $5; exit}')
    
    if [[ -n "$INTERFACE" ]]; then
        MAC=$(cat /sys/class/net/$INTERFACE/address)
        echo "Detected MAC Address: $MAC"
        check_device_status "$MAC"
    else
        echo "No active network interface found!"
        exit 1
    fi
}

# Function for manual input
manual_input() {
    read -p "Enter MAC Address (manually or via barcode scan): " MAC
    if [[ -n "$MAC" ]]; then
        check_device_status "$MAC"
    else
        echo "Invalid input! MAC Address cannot be empty."
        exit 1
    fi
}

# Function to check device status using the API
check_device_status() {
    local MAC_ADDRESS="$1"
    API_URL="http://localhost:3000/check-status?mac=$MAC_ADDRESS"
    
    RESPONSE=$(curl -s "$API_URL")
    
    if [[ -z "$RESPONSE" ]]; then
        echo "No response from server. Check if the API is running."
        exit 1
    fi
    
    # Check if the device is registered or new
    STATUS=$(echo "$RESPONSE" | jq -r '.status')
    
    if [[ "$STATUS" == "registered" ]]; then
        SERIAL_NUMBER=$(echo "$RESPONSE" | jq -r '.serialNumber')
        echo "Device is already registered. Serial number is $SERIAL_NUMBER."
    elif [[ "$STATUS" == "new" ]]; then
        echo "Device is new. Proceeding to registration."
        register_device "$MAC_ADDRESS"
    else
        echo "Unknown status: $STATUS"
    fi
}

# Function to register the device
register_device() {
    local MAC_ADDRESS="$1"
    
    # Ask user for variant choice (pro or starter)
    echo "Select variant (pro or starter):"
    read -p "Enter your choice: " VARIANT
    
    if [[ "$VARIANT" != "pro" && "$VARIANT" != "starter" ]]; then
        echo "Invalid variant choice. Exiting."
        exit 1
    fi
    
    # Get OS name from /etc/os-release
    OS_NAME=$(grep "^NAME=" /etc/os-release | cut -d= -f2 | tr -d '"')
    
    # Optional customer information
    read -p "Enter customer name (or press Enter to skip): " CUSTOMER_NAME
    read -p "Enter customer address (or press Enter to skip): " CUSTOMER_ADDRESS
    read -p "Enter customer phone number (or press Enter to skip): " CUSTOMER_NUMBER

    # Prepare JSON body for the POST request
    JSON_BODY=$(jq -n \
        --arg verient "$VARIANT" \
        --arg mac "$MAC_ADDRESS" \
        --arg os "$OS_NAME" \
        --arg status "registered" \
        --arg customerName "$CUSTOMER_NAME" \
        --arg customerAddress "$CUSTOMER_ADDRESS" \
        --arg customerNumber "$CUSTOMER_NUMBER" \
        '{
            Verient: $verient,
            mac: $mac,
            os: $os,
            status: $status,
            customerName: $customerName,
            customerAddress: $customerAddress,
            customerNumber: $customerNumber
        }')
    
    # Send the registration request
    REGISTER_URL="http://localhost:3000/upload"
    REGISTER_RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" -d "$JSON_BODY" "$REGISTER_URL")
    
    if [[ -n "$REGISTER_RESPONSE" ]]; then
        echo "Device successfully registered. Response: $REGISTER_RESPONSE"
    else
        echo "No response from the registration API. Check if the API is running."
    fi
}

# Menu for user selection
echo "Select an option:"
echo "1. Input MAC manually (type manually or barcode scan)"
echo "2. Auto-detect device MAC"

read -p "Enter your choice (1 or 2): " CHOICE

case $CHOICE in
    1) manual_input ;;
    2) get_mac_address ;;
    *) echo "Invalid option. Please select 1 or 2." ;;
esac