#!/usr/bin/env bash
# This utility pre-allocs a file and adds execution permissions. It also
# removes the resulting file if it is empty after the editor closes.

LD_INTERNAL=1
. $(dirname $(realpath $0))/daisy.source

if [[ -z $1 ]];
then
  echo "No filename specified."
  exit 2
fi

if [[ -z "${EDITOR}" ]];
then
  ched "EDITOR env variable not set! You will have to set it yourself in the next screen."
fi

[[ -e "$1" ]] && existed=1 || existed=0

touch "$1"
chmod +x "$1"
daisy_editor "$1"

if [[ ! -s "$1" && $existed -eq 0 ]]; then
  rm -f "$1"
fi
