This repository has been archived by the owner on Sep 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
mg-serial
executable file
·123 lines (110 loc) · 3.76 KB
/
mg-serial
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
117
118
119
120
121
122
123
#!/usr/bin/env bash
# mg-serial - Mac serial number generator
# The repo folder
gRepo=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=mg-common
source "$gRepo/mg-common"
function help()
{
echo "mg-serial v$gScriptVersion"
echo "Mac serial number generator script"
echo
echo "Usage: mg-serial <product name> (arguments)"
echo
echo " <product name> Target Mac product name (model ID)"
echo " --help (-h) Help (this screen)"
echo
echo "If no product name is specified, system values from the IORegistry will be used."
exit 0
}
function generate_serial_number()
{
# Source existing model data
if [[ "$gProductName" =~ "iMac" ]]; then
# shellcheck source=data/iMac.dat
source "$gRepo/data/iMac.dat"
elif [[ "$gProductName" =~ "MacBookAir" ]]; then
# shellcheck source=data/MacBookAir.dat
source "$gRepo/data/MacBookAir.dat"
elif [[ "$gProductName" =~ "MacBookPro" ]]; then
# shellcheck source=data/MacBookPro.dat
source "$gRepo/data/MacBookPro.dat"
elif [[ "$gProductName" =~ "MacBook" ]]; then
# shellcheck source=data/MacBook.dat
source "$gRepo/data/MacBook.dat"
elif [[ "$gProductName" =~ "Macmini" ]]; then
# shellcheck source=data/Macmini.dat
source "$gRepo/data/Macmini.dat"
elif [[ "$gProductName" =~ "MacPro" ]]; then
# shellcheck source=data/MacPro.dat
source "$gRepo/data/MacPro.dat"
else
print_error "Invalid model identifier!"
fi
# Generate the PPP value (manufacturing location)
local PPPIndex=$(jot -r 1 0 $((${#PPPCodes[@]} - 1)))
local PPP=${PPPCodes[PPPIndex]}
if [ $gDebug -eq 1 ]; then
local location=$(decode_location_val "$PPP")
print_debug "Generated manufacturing location" "$location"
fi
# Generate the Y value (manufacturing year)
local YIndex=$(jot -r 1 0 $((${#YCodes[@]} - 1)))
local Y=${YCodes[YIndex]}
if [ $gDebug -eq 1 ]; then
local year=$(decode_year_val "$Y")
print_debug "Generated manufacturing year" "201$year"
fi
# Generate the W value (manufacturing week)
# Possible W values: 1-9, C, D, F-H, K-N, P-R, T-Y (no 0, A, B, E, I, O, S, or Z)
local WCodes=('1' '2' '3' '4' '5' '6' '7' '8' '9' 'C' 'D' 'F' 'G' 'H' 'K' 'L' 'M' 'N' 'P' 'Q' 'R' 'T' 'U' 'V' 'W' 'X' 'Y')
local WIndex=$(jot -r 1 0 $((${#WCodes[@]} - 1)))
local W=${WCodes[WIndex]}
if [ $gDebug -eq 1 ]; then
local week=$(decode_week_val "$Y" "$W")
print_debug "Generated manufacturing week" "$week"
fi
# Generate the SSS value (unique identifier)
local base62=($(echo {0..9} {a..z} {A..Z}))
local productionNumber=$(echo $((10000 + $RANDOM)) | cut -c 1-5)
for i in $(bc <<< "obase=36; $productionNumber"); do
echo "${base62[$((10#$i))]}" | tr '\n' ',' | tr -d ',' >> "/tmp/simpleMacSerial_SSS"
done
local SSS=$(tr '[:lower:]' '[:upper:]' < /tmp/simpleMacSerial_SSS)
rm "/tmp/simpleMacSerial_SSS"
if [ $gDebug -eq 1 ]; then
print_debug "Generated unique identifier" "$SSS"
fi
# Generate the CCCC value (model identifer) from the model data
local CCCCIndex=$(jot -r 1 0 $((${#CCCCCodes[@]} - 1)))
local CCCC=${CCCCCodes[CCCCIndex]}
if [ $gDebug -eq 1 ]; then
print_debug "Generated model identifier" "$CCCC"
fi
gSerialNumber="$PPP$Y$W$SSS$CCCC"
}
# Were any valid arguments supplied?
if [ $# -eq 1 ]; then
# Print the help menu if it was requested
case "$1" in
-h|--help)
help
exit
;;
esac
# Yes. Set gProductName to the supplied model ID
gProductName="$1"
else
# No. Use the system model ID (from the IORegistry)
gProductName=$(ioreg -k product-name -d 2 | awk '/product-name/ {print $3}' | tr -d '"<>')
if [ $gDebug -eq 1 ]; then
print_debug "NOTE" "No input product name found, using IORegistry value ($gProductName)"
fi
fi
generate_serial_number
if [ $gDebug -eq 1 ]; then
echo
print_debug "Generated $gProductName serial number" "$gSerialNumber"
else
echo "$gSerialNumber"
fi