#!/usr/bin/perl

#
# Copyright (C) 1999-2026 Maksim Feoktistov.
#
# This file is part of Small HTTP server project.
# Author: Maksim Feoktistov
#
#
# Small HTTP server 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.
#
# Small HTTP server 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 this program.  If not, see https://www.gnu.org/licenses/
#
# Contact addresses for Email:  support@smallsrv.com
#
#
#



 $mf="\n";
 $all='';
 $without{'fakelibs'} = 1;
 $without{'debug'} = 1;
 $without{'TLS_1_3'} = 0;
 $crosspref='';

 $sec=' libgnutls.so libssl.so libcrypto.so';
 $sec64=' libgnutls.so libssl.so libcrypto.so';

 $adv='';

 %var = (
 'CONFIG_BASE' => '/usr/local',
 'CONFIG_CONFIG'  => '/etc/smallsrv/',
 'CONFIG_LOG' => '/var/log/smallsrv/',
 'CONFIG_DATA' => '/var',
 'CONFIG_APPDIR' => '$(CONFIG_BASE)/lib/smallsrv/',
 'CONFIG_SHARE' =>'$(CONFIG_BASE)/share/smallsrv/',
 'WIN_CONFIG_BASE' => 'c:\\shttps\\'
 );

 %tdirs = (
   'i32' => 'o',
   'i64' => 'o64',
   'arm' => 'at',
   'arm64' => 'at',
   'win' => 'wo',

   'x86' => 'o',
   'i386' => 'o',
   'i686' => 'o',
   'x86_64' => 'o64',
   'amd64' => 'o64',
   'arm' => 'at',
   'armv5te' => 'at',
   'arm64' => 'oo',
   'aarch64' => 'oo',
   'n_all' => 'oo',
 );

 %ttargets = (
   'x86' => 'i32',
   'i386' => 'i32',
   'i686' => 'i32',
   'x86_64' => 'i64',
   'amd64' => 'i64',
   'arm' => 'arm',
   'armv5te' => 'arm',
   'arm64' => 'arm64',
   'aarch64' => 'arm64'
 );

 %bld = (
   'x86_64-linux-gnu' => 'i64',
   'i386-linux-gnu' => 'i32',
   'i486-linux-gnu' => 'i32',
   'i586-linux-gnu' => 'i32',
   'i686-linux-gnu' => 'i32',
   'arm-linux-gnueabi' => 'arm',
   'arm-linux-gnu' => 'arm',
   'aarch64-linux-gnu' => 'arm64'
 );


 if($ENV['ARCH'] && $ttargets[$ENV['ARCH']] )
 {
   $all=$ttargets[$ENV['ARCH']];
   $target=1;
 }

 if( $ENV['CROSS_COMPILE'] )
 {
   $cross = 1;
   $crosspref=$ENV['CROSS_COMPILE'];
   $all='n_all';
   $target=1;
 }

 $help = '
   Configure Makefile  to build Small HTTP server.

   keys:

   --target="i32 i64 win arm arm64"   -- select one or more target throught space.
   --gcc="gcc -m32"  -- select compiler for i32
   --gcc64="gcc -m64"  -- select compiler for i64
   --openssl="/path/to/openssl1.1.1/source/dir"  -- direct the source of OpenSSL 1.1.1 for Linux.
   --wingcc="wine c:\\\\MINGW\\\\bin\\\\gcc.exe" -- direct compiler for win version
   --windir="c:\\\\MINGW" -- direct base dir for windows headers and libraries
   --wininc=" -Ic:\\\\MINGW\\\\include" -- direct include flags for windows headers
   --winlib=" -Lc:\\\\MINGW\\\\lib" -- direct lib flags for windows libraries
   --winsslinclude="c:\\\\openssl\\\\include" -- direct include dir for windows OpenSSL
   --winssllib="c:\\\\openssl\\\\lib" -- direct lib dir for windows OpenSSL
   --wingnutlsinclude="c:\\\\openssl\\\\include" -- direct include dir for windows GnuTLS
   --wingnutlslib="c:\\\\openssl\\\\lib" -- direct lib dir for windows GnuTLS
   --armgcc="arm-linux-gnueabi-gcc"  -- direct compiler for arm
   --arminclude="/usr/src/crossarm/include"
   --armlib="/usr/src/crossarm/lib"
   --withoutIPv6        -- IPv4 only
   --withoutFUTEX       -- do not use futex (Linux specifed feature)
   --withoutPOOL        -- use select instead poll
   --withoutMD5         -- without MD5
   --withoutVPN         -- without VPN
   --withoutSYSPASS     -- without system password support
   --withoutTelnet      -- without Telnet (Telnet for ARM and other little platforms)
   --withoutTFTP        -- without TFTP server
   --withoutGenTempSert -- Do not generate temporary certificate
   --commonLog          -- without separate log
   --only_vpncvlient    -- Build VPN Client only
   --withTLS_1_3        -- enable TLS 1.3
   --withoutTLS_1_3     -- disable TLS 1.3
   --withoutfakelibs    -- without fakelibs  (with fakelibs the program can run in another Linux systems)
   --withfakelibs       -- with fakelibs  (with fakelibs the program can run in another Linux systems)
   --debug              -- with debug

   --build=x86_64-linux-gnu
   --prefix=/usr
   --includedir=\${prefix}/include
   --mandir=\${prefix}/share/man
   --infodir=\${prefix}/share/info
   --sysconfdir=/etc
   --localstatedir=/var
   --disable-option-checking
   --disable-silent-rules
   --libdir=\${prefix}/lib/x86_64-linux-gnu
   --runstatedir=/run
   --disable-maintainer-mode
   --disable-dependency-tracking

   <VAR>=<value>

   VARS:
