#!/bin/bash

APP="wire-desktop"
NPM_DIR="/usr/bin/npm"
BIN_DIR="/home/${USER}/wire-packages"
PACKAGE_DEFAULT_DIR="$(pwd)/wrap/dist"
SUPER_USER_CMD="sudo"
REPO_URL="https://github.com/wireapp/${APP}.git"

# Colors
RED='\033[0;31m'
LRED="\033[1;31m"
BLUE="\033[0;34m"
LBLUE="\033[1;34m"
GREEN="\033[0;32m"
LGREEN="\033[1;32m"
YELLOW="\033[1;33m"
CYAN="\033[0;36m"
LCYAN="\033[1;36m"
PURPLE="\033[0;35m"
LPURPLE="\033[1;35m"
BWHITE="\e[1m"
NC='\033[0m' # No Color

if [[ $EUID -eq 0 ]]; then
    printf "${LRED}Do not run this script as root${NC}\n" 1>&2
    exit 1
fi

check_repo(){
    if [ ! -d ${APP} ];then
        git clone ${REPO_URL}

    elif [ -d .git ];then
        printf "\n${LRED}This is already a Git repository!${NC}\n"

    fi
}
check_sudo(){
    if [ ! -f /usr/bin/sudo ];then
        SUPER_USER_CMD="su -c"
    fi
}
apt_install(){
    local PACKAGES="git make nodejs npm"

    printf "${LCYAN}Fetching dependencies${NC}\n\n"
    ${SUPER_USER_CMD} apt install ${PACKAGES}
}

dnf_install(){
    local PACKAGES='rpm-build rpmdevtools make nodejs npm nss libXScrnSaver git'
                                
    printf "${LCYAN}Fetching dependencies${NC}\n\n"
    ${SUPER_USER_CMD} dnf install ${PACKAGES}
}

zypper_install(){
    local PACKAGES="make git nodejs npm"

    printf "${LCYAN}Fetching dependencies${NC}\n\n"
    ${SUPER_USER_CMD} 'zypper install "${PACKAGES}"'
}

check_deps(){
    declare -a FIND_PACKAGE_MANAGER=(
                                    "apt"
                                    "dnf"
                                    "zypper")

    for i in ${FIND_PACKAGE_MANAGER[@]};do
        PACKAGE_MANAGER=$(which ${i} 2> /dev/null)
        echo "$PACKAGE_MANAGER"
        if [ -n "${PACKAGE_MANAGER}" ];then
            break
        fi
    done   

    if [[ "${PACKAGE_MANAGER}" = "/usr/bin/apt" ]];then
        apt_install

        elif [ "${PACKAGE_MANAGER}" = "/usr/bin/dnf" ];then
            dnf_install

        elif [[ "${PACKAGE_MANAGER}" = "/usr/bin/xbps-install" ]];then
            xbps_install

        elif [[ "${PACKAGE_MANAGER}" = "/usr/bin/zypper" ]];then
            zypper_install 
    fi
            
}

build_deb(){
    local ARCH="x64"
    local TARGET="deb"
    local UPDATE_OPTS="'clean:linux' 'update-keys' 'release-prod'"
    local BUILD_OPTS="--arch=${ARCH} --target=${TARGET} 'electronbuilder:linux_other'"

    npm install
    npm update
    sudo npm install -g grunt
  	sudo grunt 'clean:linux' 'update-keys' 'release-prod' 'bundle'
    sudo grunt --arch=${ARCH} --target=${TARGET} 'electronbuilder:linux_other'
    sudo  mv -v ${PACKAGE_DEFAULT_DIR}/*.${TARGET} ${BIN_DIR}
}

build_rpm(){
    local ARCH="x64"
    local TARGET="rpm"
    local UPDATE_OPTS="'clean:linux' 'update-keys' 'release-prod'"
    local BUILD_OPTS="--arch=${ARCH} --target=${TARGET} 'electronbuilder:linux_other'"

    npm install
    npm update
    npm install grunt
    grunt 'clean:linux' 'update-keys' 'release-prod'
    grunt --arch=${ARCH} --target=${TARGET} 'electronbuilder:linux_other'
    mv -v ${PACKAGE_DEFAULT_DIR}/*.${TARGET} ${BIN_DIR}
}

git_pull(){
    if [ -d .git ];then
        git stash
        git pull origin master
    fi
}

main(){
    if [ ! -d ${BIN_DIR} ];then
        mkdir -pv ${BIN_DIR}
    fi

    

    case $1 in 
        all | -a) check_deps && git_pull && build_rpm && build_deb
        ;;

        deb | -d) git_pull && build_deb
        ;;

        rpm | -r) check_deps && git_pull && build_rpm
        ;;

        clone | -c) check_repo
        ;;

        *)
        printf "\n\n"
        printf "Usage: $0 [-a/all | -d/deb | -r/rpm ]\n\n"

        printf " -a | all      build all package formats\n"
        printf " -d | deb      build only DEB package format\n"
        printf " -r | rpm      build only RPM package format\n"
        printf " -c | clone    clone 'wire-desktop' from Github\n\n"
        ;;

    esac

}

main ${1}

