#!/bin/sh
set -eu

CHANNEL="stable"
PLATFORM="tar"
  
# This script requires a header.

SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"
DEST_DIR="/opt/1Password"
AFTER_INSTALL="after-install.sh"
AFTER_REMOVE="after-remove.sh"

if [ "$(id -u)" != "0" ]; then
  echo "This installer must be run as root. Re-run with sudo."
  exit 1
fi

if [ -d "$DEST_DIR" ]; then
  echo "Error: $DEST_DIR exists. Uninstall 1Password first:" 
  echo
  echo "  sudo $DEST_DIR/$AFTER_REMOVE && sudo rm -rf $DEST_DIR"
  echo    
  exit 1
fi
mkdir -p "$DEST_DIR"

echo "Installing 1Password to $DEST_DIR..."
cp -a "$SCRIPT_DIR"/. "$DEST_DIR"/;

chown -R root:root "$DEST_DIR" 

echo "Running post-installation script..."
sh "$DEST_DIR/$AFTER_INSTALL" 

echo "Installation complete."
exit 0
