#!/usr/bin/env bash

# Copyright (C) 2015 Toshinori Sato (@overlast)
#
#       https://github.com/neologd/mecab-ipadic-neologd
#
# Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

BASEDIR=$(cd $(dirname $0);pwd)
PROGRAM_NAME=install-mecab-ipadic-neologd
ECHO_PREFIX="[${PROGRAM_NAME}] :"

VERSION="install-mecab-ipadic-neologd version 0.0.1"

usage() {
    echo "Usage: $PROGRAM_NAME [OPTIONS]"
    echo "  This script is the installer of mecab-ipadic-neologd"
    echo
    echo "Options:"
    echo "  -h, --help"
    echo
    echo "  -v, --version"
    echo
    echo "  -p, --prefix /path/to/install/dirctory"
    echo "     Set any directry path where you want to install"
    echo
    echo "  -y, --forceyes"
    echo "     If you want to install regardless of the result of test"
    echo
    echo "  -u, --user"
    echo "     If you want to install to user directory"
    echo
    exit 1
}

INSTALL_PATH_PREFIX=""
IS_FORCE_YES=0
IS_AS_USER=0

for OPT in "$@"
do
    case "$OPT" in
        '-h'|'--help' )
            usage
            exit 1
            ;;
        '-v'|'--version' )
            echo $VERSION
            exit 1
            ;;
        '-p'|'--prefix' )
            if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
                echo "${PROGRAM_NAME}: option requires an argument -- $1" 1>&2
                usage
                exit 1
            fi
            INSTALL_PATH_PREFIX="$2"
            shift 2
            ;;
        '-y'|'--fourceyes' )
            IS_FORCE_YES=1
            shift 1
            ;;
        '-u'|'--user' )
            IS_AS_USER=1
            shift 1
            ;;
        -*)
            echo "${PROGRAM_NAME}: illegal option -- '$(echo $1 | sed 's/^-*//')'" 1>&2
            usage
            exit 1
            ;;
        *)
            if [[ ! -z "$1" ]] && [[ ! "$1" =~ ^-+ ]]; then
                #param=( ${param[@]} "$1" )
                param+=( "$1" )
                shift 1
            fi
            ;;
    esac
done

echo "$ECHO_PREFIX Start.."

echo "$ECHO_PREFIX Check the exists of libraries"
COMMANDS=(find sort head cut egrep mecab mecab-config make curl sed cat diff tar unxz xargs grep iconv)
for COMMAND in ${COMMANDS[@]};do
    if [ ! `which ${COMMAND}` ]; then
        echo "$ECHO_PREFIX ${COMMAND} is not found."
        exit
    else
        echo "$ECHO_PREFIX ${COMMAND} => ok"
    fi
done

if [ -z ${INSTALL_PATH_PREFIX} ]; then
    INSTALL_PATH_PREFIX=`mecab-config --dicdir`"/mecab-ipadic-neologd"
fi

echo ""
echo "$ECHO_PREFIX mecab-ipadic-neologd will be install to ${INSTALL_PATH_PREFIX}"
echo ""
sleep 3

echo "$ECHO_PREFIX Make mecab-ipadic-neologd"
${BASEDIR}/../libexec/make-mecab-ipadic-neologd.sh -p ${INSTALL_PATH_PREFIX}

echo "$ECHO_PREFIX Get results of tokenize test"
${BASEDIR}/../libexec/test-mecab-ipadic-neologd.sh

function wanna_install(){
    while true;do
        echo ""
        echo "$ECHO_PREFIX Please check the list of differences in the upper part."
        echo ""
        echo "$ECHO_PREFIX Do you want to install mecab-ipadic-neologd? Type yes or no."
        read answer
        case $answer in
            yes)
                return 0
                ;;
            no)
                echo "$ECHO_PREFIX Quit from installing process"
                echo "$ECHO_PREFIX Finish.."
                exit
                ;;
            *)
                echo -e "cannot understand $answer.\n"
                ;;
        esac
    done
}

if [ ${IS_FORCE_YES} -eq 0 ]; then
    wanna_install
fi

echo "$ECHO_PREFIX OK. Let's install mecab-ipadic-neologd."
${BASEDIR}/../libexec/install-mecab-ipadic-neologd.sh -p ${INSTALL_PATH_PREFIX} -u ${IS_AS_USER}

echo "$ECHO_PREFIX Finish.."
