#if the pkg created in packages-woof has something wrong, this file is a last
#resort to fix it. Current-dir is where this file FIXUPHACK is located.
#w469 added English.pm, NamedCapture.pm, Errno.pm, needed by 'pnmquant' utility.
#091126 added Hash.pm needed by POSIX.pm (needed by foomatic-rip).
#110621 add CORE/libperl.so to tiny list (needed in mageia 1).
#note: DISTRO_BINARY_COMPAT and DISTRO_FILE_PREFIX are visible here, exported in 2createpackages.
#140117 xARCHDIR, ARCHDIR and DISTRO_ARCHDIR_SYMLINKS exported in 2createpackages. 
#140117 note, 2createpackages may move /lib, /usr/lib, /usr/bin in template to ARCHDIR subdir.
#141112 quirky unicorn, some apps need libperl.so
#151110 some dir symlinks were missing.
#160907 netpbm ppmquant utility needs overloading.pm
#171126 pplog needs if.pm, deprecate.pm, base.pm. also ref: http://bkhome.org/news/201711/pplog-now-works-in-quirky-pyro64.html
#180806 ident-user-enum script needs FileHandle.pm, INET.pm, UNIX.pm, IP.pm, Config_git.pl (also extra module Net::Ident).

PERL_TINY_FILELIST='CORE/libperl.so IO.pm Data/Dumper.pm File/GlobMapper.pm File/Glob.pm POSIX.pm Fcntl.pm IO/File.pm IO/Pipe.pm IO/Poll.pm IO/Handle.pm IO/Seekable.pm IO/Select.pm IO/Socket.pm IO/Dir.pm Socket.pm DynaLoader.pm Config.pm XSLoader.pm Cwd.pm Socket/Socket.so POSIX/atof.al POSIX/waitpid.al POSIX/setjmp.al POSIX/perror.al SigAction/handler.al SigAction/safe.al SigAction/flags.al SigAction/new.al SigAction/mask.al SigRt/new.al SigRt/_init.al SigRt/CLEAR.al SigRt/_check.al SigRt/_exist.al SigRt/DELETE.al SigRt/SCALAR.al SigRt/STORE.al SigRt/_croak.al SigRt/_getsig.al SigRt/EXISTS.al SigRt/FETCH.al POSIX/scanf.al POSIX/malloc.al POSIX/rewind.al POSIX/strcmp.al POSIX/rename.al POSIX/setvbuf.al POSIX/memset.al POSIX/fgets.al POSIX/getgid.al POSIX/autosplit.ix POSIX/fileno.al POSIX/getpwuid.al POSIX/link.al POSIX/fputc.al POSIX/opendir.al POSIX/strpbrk.al POSIX/sprintf.al POSIX/strcpy.al POSIX/fread.al POSIX/getuid.al POSIX/strncat.al POSIX/gets.al POSIX/cos.al POSIX/ldiv.al POSIX/memcpy.al POSIX/siglongjmp.al POSIX/abs.al POSIX/geteuid.al POSIX/memmove.al POSIX/unlink.al POSIX/putchar.al POSIX/clearerr.al POSIX/execle.al POSIX/fscanf.al POSIX/bsearch.al POSIX/chdir.al POSIX/calloc.al POSIX/getgrgid.al POSIX/vprintf.al POSIX/qsort.al POSIX/rewinddir.al POSIX/puts.al POSIX/creat.al POSIX/atan2.al POSIX/gmtime.al POSIX/fdopen.al POSIX/load_imports.al POSIX/memchr.al POSIX/time.al POSIX/printf.al POSIX/getlogin.al POSIX/getpid.al POSIX/ferror.al POSIX/getc.al POSIX/tmpfile.al POSIX/rand.al POSIX/system.al POSIX/utime.al POSIX/getenv.al POSIX/atexit.al POSIX/execl.al POSIX/longjmp.al POSIX/remove.al POSIX/tolower.al POSIX/strtok.al POSIX/fflush.al POSIX/strrchr.al POSIX/offsetof.al POSIX/vsprintf.al POSIX/alarm.al POSIX/div.al POSIX/errno.al POSIX/labs.al POSIX/raise.al POSIX/redef.al POSIX/fcntl.al POSIX/sin.al POSIX/getgrnam.al POSIX/execlp.al POSIX/putc.al POSIX/sigsetjmp.al POSIX/strspn.al POSIX/ungetc.al POSIX/strncmp.al POSIX/ftell.al POSIX/atoi.al POSIX/sleep.al POSIX/fputs.al POSIX/getppid.al POSIX/execv.al POSIX/assert.al POSIX/realloc.al POSIX/feof.al POSIX/getpwnam.al POSIX/strstr.al POSIX/kill.al POSIX/exit.al POSIX/atol.al POSIX/strcat.al POSIX/fork.al POSIX/fwrite.al POSIX/srand.al POSIX/fseek.al POSIX/readdir.al POSIX/POSIX.so POSIX/fclose.al POSIX/mkdir.al POSIX/pow.al POSIX/umask.al POSIX/log.al POSIX/exp.al POSIX/getgroups.al POSIX/unimpl.al POSIX/fgetpos.al POSIX/fabs.al POSIX/strcspn.al POSIX/fsetpos.al POSIX/sqrt.al POSIX/free.al POSIX/sscanf.al POSIX/localtime.al POSIX/fstat.al POSIX/chown.al POSIX/setbuf.al POSIX/strlen.al POSIX/getpgrp.al POSIX/closedir.al POSIX/strchr.al POSIX/rmdir.al POSIX/strerror.al POSIX/getegid.al POSIX/fgetc.al POSIX/getchar.al POSIX/fsync.al POSIX/fopen.al POSIX/strncpy.al POSIX/isatty.al POSIX/usage.al POSIX/stat.al POSIX/freopen.al POSIX/wait.al POSIX/chmod.al POSIX/toupper.al POSIX/fprintf.al POSIX/execvp.al POSIX/execve.al POSIX/vfprintf.al POSIX/memcmp.al Fcntl/Fcntl.so Dumper/Dumper.so Glob/Glob.so IO/IO.so DynaLoader/autosplit.ix DynaLoader/dl_find_symbol_anywhere.al DynaLoader/dl_findfile.al DynaLoader/dl_expandspec.al Cwd/Cwd.so AutoLoader.pm strict.pm CGI.pm Getopt/Std.pm Getopt/Long.pm getopt.pl Exporter.pm Symbol.pm warnings/register.pm overload.pm File/Basename.pm File/DosGlob.pm File/Path.pm File/Temp.pm File/Fetch.pm File/CheckTree.pm Spec/OS2.pm Spec/Win32.pm Spec/Cygwin.pm Spec/Functions.pm Spec/Mac.pm Spec/VMS.pm Spec/Unix.pm Spec/Epoc.pm File/Copy.pm File/Spec.pm File/Find.pm File/stat.pm File/Compare.pm SelectSaver.pm Carp.pm Exporter/Heavy.pm constant.pm bytes.pm CGI/Cookie.pm CGI/Apache.pm CGI/Push.pm CGI/Pretty.pm CGI/Carp.pm CGI/Switch.pm CGI/Util.pm vars.pm IO/Zlib.pm sigtrap.pm warnings.pm Benchmark.pm Config_heavy.pl English.pm NamedCapture.pm Errno.pm Hash.pm overloading.pm if.pm deprecate.pm base.pm FileHandle.pm Socket/INET.pm Socket/UNIX.pm Socket/IP.pm Config_git.pl'

