<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use ZipArchive;

class InstallController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */


    function rrmdir($dir) {
        if (is_dir($dir)) {
            $objects = scandir($dir);
            foreach ($objects as $object) {
                if ($object != "." && $object != "..") {
                if (is_dir($dir. DIRECTORY_SEPARATOR .$object) && !is_link($dir."/".$object))
                    $this->rrmdir($dir. DIRECTORY_SEPARATOR .$object);
                else
                    unlink($dir. DIRECTORY_SEPARATOR .$object);
                }
            }
            rmdir($dir);
        }
    }

    public function index()
    {
        return view('pages.install');
    }

    public function extract(Request $request)
    {
        $content = file_get_contents("http://192.168.19.200/installerweb/webinstaller.zip");
        if(file_put_contents("webinstaller.zip", $content)){
            $dir ="./";
            $open =opendir($dir) or die('Folder tidak ditemukan ...!');
            while ($file    =readdir($open)) {
                if($file !='.' && $file !='..'){
                    $fileName = $file;

                }
            }

            if (empty($fileName)) {
                echo "File Zip Installer Kosong.";
            }

            if (!empty($request->extract)) {
                // $this->rrmdir("css");
                // $this->rrmdir("js");
                // $this->rrmdir("webapps");


                if (isset($fileName)) {
                    $zip = new ZipArchive;
                    $res = $zip->open($fileName);
                    if ($res === TRUE) {
                        $zip->extractTo('./');
                        $zip->close();
                        unlink('./webinstaller.zip');
                        return redirect('/install');
                    } else {
                        return redirect()->back()->withErrors('Cannot find the file name "'.$fileName.'" (the file name should include extension (.zip, ...))');
                    }
                }else{
                    return redirect()->back()->withErrors('Please set file name in the "name" param');
                }
            }
        } else {
            return redirect()->back()->withErrors('Cannot get the Web Installer!');
        }
    }

    public function removeInstaller()
    {
        $this->rrmdir("./install");
        return redirect("/");
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
