#!/usr/bin/env sh
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

wget https://cwe.mitre.org/data/xml/views/2000.xml.zip
unzip 2000.xml.zip
perl -lne 'print $1 if /((?<= ID=").*?" Name=".*?(?="))/' 2000.xml | sed 's/" Name="/| /' | awk '{print "CWE-" $0}' > cwe.csv
rm 2000.xml*
echo "Created cwe.csv. Importing into database."
while ! nc -z database 3306 ; do
  echo 'Waiting for database:3306...'; sleep 5;
done;
echo "load data local infile 'cwe.csv' into table cwe.cwe_data fields terminated by '|' lines terminated by '\n' (cwe_id, cwe_name)" | mysql --local-infile=1 -hdatabase -uroot -ppass
echo "Done"