';

 if($#ARGV >=0 )
 {
 L1:
   foreach $_ (@ARGV)
   {
     if(/help/ || /--\?/)
     {
       print  $help;
       foreach $_ ( keys %var )
       {
         print "$_=$var{$_}\n"
       }

       printf '
  This script and Makefile also understands the ARCH, CROSS_COMPILE, DESTDIR environment variables. An example other way to build for Arm64:
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
%s
make

',$0 ;

      exit 0;
     }
     if(/--target=(.*)/)
     {
       $all = $1;
       $target=1;
       next L1;
     }
     if(/--gcc=(.*)/)
     {
       $a = $1;
       $gcc=1;
       $mf .=  "GPP=$a\nGCC=$a\n";
       next L1;
     }
     if(/--gcc64=(.*)/)
     {
       $a = $1;
       $gcc64=1;
       $mf .=  "GPP64=$a\nGCC64=$a\n";
       next L1;
     }
     if(/--winegcc=(.*)/)
     {
       $a = $1;
       $winegcc=1;
       $mf .=  "WINEGPP=$a\nWINEGCC=$a\n";
       $b=$a;
       $b =~ s/[a-z0-9_.]+exe/as.exe/ ;
       $mf .=  "WINEAS=$b\n";
       $b=$a;
       $b =~ s/[a-z0-9_.]+exe/windres.exe/ ;
       $mf .=  "WINERES=$b\n";
       if( $a =~ /[ \t]+([a-zA-Z_0-9.\\\/\-]+)\\bin\\/ )
       {
         $b=$1;
         $mf .=  "MGDIR=$b\n";
       }
       elsif( $a =~ /^([a-zA-Z_0-9.\\\/\-]+)[\\\/]bin[\\\/]/ )
       {
         $b=$1;
         $b =~ s/^wine[ ]+// ;
         $mf .=  "MGDIR=$b\n";
       }
       next L1;
     }
     if(/--windir=(.*)/)
     {
       $a = $1;
       $mf .=  "MGDIR=$a\n";
       next L1;
     }
     if(/--wininc=(.*)/)
     {
       $a = $1;
       $mf .=  "WININC=$a\n";
       next L1;
     }
     if(/--winlib=(.*)/)
     {
       $a = $1;
       $mf .=  "WINLIB=$a\n";
       next L1;
     }
     if(/--winsslinclude=(.*)/)
     {
       $a = $1;
       $winissl=1;
       $mf .=  "SSLIDIR=$a\nSSLIDIR111=$a\n";
       next L1;
     }
     if(/--winssllib=(.*)/)
     {
       $a = $1;
       $winlssl=1;
       $mf .=  "SSLLIBDIR=$a\nSSLLIBDIR111=$a\n";
       next L1;
     }
     if(/--wingnutlsinclude=(.*)/)
     {
       $a = $1;
       $winigtls=1;
       $mf .=  "GNUTSLIDIR=$a\n";
       next L1;
     }
     if(/--wingnutlslib=(.*)/)
     {
       $a = $1;
       $winlgtls=1;
       $mf .=  "GNUTSLLIBDIR=$a\n";
       next L1;
     }
     if(/--armgcc=(.*)/)
     {
       $a = $1;
       $armgcc=1;
       $mf .=  "ATGPP=$a\nATGCC=$a\n";
       next L1;
     }
     if(/--arminclude=(.*)/)
     {
       $a = $1;
       $mf .=  "ATINCLUDE=$a\n";
       next L1;
     }
     if(/--armlib=(.*)/)
     {
       $a = $1;
       $mf .=  "ATLIB=$a\n";
       next L1;
     }
     if(/--without(.*)/)
     {
       $a = $1;
       if($help =~ /without$a/m )
       {
         $without{$a} = 1;
         next L1;
       }
     }
     if(/--commonLog/)
     {
       $commonLog = 1;
       next L1;
     }
     if(/--withfakelibs/)
     {
       $without{'fakelibs'} = 0;
       next L1;
     }

     if(/--withTLS_1_3/)
     {
       $without{'TLS_1_3'} = 0;
       next L1;
     }

     if(/--withoutTLS_1_3/)
     {
       $without{'TLS_1_3'} = 1;
       next L1;
     }

     if(/--openssl=(.*)/)
     {
       $openssl_path = $1;
       next L1;
     }

     if(/--build=(.*)/)
     {
       $x = $1;
       if($bld{$x})
       {
         $all = $bld{$x};
       }
       else
       {
         $cross = 1;
         $crosspref="$x-" if(! $crosspref);
         $all='n_all';
       }
       $target=1;

       next L1;
     }

     if(/--prefix=(.*)/)
     {
       $var{'CONFIG_BASE'}=$1;
       $var{'prefix'}=$1;
       next L1;
     }
     if(/--debug/)
     {
       $without{'debug'} = 0;
       next L1;
     }
     if(/--sysconfdir=(.*)/)
     {
       $x = $1;
       $var{'CONFIG_CONFIG'}="$x/smallsrv/";
       next L1;
     }
     if(/--localstatedir=(.*)/)
     {
       $x = $1;
       $var{'CONFIG_DATA'}=$x;
       $var{'CONFIG_LOG'}="$x/log/smallsrv/";
       next L1;
     }
     if(/--only_vpncvlient/)
     {
       $only_vpnc = 1;
     }

     foreach $x (
       'includedir',
       'mandir',
       'infodir',
       'disable-option-checking',
       'disable-silent-rules',
       'libdir',
       'runstatedir',
       'disable-maintainer-mode',
       'disable-dependency-tracking'
     )
     {
       if(/--$x/)
       {
         if(/--$x=(.*)/)
         {
           $avar{$x}=$1;
         }
         else
         {
           $avar{$x}=1;
         }
         $autobuild = 1;
         next L1;
       }
     }


     foreach $x ( keys %var )
     {
       if(/$x=(.*)/)
       {
         $var{$x} = $1;
         next L1;
       }
     }

     print  "Unknow key $_\n";
     exit -1 if(!$autobuild);
   }
 }

 if($cross)
 {
   $INSTALLFROM='oo';
   $mf .= "N_DEP := oo/fakelibs $(N_FAKELIBS)\nN_LFAKELIBS=-Loo/fakelibs\n" if(! $without{'fakelibs'} );
 }
 elsif( $all =~ /[^ ][ ]+[^ ]/)
 {
   $a = `uname -m`;
   $a =~ s/[ \r\n\t]+//g;
   $INSTALLFROM = $tdirs{$a} if($tdirs{$a});
 }
 else
 {
   $all =~ s/[ ]+//g;
   $INSTALLFROM = $tdirs{$all} if($tdirs{$all});
 }



 $WIN_CONFIG_BASE = $var{'WIN_CONFIG_BASE'};
 delete($var{'WIN_CONFIG_BASE'});

 if(! stat("dnsd.cpp") )
 { # VPN Client only
   $only_vpnc = 1;
 }


 foreach $_ ( keys %var )
 {
   $var{$_} =~ s/\$\(CONFIG_BASE\)/$var{'CONFIG_BASE'}/;
 }


 if(! $target )
 {
   if($ENV['ARCH'])
   {
     print STDERR "Unknown ARCH: $ENV['ARCH'].\n";
   }
   $a = `uname -m`;
   $a =~ s/[ \r\n\t]+//g;

   if($ttargets{$a})
   {
       $all = $ttargets{$a};
       $INSTALLFROM = $tdirs{$all} if($tdirs{$all});
   }
   else
   {
     print STDERR "Unknown ARCH:$a.\n";
     if( stat('/usr/lib/i386-linux-gnu') )
     {
       $all = 'i32';
       $INSTALLFROM = $tdirs{$all};
     }

     if( stat('/usr/lib/x86_64-linux-gnu') )
     {
       $all .= ' i64';
       $INSTALLFROM = $tdirs{'i64'};
     }



     if(!$winegcc)
     {

       if( chkmingw64('i686-w64-mingw32') || chkmingw64('x86_64-w64-mingw32') || chkwinemingw() )
       {
         $all .= ' win';
       }
       #     elsif( stat('/usr/bin/wine')    )
       #     {
       #      $home=$ENV{'HOME'} ;
       #      if( stat("$home/.wine/drive_c/MINGW") )
       #      {
       #        $a='wine C:\\\\MINGW\\\\bin\\\\gcc.exe';
       #        $b='wine C:\\\\MINGW\\\\bin\\\\as.exe';
       #        $c='wine C:\\\\MINGW\\\\bin\\\\windres.exe';
       #        $mf .=  "WINEGPP=$a\nWINEGCC=$a\nWINEAS=$b\nWINERES=$c\nMGDIR=C:\\\\MINGW\n";
       #        $all .= ' win';
       #      }
       #      elsif( stat("$home/.wine/drive_c/MinGW") )
       #      {
       #        $a='wine C:\\\\MinGW\\\\bin\\\\gcc.exe';
       #        $b='wine C:\\\\MinGW\\\\bin\\\\as.exe';
       #        $c='wine C:\\\\MinGW\\\\bin\\\\windres.exe';
       #        $mf .=  "WINEGPP=$a\nWINEGCC=$a\nWINEAS=$b\nWINERES=$c\nMGDIR=C:\\\\MinGW\n";
       #        $all .= ' win';
       #      }
       #     }
     }
   }
 }
 else
 {
   if($all =~ /win/ &&  !$winegcc  )
   {
    if( (! chkmingw64('i686-w64-mingw32')) && ( !chkmingw64('x86_64-w64-mingw32') ) && ! chkwinemingw )
    {
      print STDERR "MinGW GCC not found.  Use --winegcc key to build version for Windows\n";
    }

   }

 }

 $mf .= "INSTALLFROM ?= $INSTALLFROM/\n";

 $i32gnutls30 = 0;
 $i64gnutls30 = 0;
 $libdir = '/usr/lib';
 $includedir = '/usr/include';

 $vp='\\${prefix}';
 if( $avar{'libdir'} )
 {
   $libdir = $avar{'libdir'};
   $libdir =~ s/$vp/$var{'CONFIG_BASE'}/;


   $adv .= " -L$libdir";

   if( $avar{'includedir'} )
   {
     $includedir = $avar{'includedir'};
     $includedir =~ s/$vp/$var{'CONFIG_BASE'}/;

     if( $includedir ne '/usr/include' )
     {
       $adv .= " -I$includedir";
     }

     print "****TEMP includedir $libdir $includedir \n";
   }

 }


 if($all =~ /i32/   )
 {

   if( (!$openssl_path) &&  ((!stat('/usr/lib/i386-linux-gnu/libssl.so') ) || !stat('/usr/include/openssl') ) )
   {
      print STDERR "i32: Not found OpenSSL library or OpenSSL header files.  Build without seclib.so\n";
      $woseclib32=1;
      $sec =~ s/libssl.so//;
   }

   if(stat($gtls = '/usr/lib/i386-linux-gnu/libgnutls.so') || stat($gtls = "$libdir/libgnutls.so") )
   {
     $nm = `nm -D $gtls`;
     if($nm =~ /gnutls_certificate_verify_peers3/g)
     {
       $i32gnutls30 = 3;
     }
   }
   elsif(stat('/usr/lib/i386-linux-gnu/libgnutls.so.30') || stat("$libdir/libgnutls.so.30") )
   {
     $i32gnutls30 = 1;
   }
   elsif(stat('/usr/lib/i386-linux-gnu/libgnutls.a') || stat("$libdir/libgnutls.a") )
   {
     $i32gnutls30 = 2;
   }
   if( (! $i32gnutls30 ) || ( (!stat('/usr/include/gnutls')) &&  !stat("$includedir/gnutls") )  )
   {
      print STDERR "i32: Not found GnuTLS library or GnuTLS header files.  Build without libgnutls.so\n";
      $wolibgnutls32=1;
      $sec =~ s/libgnutls.so//;
   }
 }

 if($all =~ /i64/   )
 {
   if( (!stat('/usr/lib/x86_64-linux-gnu/libssl.so') ) || !stat('/usr/include/openssl') )
   {
      print STDERR "i64: Not found OpenSSL library or OpenSSL header files.  Build without seclib111.so\n";
      $woseclib64=1;
      $sec64 =~ s/libssl.so//;
   }


   if(stat($gtls = '/usr/lib/x86_64-linux-gnu/libgnutls.so')  || stat($gtls = "$libdir/libgnutls.so") )
   {
     $nm = `nm -D $gtls`;
     if($nm =~ /gnutls_certificate_verify_peers3/g)
     {
       $i64gnutls30 = 3;
     }
   }
   elsif(stat('/usr/lib/x86_64-linux-gnu/libgnutls.so.30') || stat("$libdir/libgnutls.so.30") )
   {
     $i64gnutls30 = 1;
   }
   elsif(stat('/usr/lib/x86_64-linux-gnu/libgnutls.a') || stat("$libdir/libgnutls.a") )
   {
     $i64gnutls30 = 2;
   }

   if( (!$i64gnutls30) || !stat('/usr/include/gnutls') )
   {
      print STDERR "i64: Not found GnuTLS library or GnuTLS header files.  Build without libgnutls.so\n";
      $wolibgnutls64=1;
      $sec64 =~ s/libgnutls.so//;
   }

 }

 if(! $without['GenTempSert'] )
 {

   $sertapp=`openssl version`;
   if($sertapp && $sertapp =~ /[0-9]/)
   {
     $all .= ' temp_sert';
     $use_temp_sert=1;
   }
   else
   {
     $sertapp=`certtool -v`;
     if($sertapp && $sertapp =~ /[0-9]/)
     {
       $all .= ' temp_sert';
       $use_temp_sert=2;
     }
   }
 }

 if($all =~ /i64|i32/)
 {
   $warn=`gcc --help=warnings`;
   if($warn =~ /address-of-packed-member/) {
     $adv .= ' -Wno-address-of-packed-member'
   }
   if($warn =~ /-Wattributes/) {
     $adv .= ' -Wno-attributes'
   }
 }

  open(FILE, "<makefile.in") || die("Can\'t open makefile.in\n");
  if( stat('makefile') )
  {
    unlink('makefile.orig');
    rename('makefile', 'makefile.orig');
  }
  open(OFILE, ">Makefile") || die("Can\'t open Makefile\n");

  if(!$without{'fakelibs'})
  {
    $mf .= sprintf('

FLIBS=libc.so libdl.so libpthread.so %s
FAKELIBS=$(addprefix fakelibs/,$(FLIBS))
FLIBS64=libc.so libdl.so libpthread.so %s
FAKELIBS64=$(addprefix fakelibs64/,$(FLIBS64))
LIBDIR32=-Lfakelibs/
LIBDIR64=-Lfakelibs64/
N_LFAKELIBS=-Loo/fakelibs/
N_DEP := $(N_FAKELIBS)

    ',$sec,$sec64);

    if( $all =~ /i32/)
    {
      $all = '$(FAKELIBS) ' . $all;
    }
    if( $all =~ /i64/)
    {
      $all = '$(FAKELIBS64) ' . $all;
    }
  }

  if(! $without{'TLS_1_3'}) {
    $adv .= ' -DWITHTLS1_3'
  }

  if($adv ne '' )
  {
    $mf .= "\nADVOPT=$adv\n";
  }

  print OFILE $mf;
#  print OFILE "\nall: /dev/shm/shttps/o/1.x o/1.x $all\n\n";
  print OFILE "\nall: $all\n\n";

 L2:
  while(<FILE>)
  {
    if(/^([A-Z0-9]+=)/)
    {
      $a=$1;
      if($mf =~ /\n$a/m )
      {
        next L2;
      }
    }
    if($without{'POOL'})
    {
      s/-DUSE_POOL//g;
    }
    if($without{'IPv6'})
    {
      s/-DUSE_IPV6//g;
    }
    if($without{'FUTEX'})
    {
      s/-DUSE_FUTEX//g;
    }
    if($without{'MD5'})
    {
      s/-DWITHMD5//g;
    }
    if($without{'VPN'})
    {
      s/-DVPN[A-Z_]+//g;
      s/-DTLSVPN//g;
      s/ vpn.o//g;
      s/ shs_vpnclient[^ ]*//g;
    }
    if($without{'TFTP'})
    {
      s/-DTFTP//g;
      s/ tftp.o//g;
    }
    if($without{'SYSPASS'})
    {
      s/-DUSE_SYSPASS//g;
      s/ -lcrypto//g;
    }
    if($without{'Telnet'})
    {
      s/-DTELNET//g;
      s/ telnet.o//g;
    }
    if($commonLog)
    {
      s/-DSEPLOG//g;
    }
    if($without{'debug'} == 0 )
    {
      s/ -s([^a-z])/ -g$1/g;
      s/ -s$/ -g/g;
    }

#     if($without{'fakelibs'})
#     {
#       s/-Lfakelibs[0-9]*//g;
#     }
#     else if(!$without{'fakelibs'})
    {
      if( stat('/lib/x86_64-linux-gnu/libc.so.6') )
      {
        s/\/usr\/lib\/x86_64-linux-gnu\/libc.so.6/\/lib\/x86_64-linux-gnu\/libc.so.6/ ;
      }
    }

    if($woseclib32)
    {
      s/^i32([f]*\:.*)o[\/of]*?\/libsec111.so(.*)/i32$1 $2/g;
      s/^i32([f]*\:.*)o[\/of]*?\/libsec.so(.*)/i32$1 $2/g;
      s/^i32([f]*\:.*)o[\/of]*?\/httpd.exopenssl(.*)/i32$1 $2/g;
      s/^i32([f]*\:.*)o[\/of]*?\/shs_vpnclient.openssl(.*)/i32$1 $2/g;
    }
    if($wolibgnutls32)
    {
      s/^i32([f]*\:.*)o[\/of]*?\/libsecgnutls.so(.*)/i32$1 $2/g;
      s/^i32([f]*\:.*)o[\/of]*?\/httpd.exgnutls(.*)/i32$1 $2/g;
      s/^i32([f]*\:.*)o[\/of]*?\/shs_vpnclient.gnutls(.*)/i32$1 $2/g;
    }

    if($woseclib64)
    {
      s/^i64([f]?\:.*)o64[\/of]*?\/libsec.so(.*)/i64$1 $2/g;
      s/^i64([f]?\:.*)o64[\/of]*?\/libsec111.so(.*)/i64$1 $2/g;
      s/^i64([f]?\:.*)o64[\/of]*?\/httpd.exopenssl(.*)/i64$1 $2/g;
      s/^i64([f]*\:.*)o64[\/of]*?\/shs_vpnclient.openssl(.*)/i64$1 $2/g;
    }
    if($wolibgnutls64)
    {
      s/^i64:(.*)o64\/libsecgnutls.so(.*)/i64: $1 $2/g;
      s/^i64([f]?\:.*)o64[\/of]*?\/httpd.exgnutls(.*)/i64$1 $2/g;
      s/^i64([f]*\:.*)o64[\/of]*?\/shs_vpnclient.gnutls(.*)/i64$1 $2/g;
    }
    if($i32gnutls30 == 1)
    {
      s/LIBDIR32\) \-l\:libgnutls\.so /LIBDIR32\) -l:libgnutls.so.30 /
    }
    elsif($i32gnutls30 == 2)
    {
      s/LIBDIR32\) \-l\:libgnutls\.so /LIBDIR32\) -lgnutls /
    }
    if($i64gnutls30 == 1)
    {
      s/LIBDIR64\) \-l\:libgnutls\.so /LIBDIR64\) -l:libgnutls.so.30 /
    }
    elsif($i64gnutls30 == 2)
    {
      s/LIBDIR64\) \-l\:libgnutls\.so /LIBDIR64\) -lgnutls /
    }
    if($openssl_path)
    {
      s/\/usr\/src\/openssl-1.1.1m/$openssl_path/g;
    }
    if($use_temp_sert==2)
    {
      s/[:][ ]*o\/stemp_sert.pem/: o\/gtemp_sert.pem/g;
    }
    if($only_vpnc == 1)
    {
      while(s/^([a-z_0-9]+):(.*) [^\/]+\/(httpd|sndmsg|shs_ctl)([^ ]*)/$1:$2 /g)
      {
        ;
      }

      s/^([a-z_0-9]+):(.*) [^\/]+\/sndmsg/$1:$2 /g;
      s/^([a-z_0-9]+):(.*) [^\/]+\/http.exe/$1:$2 /g;
      s/^([a-z_0-9]+):(.*) [^\/]+\/sndmsg.exe/$1:$2 /g;
      s/^([a-z_0-9]+):(.*) [^\/]+\/shs_ctl/$1:$2 /g;

      while(s/:=(.*)(httpd|sndmsg|shs_ctl)([^ ]*)/:=$1/g)
      {
        ;
      }
      while(s/(for i in .*)(httpd|sndmsg|shs_ctl)([^ ]*)/$1/g)
      {
        ;
      }
    }

    foreach $x ( keys %var )
    {
      s/^[ \t]*$x[ ]*\?=.*/$x ?= $var{$x}/;
    }

    print OFILE $_;
  }

  close OFILE;
  close FILE;


  if( ! stat('config.h.default') )
  {
    rename('config.h', 'config.h.default');
  }

  open(OFILE, ">config.h") || die("Can\'t open config.h\n");

  $WIN_CONFIG_BASE =~ s/[\\]/\\\\/g;
  printf OFILE '
