#!/usr/bin/env bash

if [[ $# -lt 2 ]]; then
  echo "usage: $(basename $0) infile outfile" 2>&1
  exit 1
fi

if [[ -z $(which AtomicParsley) ]]; then
  echo "Error: AtomicParsley is not installed?" 2>&1
  exit 1
fi

if [[ ! -d $(dirname "$2") ]]; then
  echo "Error: destination folder does not exist" 2>&1
  exit 1
fi

fullname=$(dscl . read /Users/${USER} RealName | tail -n 1)
fullname=$(echo ${fullname#\b})
replace=$(printf "\0%${#fullname}s")

AtomicParsley "$1" \
  --manualAtomRemove "moov.udta.meta.ilst.apID" \
  --manualAtomRemove "moov.udta.meta.ilst.cnID" \
  --manualAtomRemove "moov.udta.meta.ilst.atID" \
  --manualAtomRemove "moov.udta.meta.ilst.plID" \
  --manualAtomRemove "moov.udta.meta.ilst.geID" \
  --manualAtomRemove "moov.udta.meta.ilst.sfID" \
  --manualAtomRemove "moov.udta.meta.ilst.akID" \
  --manualAtomRemove "moov.udta.meta.ilst.purd" \
  --manualAtomRemove "moov.udta.meta.ilst.----.name:[iTunNORM]" \
  --manualAtomRemove "moov.udta.meta.ilst.----.name:[iTunSMPB]" \
  --manualAtomRemove "moov.udta.meta.ilst.----.name:[iTunMOVI]" \
  --output "$2"

if [[ $? ]]; then
  sed -i '' "s/${fullname}/${replace}/" "$2"
fi

