-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathpkgver.sh
More file actions
82 lines (59 loc) · 2.12 KB
/
pkgver.sh
File metadata and controls
82 lines (59 loc) · 2.12 KB
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
#!/bin/sh
# install location: /usr/local/bin
# This script was written to interrogate a package file for its internal
# file name and version number. This can be helpful when a package file
# no longer retains its original file name or outwardly identifies its
# version number.
# Author: Andrew Thomson
# Date: 05-23-2016
# Use the following syntax to interrogate all the packages in a folder
# for PKGS in /path/to/packages/*.pkg; do pkgver "$PKGS"; done
# RESOURCES:
# http://stackoverflow.com/questions/11298855/how-to-unpack-and-pack-pkg-file
# http://www.mactech.com/articles/mactech/Vol.26/26.02/TheFlatPackage/index.html
FILE_PATH="$1"
FILE_NAME="${FILE_PATH##*/}"
PKG_PATH="${FILE_PATH%/*}"
TMP_PATH=`/usr/bin/mktemp -d /tmp/PKGINFO.XXXX`
DEBUG=false
# verify file exists and is of type pkg
if [ ! -f "$FILE_PATH" ] || [ "$FILE_NAME##*." == "pkg" ]; then
echo "ERROR: Unable to find valid package file."
echo "USAGE: ${0##*/} /path/to/package"
exit $LINENO
fi
# display variable in debug mode
if $DEBUG; then
echo "FILE: $FILE_NAME"
echo "FOLDER: $PKG_PATH"
echo "TEMP: $TMP_PATH"
fi
# get package title
PKG_TITLE=`/usr/sbin/installer -verbose -pkginfo -pkg "$FILE_PATH" | /usr/bin/grep -m 1 Title | /usr/bin/awk -F " : " '{print $2}'`
# get paths of PackageInfo files
if ! PKG_INFO=(`/usr/bin/xar -t -f "$FILE_PATH" | /usr/bin/grep PackageInfo`); then
echo "ERROR: Unable to find package file information."
exit $LINENO
fi
# change to temp folder
pushd "$TMP_PATH" > /dev/null
# extract each PackageInfo file to temp location
for PKG_FILE in ${PKG_INFO}; do
if ! /usr/bin/xar -x -f "$FILE_PATH" "$PKG_FILE"; then
echo "ERROR: Unable to extract package file information."
exit $LINENO
else
TMP_INFO+=("$TMP_PATH/$PKG_FILE")
if $DEBUG; then echo "INFO: ${TMP_INFO[@]}"; fi
fi
done
# read each PackageInfo file to extract info
for FILE_INFO in $TMP_INFO; do
PKG_VERSION+=(`/usr/bin/xpath "$FILE_INFO" "string(/pkg-info[1]/@version)" 2> /dev/null`)
echo "TITLE: $PKG_TITLE"
echo "VERSION: ${PKG_VERSION[@]}"
done
# change back to original folder
popd > /dev/null
# remove tmp files
/bin/rm -rf "$TMP_PATH"