# moosic/client/cli/main.py - The client portion of the moosic jukebox system.
#
# This is free and unencumbered software released into the public domain.
# 
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
# 
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
# 
# 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 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.
# 
# For more information, please refer to <http://unlicense.org/>

import sys, socket, os, os.path, getopt, xmlrpclib, errno, time, locale, re

from moosic import VERSION
from moosic.utilities import *
from moosic.client.factory import *
from moosic.client.cli.dispatcher import *

# Define the True and False constants if they don't already exist.
try: True
except NameError: True = 1
try: False
except NameError: False = 0

# Giving stderr a shorter name is convenient when printing error messages.
err = sys.stderr

# Set the locale to the user's default settings, if possible.
try: locale.setlocale(locale.LC_ALL, '')
except: pass


def main(argv):
    COMMANDS = get_command_docs() \
        + center_text('This Moosic has Super Cow Powers.', pad_char=' ')
    USAGE = "usage: " + os.path.basename(argv[0]) + \
        " [options] <command>" + '''
    Options:
        -d, --shuffle-dir       When a directory is named on the command line,
                                shuffle the result of recursing through the
                                directory before inserting it into the list.
        -a, --shuffle-args      Shuffle only the arguments explicitly specified
                                on the command line.
        -g, --shuffle-global    Shuffle the entire argument list after
                                directory arguments specified on the command
                                line have been replaced with their contents.
                                This is the default behavior.
        -o, --inorder           Don't shuffle the given filelist at all, and
                                maintain the order specified on the command
                                line.
        -s, --sort              Sort the filelist, regardless of the order
                                specified on the command line.
        -i, --ignore-case       Treat any given regular expressions as if they
                                were case-insensitive.
        -r, --no-recurse        Don't replace directories named on the command
                                line with their contents.
        -n, --non-file-args     Don't change any names given in a filelist.
                                Useful if your filelist consists of URLs or
                                other objects that aren't local files.
        -f, --auto-find         Replace each string in the given filelist with
                                the results of a "fuzzy" search for music files
                                which match that string. (Beware: this can
                                be slow with large music collections.)
        -F, --auto-grep         Replace each string in the given filelist with
                                the results of a regular-expression search for
                                music files which match that string. (Beware:
                                this can be slow with large music collections.)
        -m, --music-dir <dir>   Specifies the directory to search when using the
                                "auto-find" and "auto-grep" features.
                                (Default: ~/music/)