#have some difficulty with the different distros having quite different directory layout for their perl pkg...
#so, template allows everything, as-is, then we cut it down...

mkdir -p ../tempdir1
cp -a ./* ../tempdir1/
PERL_ALL_FILELIST="`find ./ -not -type d | sed -e 's%^\./%%'`"
rm -rf ./usr

for ONE_PERL_FILE in $PERL_TINY_FILELIST
do
 opfPATTERN='/'"$ONE_PERL_FILE"'$'
 for ONEFND in `echo "$PERL_ALL_FILELIST" | grep "$ONE_PERL_FILE" | tr '\n' ' '`
 do
  [ ! -f ../tempdir1/$ONEFND ] && continue
  ONEFND_PATH="`dirname $ONEFND`"
  mkdir -p $ONEFND_PATH
  cp -a -f ../tempdir1/$ONEFND $ONEFND_PATH/
 done
done

if [ "$xARCHDIR" ];then #140117
 mkdir -p usr/bin/${ARCHDIR}
 cp -a ../tempdir1/usr/bin/${ARCHDIR}/perl* usr/bin/${ARCHDIR}/ 2>/dev/null
fi
mkdir -p usr/bin
cp -a ../tempdir1/usr/bin/perl* usr/bin/ 2>/dev/null

#for Perl, just leave 'perl' executable... um, no, make 'perl' a symlink... 120824 fix...
if [ "$xARCHDIR" ];then #140117
 UPKGVERSION="$(find usr/bin/${ARCHDIR} -mindepth 1 -maxdepth 1 -type f -executable -name 'perl[0-9]*' | sed -e "s%usr/bin/${ARCHDIR}/perl%%" | head -n 1)"
 if [ "$UPKGVERSION" != "" ];then
  if [ ! -h usr/bin/${ARCHDIR}/perl${UPKGVERSION} ];then
   rm -f usr/bin/${ARCHDIR}/perl
   ln -s perl${UPKGVERSION} usr/bin/${ARCHDIR}/perl
  fi
 fi
fi
UPKGVERSION="$(find usr/bin -mindepth 1 -maxdepth 1 -type f -executable -name 'perl[0-9]*' | sed -e 's%usr/bin/perl%%' | head -n 1)"
if [ "$UPKGVERSION" != "" ];then
 if [ ! -h usr/bin/perl${UPKGVERSION} ];then
  rm -f usr/bin/perl
  ln -s perl${UPKGVERSION} usr/bin/perl
 fi
fi

#120715 debian squeeze has libperl* in perl_base pkg -- not in libperl pkg!
if [ "$xARCHDIR" ];then #140117
 if [ -d ../tempdir1/usr/lib/${ARCHDIR} ];then
  mkdir -p usr/lib/${ARCHDIR}
  cp -a -f --remove-destination ../tempdir1/usr/lib/${ARCHDIR}/libperl.so* usr/lib/${ARCHDIR}/ 2>/dev/null
 fi
fi
if [ -d ../tempdir1/usr/lib ];then
 mkdir -p usr/lib
 cp -a -f --remove-destination ../tempdir1/usr/lib/libperl.so* usr/lib/ 2>/dev/null
fi

#141112 some apps need libperl.so
PERLLIB="$(find . -type f -name 'libperl.so*' | head -n 1)"
if [ "$PERLLIB" ];then
 LIBPATH="$(dirname $PERLLIB)"
 LIBNAME="$(basename $PERLLIB)"
 [ ! -e ${LIBPATH}/libperl.so ] && ln -s ${LIBNAME} ${LIBPATH}/libperl.so
fi

#151110 some symlinks are missing...
for ADIR in `find . -type d | sed -e 's%^\./%%' | tr '\n' ' '`
do
 [ ! -d $ADIR ] && continue
 UPDIR="$(dirname $ADIR)"
 for ALINK in `find ../tempdir1/${UPDIR} -mindepth 1 -maxdepth 1 -type l | tr '\n' ' '`
 do
  [ ! -d $ALINK ] && continue
  cp -a -f --remove-destination $ALINK ${UPDIR}/
 done
done
sync

rm -rf ../tempdir1

#181213 oe compiled perl has some incorrect entries in Config.pm
if [ -d usr/lib/perl ];then
 CONFPM="$(find usr/lib/perl -mindepth 2 -maxdepth 2 -type f -name 'Config.pm' | head -n 1)"
 if [ "$CONFPM" ];then
  sed -i -e 's%/mnt/sd.*/recipe\-sysroot\-native%%' $CONFPM
  sed -i -e 's%/mnt/sd.*/recipe\-sysroot%%' $CONFPM
  sed -i -e 's%\-\-sysroot=%%' $CONFPM
 fi
fi
