#!/bin/sh
# Check if /cfg/startup-config.cfg needs to be migrated to new syntax.
# Backup of the original is created in /cfg/backup/ for old versions,
# the migrate tool inserts old version in name before .cfg extension.
CONFIG_FILE="/cfg/startup-config.cfg"
BACKUP_FILE="/cfg/backup/startup-config.cfg"
BACKUP_DIR="$(dirname "$BACKUP_FILE")"

mkdir -p         "$BACKUP_DIR"
chown root:wheel "$BACKUP_DIR"
chmod 0770       "$BACKUP_DIR"

if [ ! -f "$CONFIG_FILE" ]; then
    logger -I $$ -k -p user.notice -t $(basename "$0")  "No $(basename "$CONFIG_FILE" .cfg) yet, likely factory reset."
    exit 0
elif migrate -cq "$CONFIG_FILE"; then
    exit 0
fi

migrate -i -b "$BACKUP_FILE" "$CONFIG_FILE"
