#!/bin/bash
# Check if root
if [ "$EUID" -ne 0 ]
  then 
    echo "  Please run as root"
  exit 1
fi

model=$1

if [ -z "$model" ]; then
    echo "Error: Model not specified"
    exit 1
fi

date=$(date '+%Y-%m-%d_%H-%M-%S')
tmpdir="/tmp"

rm -f /tmp/${model}_*.icd
filename=${model}_${date}.icd
cd ${tmpdir}
if [ -f "/opt/scripts/device-iec61850/src/config/device/${model}.icd" ]; then
    cp /opt/scripts/device-iec61850/src/config/device/${model}.icd ${filename}
    if [ $? -eq 0 ]; then
        echo $(pwd)/${filename}
        exit 0
    else
        echo "Error copying ICD file"
        exit 1
    fi
else
    echo "Error: ICD file for ${model} not found in /opt/scripts/device-iec61850/src/config/device/"
    exit 1
fi