#!/bin/bash

# Create a flag for verifying installation
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
INSTALL_FLAG=$SCRIPT_DIR/.linux.ubuntu1404.installed

tarname="*.tgz"

#######################################################
# Checking if there this script has already been complete.
#######################################################
function check_flag() {
    if [[ -f "$INSTALL_FLAG" ]]; then
        echo >&2 "linux.ubuntu1404 is already installed!"
        exit 117;  # Structure needs cleaning
    fi
}


#######################################################
# Download using wget and then checksum the downloaded files.
#
# It is important to verify that the downloaded files
# are the files are the same ones as expected.
# This function provides an outline of how to checksum files,
# but will need to be updated with the specific hashes/file names
# that have been downloaded.
#
# This function assumes that the passed in hashes are SHA-256
#######################################################
function wget_and_checksum() {
    downloads=("$@")
    # Uses 2D arrays in bash: https://stackoverflow.com/a/44831174
    declare -n d
    for d in "${downloads[@]}";
    do
        wget "${d[0]}"
        echo "${d[1]}  ${d[2]}" | shasum -a 256 --check
    done
}

#######################################################
# A function to help users clean up a partial installation
# in the event of an error.
#######################################################
function cleanup() {
    echo "Cleaning up linux.ubuntu1404 install"
    rm -rf "vm_resources/debs/$tarname"
    rm -rf $INSTALL_FLAG
    exit 1
}
trap cleanup ERR

# Ensure we only complete the script once
check_flag


#######################################################
# Uncomment if there is data/VM resources/images to download.
# `file1`, `file2`, etc. should be space separated strings of
# (URL SHASUM-256 FILENAME).
#
# We recommend that explicit versions are used for all Images/VMRs to prevent
# possible differences between instances of a given Model Component.
# Please be mindful of the software versions as it can have unintended
# consequences on your Emulytics experiment.
#
# We require checksums of the files to assist users in verifying
# that they have downloaded the same version.
#######################################################
# Be sure to use SHA-256 hashes for the checksums (e.g. shasum -a 256 <file>)
# Create the set of php5-fpm debian packages
dirname="php5-fpm"
mkdir "$dirname"
pushd "$dirname"
file1=("https://launchpad.net/~ubuntu-security/+archive/ubuntu/ppa/+build/8849399/+files/libxml2_2.9.1+dfsg1-3ubuntu4.7_amd64.deb" "7b51243d1f3ff7374c0ac0433b322ab6e3c803db7afc6a2a5bcdc19ce37c7ea6" "libxml2_2.9.1+dfsg1-3ubuntu4.7_amd64.deb")
file2=("http://launchpadlibrarian.net/420574937/php5-common_5.5.9+dfsg-1ubuntu4.29_amd64.deb" "1f5d38b52e0325e3d2f6cea9f749e9404159c0f14411c6affc66b9c685e98039" "php5-common_5.5.9+dfsg-1ubuntu4.29_amd64.deb")
file3=("http://launchpadlibrarian.net/166644209/php5-json_1.3.2-2build1_amd64.deb" "3e6543d597f85e12d7620997c4ca053ab4dcbfd570ba40a80dbb14f097fdcd2d" "php5-json_1.3.2-2build1_amd64.deb")
file4=("http://launchpadlibrarian.net/420574942/php5-fpm_5.5.9+dfsg-1ubuntu4.29_amd64.deb" "27b856825a0a02dcbcf12972db8d213a55036b6b155c856131011815bd67a94e" "php5-fpm_5.5.9+dfsg-1ubuntu4.29_amd64.deb")
file5=("http://launchpadlibrarian.net/323668248/libgnutls-openssl27_2.12.23-12ubuntu2.8_amd64.deb" "db5593d5ed61219ebf9f6a0959962496fbcfe50fe4ba53bbabc0b55a62cf79bd" "libgnutls-openssl27_2.12.23-12ubuntu2.8_amd64.deb")
file6=("http://launchpadlibrarian.net/323668244/libgnutls26_2.12.23-12ubuntu2.8_amd64.deb" "75417c39414ab8919ee02eb4f1761c412d92c10a9ac1839fcd1e04bcfc85f607" "libgnutls26_2.12.23-12ubuntu2.8_amd64.deb")
file7=("http://launchpadlibrarian.net/375021363/libgcrypt11_1.5.3-2ubuntu4.6_amd64.deb" "39ab5032aa4597366d2c33f31e06ba91ba2ad79c8f68aff8ffcfab704b256a2c" "libgcrypt11_1.5.3-2ubuntu4.6_amd64.deb")
files=(file1 file2 file3 file4 file5 file6 file7)
wget_and_checksum "${files[@]}"
echo "Downloaded and checksummed all files!"
popd

tar -czf "${dirname}.tgz" "$dirname"
rm -rf "$dirname"

