#!/bin/bash
#
# Copyright © 2015 Jesse 'Jeaye' Wilkerson
# See licensing in LICENSE file, or at:
#   http://www.opensource.org/licenses/MIT
#
# File: do_install
# Author: Jesse 'Jeaye' Wilkerson

# XXX: Do not call this script directly: use `make install`
set -o errexit
set -o nounset

if [[ $# == 0 ]];
then
  echo "error: Use \`make install\` to install"
  exit 1
fi

uninstall=0
if [[ $# == 1 && "$1" = "undo" ]];
then
  uninstall=1
  echo "Uninstalling from $installdir"
else
  echo "Installing from $PWD to $installdir"
fi

mkdir -p $installdir
for file in $(find "$PWD"/include -name '*.hpp'); do
  out=$installdir/$project/$(echo "$file" | sed "s_^.*${project}/__");

  if [ $uninstall -eq 1 ];
  then
    rm -f "$out";
  else
    mkdir -p $(dirname "$out")
    install -m 0644 "$file" "$out";
  fi
done

if [ $uninstall -eq 1 ];
then
  rm -r "$installdir/$project" || true
fi
echo "Done"
