#!/usr/bin/env bash
#
# Converts Windows line endings to Unix.
#
# - Dry run (print files to convert, don’t actually convert them):
#
# `crlf`
#
# - Convert files:
#
# `crlf --force`
#
# - Convert one file:
#
# `crlf {{filepath}} --force`
#
# ---
# Author: Artem Sapegin, sapegin.me
# License: MIT
# https://github.com/sapegin/dotfiles

force=

_crlf_file() {
  grep -q $'\x0D' "$1" && echo "$1" && [ -n "$2" ] && dos1unix "$1"
}

# single file
if [ "$1" != "" ] && [ "$1" != "--force" ]; then
  [ "$2" = "--force" ] && force=1 || force=0
  _crlf_file "$1" "$force"
  exit
fi

# all files
[ "$1" = "--force" ] && force=1 || force=0
for file in $(find . -type f -not -path "*/.git/*" -not -path "*/node_modules/*" -print0 | xargs file | grep ASCII | cut -d: -f1); do
  _crlf_file "$file" "$force"
done