# Save off nginx packages
dirname="nginx"
mkdir "$dirname"
pushd "$dirname"
file1=("http://launchpadlibrarian.net/396275967/nginx-full_1.4.6-1ubuntu3.9_amd64.deb" "096388ee57570971697259973e3f6c4845f4716b86de4dce2b56108af95ecb55" "nginx-full_1.4.6-1ubuntu3.9_amd64.deb")
file2=("http://launchpadlibrarian.net/396275654/nginx-common_1.4.6-1ubuntu3.9_all.deb" "5b0876c6daaaf4b38b79ac868da4da5292ae1cacb31f240e21576e4daaa4c2ea" "nginx-common_1.4.6-1ubuntu3.9_all.deb")
file3=("http://launchpadlibrarian.net/396275652/nginx_1.4.6-1ubuntu3.9_all.deb" "f378c38389dc640d44f3b2845fd7a5aaef57493fac1ee15bff734f82a172a308" "nginx_1.4.6-1ubuntu3.9_all.deb")
file4=("http://launchpadlibrarian.net/419388713/libxslt1.1_1.1.28-2ubuntu0.2_amd64.deb" "cf8f88e9618fba98eef83e6f88a7e2c2480e2bbaa302f99858c970570de7fe94" "libxslt1.1_1.1.28-2ubuntu0.2_amd64.deb")
file5=("http://launchpadlibrarian.net/303890383/libxpm4_3.5.10-1ubuntu0.1_amd64.deb" "800582e71ad963032c1aa27df85f05a50e3ecbb4d2be7a545888d9a2f80ba249" "libxpm4_3.5.10-1ubuntu0.1_amd64.deb")
file6=("http://launchpadlibrarian.net/161837330/libvpx1_1.3.0-2_amd64.deb" "cf537cdbb2fef2d2bf25664651030c8de2083916f4b4a8bd59c4350e5f21e190" "libvpx1_1.3.0-2_amd64.deb")
file7=("http://launchpadlibrarian.net/414706924/libtiff5_4.0.3-7ubuntu0.11_amd64.deb" "2219cdb57de2893a02b6c5cef554e97fce61018ad6d1277203f8bc29a8ce1dc4" "libtiff5_4.0.3-7ubuntu0.11_amd64.deb")
file8=("http://launchpadlibrarian.net/144550460/libjpeg8_8c-2ubuntu8_amd64.deb" "baaecbc8e7ef55fc1887365721a7771f7d533fabca38fca878668b9c8f7ee13f" "libjpeg8_8c-2ubuntu8_amd64.deb")
file9=("http://launchpadlibrarian.net/172895541/libjbig0_2.0-2ubuntu4.1_amd64.deb" "11e85606a22866224f2ce7283feaba0c207d32882ea98d946254def0ecbee63e" "libjbig0_2.0-2ubuntu4.1_amd64.deb")
file10=("http://launchpadlibrarian.net/377330737/libjpeg-turbo8_1.3.0-0ubuntu2.1_amd64.deb" "14bc0c28aa8218b370c40cf3e6a5ee39cf15f70b08be1293c94da91b55c2ff1e" "libjpeg-turbo8_1.3.0-0ubuntu2.1_amd64.deb")
file11=("http://launchpadlibrarian.net/413076346/libgd3_2.1.0-3ubuntu0.11_amd64.deb" "6442af257f59870bf45824a355280c650ae021297eebcffe84336d897c9a083f" "libgd3_2.1.0-3ubuntu0.11_amd64.deb")
file12=("http://launchpadlibrarian.net/162131271/fonts-dejavu-core_2.34-1ubuntu1_all.deb" "d7eb9a90ecf62b85dcf2f3bb84a0d66e41fef81141240c73e5cab3351553dc03" "libgd3_2.1.0-3ubuntu0.11_amd64.deb")
file13=("http://launchpadlibrarian.net/279293791/libfontconfig1_2.11.0-0ubuntu4.2_amd64.deb" "ccc0f4ecb0f66a27dbb74ec98fba8741567ff2118eab8559b9f5ef01a6a26e03" "libfontconfig1_2.11.0-0ubuntu4.2_amd64.deb")
file14=("http://launchpadlibrarian.net/279293672/fontconfig-config_2.11.0-0ubuntu4.2_all.deb" "544544d3da955ffbb276230d142875b33e60c2b7747c429542f4c4da5c41bda4" "fontconfig-config_2.11.0-0ubuntu4.2_all.deb")
files=(file1 file2 file3 file4 file5 file6 file7 file8 file9 file10 file11 file12 file13 file14)
wget_and_checksum "${files[@]}"
echo "Downloaded and checksummed all files!"
popd

tar -czf "${dirname}_trusty_debs.tgz" "$dirname"
rm -rf "$dirname"



# Move all tars into the right directory
mv *.tgz ./vm_resources/debs/

# Set the flag to notify of successful completion
touch $INSTALL_FLAG
