#!/usr/bin/env bash
#
# **** License ****
#
# Copyright (C) 2017 by Helm Rock Consulting
#
# 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.
# **** End License ****
#
# Author: Neil Beadle
# Description: Scripts for creating dnsmasq configuration files to redirect dns
# look ups to alternative IPs (blackholes, pixel servers etc.)

export COPY_EXTENDED_ATTRIBUTES_DISABLE=true
export COPYFILE_DISABLE=true

PWD=$(pwd)
VERSION=$(cat VERSION)
OLDVERSION=$(cat OLDVERSION)
PMVER='s/^our $VERSION = q{'${OLDVERSION}'};/our $VERSION = q{'${VERSION}'};/'
NAME='dnsmasq_blklist'
DROPBOX="$HOME/Dropbox/EdgeOS"
BLACKLIST="${NAME}.${VERSION}."
INSTALL="install_${NAME}.v${VERSION}"
LIB="~/Projects/AdBlock/payload/lib/EdgeOS/DNS/Blacklist.pm"
PAYLOAD='payload'
POSTINSTALL="Install_${NAME}"
SED='/usr/local/bin/gsed -i.bak'
SEDCMD='s/Copyright (C) 20../Copyright (C) '$(date +"%Y")'/g'
SEDVER='s/^VERSION=.*$/VERSION=\"'${VERSION}'\"/g'
UPDATE_CPYRT=(LICENSE decompress README.md)
UPDATE_RDME_VER='s/install_'${NAME}'\.v'${OLDVERSION}'/install_'${NAME}'.v'${VERSION}'/g'

if [[ -e "${0##*/}" ]]
then
	cp VERSION OLDVERSION
	BASEDIR="$(pwd)"
	[[ -f ${PAYLOAD}.tgz ]] && rm ${PAYLOAD}.tgz
	[[ -f ${BLACKLIST}setup.tgz ]] && rm ${BLACKLIST}setup.tgz

	cd ${PAYLOAD}
	[[ -f ${POSTINSTALL} ]] && rm ${POSTINSTALL}
	tar zcf ../${PAYLOAD}.tgz --exclude='._*' --exclude='.svn' --exclude='.DS_Store' --exclude='*.bak' --exclude='*~' ./*
	cd ..

# Update README.md instructions with current version
	eval ${SED} \'${PMVER}\' ${LIB}
	eval ${SED} \'${UPDATE_RDME_VER}\' README.md
	eval ${SED} \'${SEDVER}\' decompress decompress_install_Adblock

	echo "#!/usr/bin/env bash" > "${PAYLOAD}/${POSTINSTALL}"
	cat LICENSE decompress_install_Adblock ${PAYLOAD}.tgz >> "${PAYLOAD}/${POSTINSTALL}"
	echo "${POSTINSTALL} created"
	cd ${PAYLOAD}

	chmod 0755 ${POSTINSTALL}

	cp setup remove

# Update Copyright to current year
	for i in $(find . -type f ! -path "./blacklist/*" ! -path "./.git/*" ! -name '*.bak' ! -name '.?*')
	do
		eval ${SED} \'${SEDCMD}\' ${i}
	done

	tar zcf ../${PAYLOAD}.tgz --exclude='._*' --exclude='.svn' --exclude='.DS_Store' --exclude='*.bak' --exclude="./${PAYLOAD}.tgz" --exclude='*~' ./*
	cd ..

	if [[ -e ${PAYLOAD}.tgz ]]; then
		for i in ${UPDATE_CPYRT[@]}
		do
			# Update Copyright to current year
			eval ${SED} \'${SEDCMD}\' ${i}
		done

		echo "#!/usr/bin/env bash" > ${INSTALL}
		cat LICENSE ${PAYLOAD}/shell_functions decompress ${PAYLOAD}.tgz >> ${INSTALL}

	else
		echo "${PAYLOAD}.tgz does not exist"
		exit 1
	fi

	chmod 0755 ${INSTALL}
	echo "${INSTALL} created"

	[[ -f ${INSTALL}.tgz ]] && rm ${INSTALL}.tgz
	tar zcf ${INSTALL}.tgz ${INSTALL}
	[[ -d "${DROPBOX}/" ]] && install -m 0755 ${INSTALL}.tgz "${DROPBOX}/"

# scp the build to our test router for debugging
	[[ ${1} ]] && /usr/bin/scp "${BASEDIR}"/${INSTALL} ${1}.ashcreek.home:/tmp/${INSTALL}
else
	echo "$(basename $0) must be run in the directory where it is located."
fi
exit 0