#ifndef CONFIG_H
#define CONFIG_H

#ifdef CONFIG_CURRENT_DIR

#define CONFIG_BASE   ""
#define CONFIG_APPDIR ""
#define CONFIG_LANG   ""
#define CONFIG_DATA   ""
#define CONFIG_CONFIG ""
#define CONFIG_LOG    ""
#define CONFIG_TMP    ""

#elif ! defined(SYSUNIX)
// Windows definitions

#define CONFIG_BASE   "%s"
#define CONFIG_APPDIR CONFIG_BASE
#define CONFIG_SHARE  CONFIG_BASE
#define CONFIG_DATA   CONFIG_BASE
#define CONFIG_CONFIG CONFIG_BASE
#define CONFIG_LOG    CONFIG_BASE
#define CONFIG_TMP    CONFIG_BASE

#else

', $WIN_CONFIG_BASE;

  if($var{'prefix'})
  {
    delete $var{'prefix'};
  }
  foreach $x ( keys %var )
  {
    print OFILE "#define $x \"$var{$x}\"\n";
  }

  print OFILE '
#endif

#endif
';
  close OFILE;

  if( $var{'CONFIG_DATA'} ne '/var' || $var{'CONFIG_LOG'} ne '/var/log/smallsrv/' )
  {
     if( ! stat('httpd.conf.default') )
     {
       rename('httpd.conf', 'httpd.conf.default');
     }
     if(! open FILE, "<httpd.conf.default" )
     {

       if(! open OFILE, ">httpd.conf.default" )
       {
         while(<FILE>)
         {
           s/var\/log\/smallsrv/$var{'CONFIG_LOG'}/ ||
           s/\/var/$var{'CONFIG_DATA'}/ ;
           print $_;
         }
         close OFILE;
       }
       close FILE;
     }
  }

  print "
  Done
  Target:        $all
  Install from:  $INSTALLFROM\n";

  my @opt_key = ( $help =~ /without([^ \t\n]+)/gm );
  foreach $_ (@opt_key, 'debug', 'TLS_1_3') {
    printf  "  %-14s %s\n", "$_:", $without{$_}? 'disabled':'enabled';
  }
  foreach $_ (keys %var) {
    printf  "  %-14s %s\n", "$_:", $var{$_};
  }



