#!/bin/sh

puzChoice="$1"
localPuzzleDir="$HOME/.puzzles"
systemPuzzleDir="/var/puzzles"

if [ -n "$puzChoice" ]; then
  if [ "$puzChoice" = "-h" ]; then
    printf "Run program without arguments for list of puzzles. Add puzzle name as argument to start puzzle.\\n"
  else
    sysPuzChoiceFile="${systemPuzzleDir}/${puzChoice}.puz"
    localPuzChoiceFile="${localPuzzleDir}/${puzChoice}.puz" 
    if [ -f "$sysPuzChoiceFile" ]; then
      if [ ! -f "$localPuzChoiceFile" ]; then
        mkdir -p "$localPuzzleDir"
        cp "$sysPuzChoiceFile" "$localPuzChoiceFile"
      fi
      cursewords "$localPuzChoiceFile"
    else
      printf "puzzle not found.\\n"
    fi
  fi
else
  for f in "${systemPuzzleDir}"/*.puz; do
    puzzleFilename=$(basename "$f")
    puzzleName=${puzzleFilename%.*}
    printf "%s\\n" "$puzzleName"
  done
fi
