blob: dcdf2f240906bce72749e8e1c4da26e281fd7c2d [file] [log] [blame]
Jim Ostrowski097bb2d2024-02-24 23:13:35 -08001#!/bin/bash
2
3# Helper script to rename the camera calibration file when moving to new robot
4
5# grep isn't happy with set
6# set -e
7
8
9usage_and_exit () {
10 echo
11 echo "Usage:"
12 echo "$0 ORIG_FILENAME NEW_TEAM_NUMBER NEW_ORIN_NUMBER NEW_CAMERA_NUMBER"
13 echo
14 exit 2
15}
16
17if [[ $# -ne 4 ]]; then
18 echo "ERROR: Requires 4 parameters"
19 usage_and_exit
20fi
21
22ORIG_FILENAME=$1
23NEW_TEAM_NUMBER=$2
24NEW_ORIN_NUMBER=$3
25NEW_CAMERA_NUMBER=$4
26
27if [[ ! -x ${ORIG_FILENAME} ]]; then
28 echo "${ORIG_FILENAME} does not exist"
29 usage_and_exit
30fi
31
32check_971=`echo "${NEW_TEAM_NUMBER}" | grep "971"`
33if [[ ${check_971} == "" ]]; then
34 echo "NEW_TEAM_NUMBER (${NEW_TEAM_NUMBER}) does not contain '971'"
35 usage_and_exit
36fi
37
38if [[ ${NEW_ORIN_NUMBER} != 1 && ${NEW_ORIN_NUMBER} != 2 ]]; then
39 echo "NEW_ORIN_NUMBER (${NEW_ORIN_NUMBER}) must be either 1 or 2"
40 usage_and_exit
41fi
42
43if [[ ${NEW_CAMERA_NUMBER} != 0 && ${NEW_CAMERA_NUMBER} != 1 ]]; then
44 echo "NEW_CAMERA_NUMBER (${NEW_CAMERA_NUMBER}) must be either 0 or 1"
45 usage_and_exit
46fi
47
48# Extract parts of the filename, based on just the basename
49# This assumes filenames of the form:
50# calibration_orin-971-1-0_cam-24-01_2024-02-07_20-11-35.566609408.json
51IFS='_' read -r -a name_parts <<< `basename "${ORIG_FILENAME}"`
52
53echo "For ${ORIG_FILENAME}:"
54for element in "${name_parts[@]}"
55do
56 echo "$element"
57done
58
59# Rename file based on this new info (be sure to handle paths properly)
60NEW_FILENAME=`dirname ${ORIG_FILENAME}`"/${name_parts[0]}_orin-${NEW_TEAM_NUMBER}-${NEW_ORIN_NUMBER}-${NEW_CAMERA_NUMBER}_${name_parts[2]}_${name_parts[3]}_${name_parts[4]}"
61
62echo
63echo "For camera id: ${name_parts[2]}"
64echo "Renaming from:"
65echo "${ORIG_FILENAME} to: "
66echo "${NEW_FILENAME}"
67echo
68echo "and changing from "
69echo "${name_parts[1]} to: "
70echo "orin-${NEW_TEAM_NUMBER}-${NEW_ORIN_NUMBER}-${NEW_CAMERA_NUMBER}"
71echo
72
73mv ${ORIG_FILENAME} ${NEW_FILENAME}
74
75
76echo "REPLACING ORIN_NUMBER"
77sed -i s/orin./orin${NEW_ORIN_NUMBER}/ ${NEW_FILENAME}
78
79echo "Replacing TEAM NUMBER"
80sed -i s/\"team_number\"\:\ [1-9]*\,/\"team_number\"\:\ ${NEW_TEAM_NUMBER},/ ${NEW_FILENAME}
81
82echo "REPLACING CAMERA_NUMBER"
83sed -i s/\"camera_number\"\:\ [0-9]/\"camera_number\"\:\ ${NEW_CAMERA_NUMBER}/ ${NEW_FILENAME}
84
85
86