sub chkmingw64
{
 my ($mingw) = @_;
 my($sdir) ;
 if( stat("/usr/bin/$mingw-gcc")  || stat("/usr/local/bin/$mingw-gcc") )
 {
   $mf .=  "WINEGPP=$mingw-gcc -m32 -DMINGW64\nWINEGCC=$mingw-gcc -m32 -DMINGW64\nWINEAS=$mingw-as\nWINERES=$mingw-windres\nMGDIR=/usr/$mingw\nWINLIB= -L\$(MGDIR)/lib\n";
   $sdir = "/usr/lib/gcc/$mingw/";
   if( !opendir(DIR, $sdir) )
   {
     print STDERR "Can't open $sdir\n";
   }
   else
   {
      while(readdir(DIR))
      {
        if(/win32/)
        {
          $MINGWVERDIR=$_;
          last;
        }
        if(/posix/)
        {
          $MINGWVERDIR=$_;
        }

      }
      closedir DIR;
   }


   $mf .= "WININC= -I/usr/$mingw/include -I/usr/lib/gcc/$mingw/$MINGWVERDIR/include\n";


   return 1;
  }

  return 0;



}


sub chkwinemingw
{
    if( stat('/usr/bin/wine')    )
    {
     $home=$ENV{'HOME'} ;
     if( stat("$home/.wine/drive_c/MINGW") )
     {
       $a='wine C:\\\\MINGW\\\\bin\\\\gcc.exe';
       $b='wine C:\\\\MINGW\\\\bin\\\\as.exe';
       $c='wine C:\\\\MINGW\\\\bin\\\\windres.exe';
       $mf .=  "WINEGPP=$a\nWINEGCC=$a\nWINEAS=$b\nWINERES=$c\nMGDIR=C:\\\\MINGW\n";
       return 1;
     }
     elsif( stat("$home/.wine/drive_c/MinGW") )
     {
       $a='wine C:\\\\MinGW\\\\bin\\\\gcc.exe';
       $b='wine C:\\\\MinGW\\\\bin\\\\as.exe';
       $c='wine C:\\\\MinGW\\\\bin\\\\windres.exe';
       $mf .=  "WINEGPP=$a\nWINEGCC=$a\nWINEAS=$b\nWINERES=$c\nMGDIR=C:\\\\MinGW\n";
       return 1;
     }
    }
    return 0;
}

