-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_ip2sg_package.sh
executable file
·167 lines (142 loc) · 4.46 KB
/
make_ip2sg_package.sh
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#! /bin/bash
# Reset marker for optind
OPTIND=1
# Set our own variables
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Define a function to show help text
function show_help {
echo "make_ip2sg_package: Create OPC packages for IP2SG XML batch filing"
echo "Usage:"
echo " make_ip2sg_package [-f] file_name"
echo "Options:"
echo " -f overwrite existing files"
exit 1
}
# Define a function to show debug info
function debug_info {
echo ""
echo "Showing debugging information..."
echo ""
echo "Variable values:"
echo '$FILE_DIR' "is $FILE_DIR"
echo '$FILE_NAME' "is $FILE_NAME"
echo '$FILE_NAME_NO_SUFFIX' "is $FILE_NAME_NO_SUFFIX"
echo '$FILE_MD5' "is $FILE_MD5"
echo '$RELATIONSHIP_ID' "is $RELATIONSHIP_ID"
echo '$PACKAGE_DIR' "is $PACKAGE_DIR"
echo '$PACKAGE_NAME' "is $PACKAGE_NAME"
echo '$ZIP_FILE_PATH' "is $ZIP_FILE_PATH"
echo '$UUID' "is $UUID"
echo '$XML_DIR' "is $XML_DIR"
echo '$RELS_DIR' "is $RELS_DIR"
echo '$RELS_FILE' "is $RELS_FILE"
echo ""
echo "Contents of content types file:"
cat "$PACKAGE_DIR/[Content_Types].xml"
echo ""
echo "Contents of .rels file:"
cat "$RELS_FILE"
echo ""
echo "Contents of package directory:"
tree -a "$PACKAGE_DIR"
echo ""
echo "Contents of .frmx zip file:"
unzip -l ../$PACKAGE_NAME.frmx
echo ""
exit 1
}
while getopts ":fh:" OPT; do
case "$OPT" in
f) FORCE_OVERWRITE=true
;;
h) show_help
;;
*) show_help
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
# Store the operand as the name of the application
FILE_PATH=$@
# Show usage if no parameter is passed
if [[ -z "$FILE_PATH" ]]; then
show_help
fi
# Show error message and quit if file does not exist
if [[ ! -f $FILE_PATH ]]; then
echo "File $FILE_PATH does not exist. Quitting."
exit 1
fi
# Set up variables
FILE_DIR="$(dirname "$FILE_PATH")"
FILE_NAME="$(basename "$FILE_PATH")"
FILE_NAME_NO_SUFFIX="$(basename "$FILE_PATH" .xml)"
FILE_MD5="$(openssl md5 "$FILE_PATH" | awk '{ print $NF }')"
RELATIONSHIP_ID="R$FILE_MD5"
PACKAGE_DIR="$FILE_DIR/$FILE_NAME_NO_SUFFIX"
PACKAGE_NAME="$FILE_NAME_NO_SUFFIX"
ZIP_FILE_PATH="$FILE_DIR/$PACKAGE_NAME.frmx"
UUID="$(uuidgen)"
XML_DIR="$PACKAGE_DIR/$UUID"
RELS_DIR="$PACKAGE_DIR/_rels"
RELS_FILE="$RELS_DIR/.rels"
# Prompt to overwrite if package directory already exists
if [[ -d "$PACKAGE_DIR" ]]; then
if [[ ! "$FORCE_OVERWRITE" = true ]]; then
read -p "Delete existing package directory $PACKAGE_DIR (Y/n)? " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Package directory $PACKAGE_DIR already present. Quitting."
exit 1
fi
fi
echo "Deleting directory $PACKAGE_DIR..."
trash "$PACKAGE_DIR"
fi
# Create package directory
echo "Creating new directory for package $PACKAGE_DIR..."
mkdir -p "$PACKAGE_DIR"
# # Create [Content_Types] file
echo "Creating [Content_Types].xml file to specify content types..."
cp -f "$SCRIPT_DIR/templates/content_types.xml" "$PACKAGE_DIR/[Content_Types].xml"
# Create directory and add XML file for form
echo "Creating directory for XML file..."
mkdir -p "$XML_DIR"
echo "Copying XML file into XML directory..."
cp -f "$FILE_PATH" "$XML_DIR/"
# Create _rels directory and add .rels file
echo "Creating _rels directory to store relationships..."
mkdir -p "$RELS_DIR"
echo "Creating .rels file to specify relationships..."
cp -f "$SCRIPT_DIR/templates/rels.xml" "$RELS_FILE"
RELATIONSHIP="<Relationship Type=\"http://schemas.openxmlformats.org/ip2sg/package/relationships/efilingapplication\" Target=\"/$UUID/$FILE_NAME\" Id=\"$RELATIONSHIP_ID\" />"
echo "Adding relationship to .rels file..."
sed -i '' '/<\/Relationships>/i \
\ \'"$RELATIONSHIP"'
' "$RELS_FILE"
# Delete the .DS_Store file generated by Mac OS X
if [[ -f "$PACKAGE_DIR/.DS_Store" ]]; then
echo "Removing .DS_Store file..."
trash "$PACKAGE_DIR/.DS_Store"
fi
# Prompt to overwrite if zip file already exists
if [[ -f "$ZIP_FILE_PATH" ]]; then
if [[ ! "$FORCE_OVERWRITE" = true ]]; then
read -p "Delete existing zip file $PACKAGE_NAME.frmx (Y/n)? " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Zip file $PACKAGE_NAME.frmx already present. Quitting."
exit 1
fi
fi
echo "Deleting file $PACKAGE_NAME.frmx..."
trash "$ZIP_FILE_PATH"
fi
# Create the zip file from inside package directory
echo "Making the zip file..."
cd "$PACKAGE_DIR" && zip -r "$ZIP_FILE_PATH" .
# Delete the package directory
echo "Deleting the package directory..."
trash "$PACKAGE_DIR"
debug_info