#!/bin/sh

if [ -z "$1" ]; then
    # If no option is specified, search for the launcher directory.
    LAUNCHER_DIR="";
    for base in /app /opt /usr/local /usr/pkg /usr; do
        for sub in "share/suika3/launcher" \
                   "share/doc/suika3/launcher" \
                   "share/examples/suika3/launcher"; do
            target="$base/$sub";
            if [ -d "$target" ]; then
                LAUNCHER_DIR="$target";
                break 2;
            fi
        done
    done;
    if [ -z "$LAUNCHER_DIR" ]; then
        echo "No launcher directory found.";
        exit 1;
    fi;
    cd "$LAUNCHER_DIR";
    exec suika3;
else
    # If an option is specified.

    # If a directory is specified and can be accessed.
    DIR="$1";
    if [ -d "$DIR" ]; then
        cd "$DIR";
        exec suika3;
    fi;

    # If a file is specified and can be accessed.
    if [ -f "$DIR" ]; then
        # Get the directory.
        DIR=$(dirname "$DIR");

        # If the directory can be accessed.
        if [ -d "$DIR" ]; then
            cd "$DIR";
            exec suika3;
        fi;
    fi;

    # Otherwise, use portal to access files/directories.
    exec suika3 --open;
fi
