/**************************************************************************************
 *
 *     Project:    kvmstoreserver
 *     File name:  stage0_ext4.go
 *     Version:    1.0.0
 *     Date:       2018-01-14
 *
 *     Autor:      Bogdanovic Theodor
 *     Contact:    t.bogdanovic@hotmail.com
 *
 *     License:    GPLv3
 *
 **************************************************************************************/
/**************************************************************************************
 * Copyright 2018 Bogdanovic Theodor
 *
 * This file is part of kvmstoreserver.
 *
 * kvmstoreserver is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * kvmstoreserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with kvmstoreserver.  If not, see <http://www.gnu.org/licenses/>.
 **************************************************************************************/
/**************************************************************************************
 *  Further explanations:  This source code is under the license specified by the
 *                         Autor above. Core components or other packages are still
 *                         in source code form, under the license specified by the
 *                         respective author.
 **************************************************************************************/

package main

import (
	"fmt"
	"os"
	"sync"
	"syscall"
	"time"

	log "github.com/judwhite/logrjack"
)

//*****************************************************************************
//*
//*      Private Functions: Loader Functions
//*
//*****************************************************************************

// This controller controls the removal of OS signals. So a ctrl + c or SIGTERM
// can be intercepted and a clean shutdown initiated
func initializeShutdownWatcher() {

	shutdown = &shutdownController{signalChan: make(chan os.Signal, 1)}
}

//*****************************************************************************
//*
//*      Variable declarations
//*
//*****************************************************************************

var shutdown *shutdownController

//*****************************************************************************
//*
//*      Struct: shutdownCotroller
//*
//*****************************************************************************

// shutdownController ...
type shutdownController struct {
	signalChan chan os.Signal
	wg         sync.WaitGroup
}

//******************** Methods ************************************************

// This method is started from the outside as goroutine
func (slf *shutdownController) captureSignal() {

	sig := <-slf.signalChan
	if sig == syscall.SIGINT ||
		sig == syscall.SIGTERM {
		slf.gracefulShutdown()
	}
}

func (slf *shutdownController) gracefulShutdown() {

	slf.wg.Add(1)
	httpFrontend.stop()

	fmt.Println("Start graceful shutdown procedure - Please wait...")
	log.Info("Started graceful shutdown procedure")
	time.Sleep(time.Second * 5)
	fmt.Println("The appserver is down - Bye :)")
	log.Info("The server ist down")

	slf.wg.Done()
}
