#!/bin/bash
# Script: can.sh
# Description: Configures CAN interface can0 on startup

# Check if running as root
if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root" 
    exit 1
fi

# Disable CAN interface can0 if it's up
if ip link show can0 &> /dev/null; then
    ip link set can0 down
fi

# Load necessary kernel modules
modprobe can
modprobe can_raw
modprobe mttcan

# Configure CAN interface can0 with bitrate 250000
ip link set can0 type can bitrate 250000
ip link set up can0

# RTK
chmod 777 /dev/ttyCH341USB0

# remote
chmod 777 /dev/input/event*

# Check the return status of the last command
if [[ $? -ne 0 ]]; then
    echo "Error configuring CAN interface can0"
    exit 1
fi

echo "CAN interface can0 configured successfully"
exit 0

