#! /bin/bash

partieOuverte="~/Nom_Prenom/Assos/EnCours/partieOuverte.txt"

if [ $# -neq 1 ] ; then
    echo "Nombre de paramètres incorrects" >&2
    exit 1
fi


if [ ! -f $partiesOuvertes ] ; then
    touch $partiesOuvertes
fi

./P.sh partiesOuvertes.lock
nbparties=$(wc -l $parties | cut -d ' ' -f 1)

if [ $nbparties -gt 0 ] ; then
    adversaire=$(head -n 1 $partiesOuvertes)
    tail -n $nbparties $partiesOuvertes > $partiesOuvertes
    ./V.sh partiesOuvertes.lock

    ./pingpongParty.sh $1 $adversaire &

else
    ./pingpongParty.sh $1 &
    echo "$1" >> $partiesOuvertes
    ./V.sh partiesOuvertes.lock
fi     
