#!/bin/sh
# Copyright (c) 2017 Guillaume Outters
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

SCRIPTS="`command -v "$0"`" ; [ -x "$SCRIPTS" -o ! -x "$0" ] || SCRIPTS="$0" ; case "`basename "$SCRIPTS"`" in *.*) true ;; *sh) SCRIPTS="$1" ;; esac ; case "$SCRIPTS" in /*) ;; *) SCRIPTS="`pwd`/$SCRIPTS" ;; esac ; delie() { while [ -h "$SCRIPTS" ] ; do SCRIPTS2="`readlink "$SCRIPTS"`" ; case "$SCRIPTS2" in /*) SCRIPTS="$SCRIPTS2" ;; *) SCRIPTS="`dirname "$SCRIPTS"`/$SCRIPTS2" ;; esac ; done ; } ; delie ; SCRIPTS="`dirname "$SCRIPTS"`" ; delie
. "$SCRIPTS/garg.sh"

auSecours()
{
	nom="`basename "$0"`"
	cat >&2 << TERMINE
# $nom
# Ajoute des fichiers à une liste type cmus
# © 2017 Guillaume Outters

Utilisation: $nom [-s <suffixes>] (-l <liste>)* <fichier>*
  -s <suffixes>
    Suffixes à prendre, séparés par des espaces, deux points ou virgules.
  -l <liste>
    Bête fichier liste (de fichiers). Les fichiers trouvés seront ajoutés à
    cette liste s'ils n'y sont déjà.
  <fichier>
    Fichier à ajouter à la liste s'il n'y est déjà. Si le fichier est un
    dossier, tous ses fichiers seront ajoutés.
TERMINE
	exit 1
}

analyserParametres()
{
	paramsFind=
	listes=
	suffixes=
	while [ $# -gt 0 ] ; do
		case "$1" in
			-h) auSecours ;;
			-l) shift ; garg -a listes "$1" ;;
			-s) shift ; suffixes="$suffixes `echo "$1" | tr ':,' '  '`" ;;
			*)
				case "$1" in
					/*) f="$1" ;;
					*) f="`pwd`/$1" ;;
				esac
				garg -a paramsFind "` echo "$f" | sed -e 's#//*$##'`"
				;;
		esac
		shift
	done
	
	[ ! -z "$paramsFind" ] || auSecours
	
	garg -a paramsFind -type f
	
	if [ ! -z "$suffixes" ] ; then
		sepParamsFind="("
		for suffixe in $suffixes ; do
			garg -a paramsFind "$sepParamsFind" -name "*.$suffixe"
			sepParamsFind="-or"
		done
		garg -a paramsFind ")"
	fi
}

faire()
{
	if [ -z "$listes" ] ; then
		garg paramsFind feindre
	else
		_garg -1 feindreEtAjouter : listes paramsFind
	fi
}

feindre()
{
	find "$@"
}

# Trouve des fichiers, et ajoute ceux qui ne sont pas dans la liste existante en fin de liste, sans toucher ce qui précède (ainsi les nouvelles "strates" générées par chaque feindre viennent-elles s'accumuler en fin de fichier).
feindreEtAjouter()
{
	liste="$1" ; shift
	touch "$liste"
	sort -u < "$liste" > "$liste.existants"
	feindre "$@" | sort > "$liste.trouves"
	diff "$liste.existants" "$liste.trouves" | grep '^> ' | cut -c 3- > "$liste.ajouts"
	cat "$liste.ajouts" >> "$liste"
	rm "$liste.existants" "$liste.trouves" "$liste.ajouts"
}

analyserParametres "$@"
faire
