#!/bin/sh

#############################################################################
# configuration to fill in

PERLVER=5.12.2 # 5.8.9 is also a good choice
STATICPERL=~/.staticperl
CPAN=http://mirror.netcologne.de/cpan/ # which mirror to use
EMAIL="read the documentation <rtfm@example.org>"

MKBUNDLE="$STATICPERL/mkbundle"

# perl build variables
PREFIX="$STATICPERL/perl" # where the perl gets installed
PERL_CONFIGURE="" # additional Configure arguments
PERL_CPPFLAGS="-DPERL_DISABLE_PMC -DPERL_ARENA_SIZE=65536 -D_GNU_SOURCE -DNDEBUG -USITELIB_EXP -USITEARCHEXP -UARCHLIB_EXP"
PERL_OPTIMIZE="-Os -ffunction-sections -fdata-sections -finline-limit=8 -ffast-math"

ARCH="$(uname -m)"

case "$ARCH" in
   i*86 | x86_64 | amd64 )
      PERL_OPTIMIZE="$PERL_OPTIMIZE -mpush-args -mno-inline-stringops-dynamically -mno-align-stringops -mno-ieee-fp" # x86/amd64
      case "$ARCH" in
         i*86 )
            PERL_OPTIMIZE="$PERL_OPTIMIZE -fomit-frame-pointer -march=pentium3 -mtune=i386" # x86 only
            ;;
      esac
      ;;
esac

# -Wl,--gc-sections makes it impossible to check for undefined references
# for some reason so we need to patch away the "-no" after Configure and before make :/
# -z muldefs is to work around uclibc's pthread static linking bug
PERL_LDFLAGS="-Wl,--no-gc-sections -z muldefs"
PERL_LIBS="-lm -lcrypt" # perl loves to add lotsa crap itself

# some configuration options for modules
export PERL_MM_USE_DEFAULT=1
#export CORO_INTERFACE=p # needed without nptl on x86, due to bugs in linuxthreads - very slow
export EV_EXTRA_DEFS='-DEV_FEATURES=4+8+16+64 -DEV_USE_SELECT=0 -DEV_USE_POLL=1 -DEV_USE_EPOLL=1 -DEV_NO_LOOPS -DEV_COMPAT3=0'

# which extra modules to install by default from CPAN that are
# required by mkbundle
STATICPERL_MODULES="common::sense Pod::Strip PPI::XS Pod::Usage"

# which extra modules you might want to install
EXTRA_MODULES=""

# overridable functions
postconfigure() { : ; }
postbuild()     { : ; }
postinstall()   { : ; }

# now source user config, if any
[ -r /etc/staticperlrc ] && . /etc/staticperlrc
[ -r ~/.staticperlrc   ] && . ~/.staticperlrc
[ -r "$STATICPERL/rc"  ] && . "$STATICPERL/rc"

#############################################################################
# support

# set version in a way that Makefile.PL can extract
VERSION=VERSION; eval \
$VERSION=0.1

BZ2=bz2
BZIP2=bzip2

fatal() {
   printf -- "\nFATAL: %s\n\n" "$*" >&2
   exit 1
}

verbose() {
   printf -- "%s\n" "$*"
}

verblock() {
   verbose
   verbose "***"
   while read line; do
      verbose "*** $line"
   done
   verbose "***"
   verbose
}

rcd() {
   cd "$1" || fatal "$1: cannot enter"
}

trace() {
   prefix="$1"; shift
#   "$@" 2>&1 | while read line; do
#      echo "$prefix: $line"
#   done
   "$@"
}

trap wait 0

#############################################################################
# clean

distclean() {
   verblock <<EOF
deleting everything installed by this script
EOF

   rm -rf "$STATICPERL"
}

#############################################################################
# download/configure/compile/install perl

clean() {
   cd "$STATICPERL/src/perl-$PERLVER" 2>/dev/null || return

   rm -f staticstamp.configure
   make distclean >/dev/null 2>&1
}

fetch() {
   rcd "$STATICPERL"

   mkdir -p src
   rcd src

   if ! [ -d "perl-$PERLVER" ]; then
      if ! [ -e "perl-$PERLVER.tar.$BZ2" ]; then

         URL="$CPAN/src/5.0/perl-$PERLVER.tar.$BZ2"

         verblock <<EOF
downloading perl
to manually download perl yourself, place
perl-$PERLVER.tar.$BZ2 in $STATICPERL
trying $URL
EOF

         curl >perl-$PERLVER.tar.$BZ2~ "$URL" \
            || wget -O perl-$PERLVER.tar.$BZ2~ "$URL" \
            || fatal "$URL: error during downloading"
         mv perl-$PERLVER.tar.$BZ2~ perl-$PERLVER.tar.$BZ2
      fi

      verblock <<EOF
unpacking perl
EOF

      mkdir -p unpack
      $BZIP2 -d <perl-$PERLVER.tar.bz2 | tar xpC unpack \
         || fatal "perl-$PERLVER.tar.bz2: error during unpacking"
      mv unpack/perl-$PERLVER perl-$PERLVER
      rmdir -p unpack
   fi
}

# similar to GNU-sed -i or perl -pi
sedreplace() {
   sed -e "$1" <"$2" > "$2~" || fatal "error while running sed"
   mv "$2~" "$2"
}

configure() {
   fetch

   rcd "$STATICPERL/src/perl-$PERLVER"

   [ -e staticstamp.configure ] && return

   verblock <<EOF
configuring $STATICPERL/src/perl-$PERLVER
EOF

   clean

   rm -f "$PREFIX/staticstamp.install"

   # I hate them
   grep -q -- -fstack-protector Configure && \
      sedreplace 's/-fstack-protector/-fno-stack-protector/g' Configure

#   trace configure \
   sh Configure -Duselargefiles \
                -Uuse64bitint \
                -Dusemymalloc=n \
                -Uusedl \
                -Uusethreads \
                -Uuseithreads \
                -Uusemultiplicity \
                -Duseperlio \
                -Uusesfio \
                -Uuseshrplib \
                -Dcppflags="$PERL_CPPFLAGS" \
                -Dccflags="-g2 -fno-strict-aliasing" \
                -Doptimize="$PERL_OPTIMIZE" \
                -Dldflags="$PERL_LDFLAGS" \
                -Dlibs="$PERL_LIBS" \
                -Dprefix="$PREFIX" \
                -Dbin="$PREFIX/bin" \
                -Dprivlib="$PREFIX/lib" \
                -Darchlib="$PREFIX/lib" \
                -Uusevendorprefix \
                -Dsitelib="$PREFIX/lib" \
                -Dsitearch="$PREFIX/lib" \
                -Usitelibexp \
                -Uman1dir \
                -Uman3dir \
                -Usiteman1dir \
                -Usiteman3dir \
                -Dpager=/usr/bin/less \
                -Demail="$EMAIL" \
                -Dcf_email="$EMAIL" \
                -Dcf_by="$EMAIL" \
                $PERL_CONFIGURE \
                -dE || fatal "Configure failed"

   sedreplace '
      s/-Wl,--no-gc-sections/-Wl,--gc-sections/g
      s/ *-fno-stack-protector */ /g
   ' config.sh

   sh Configure -S || fatal "Configure -S failed"

   postconfigure || fatal "postconfigure hook failed"

   touch staticstamp.configure
}

build() {
   configure

   rcd "$STATICPERL/src/perl-$PERLVER"

   verblock <<EOF
building $STATICPERL/src/perl-$PERLVER
EOF

   rm -f "$PREFIX/staticstamp.install"

   make || fatal "make: error while building perl"

   postbuild || fatal "postbuild hook failed"
}

install() {
   [ -e "$PREFIX/staticstamp.install" ] && return

   build

   verblock <<EOF
installing $STATICPERL/src/perl-$PERLVER
to $PREFIX
EOF

   rm -rf "$PREFIX"
   
   make install || fatal "make install: error while installing"

   rcd "$PREFIX"

   # create a "make install" replacement for CPAN
   cat >"$PREFIX"/bin/cpan-make-install <<EOF
make install UNINST=1
if find blib/arch/auto -type f | grep -q -v .exists; then
   echo Probably an XS module, rebuilding perl
   make perl
   rm -f "$PREFIX"/bin/perl
   make -f Makefile.aperl inst_perl
   make -f Makefile.aperl map_clean
fi
EOF
   chmod 755 "$PREFIX"/bin/cpan-make-install

   # trick CPAN into avoiding ~/.cpan completely
   echo 1 >"$PREFIX/lib/CPAN/MyConfig.pm"

   "$PREFIX"/bin/perl -MCPAN -e '
      CPAN::Shell->o (conf => urllist => push => "'"$CPAN"'");
      CPAN::Shell->o (conf => q<cpan_home>, "'"$STATICPERL"'/cpan");
      CPAN::Shell->o (conf => q<init>);
      CPAN::Shell->o (conf => q<cpan_home>, "'"$STATICPERL"'/cpan");
      CPAN::Shell->o (conf => q<build_dir>, "'"$STATICPERL"'/cpan/build");
      CPAN::Shell->o (conf => q<prefs_dir>, "'"$STATICPERL"'/cpan/prefs");
      CPAN::Shell->o (conf => q<histfile> , "'"$STATICPERL"'/cpan/histfile");
      CPAN::Shell->o (conf => q<keep_source_where>, "'"$STATICPERL"'/cpan/sources");
      CPAN::Shell->o (conf => q<make_install_make_command>, "'"$PREFIX"'/bin/cpan-make-install");
      CPAN::Shell->o (conf => q<prerequisites_policy>, q<follow>);
      CPAN::Shell->o (conf => q<build_requires_install_policy>, q<no>);
      CPAN::Shell->o (conf => q<commit>);
   ' || fatal "error while initialising CPAN"

   NOCHECK_INSTALL=+
   instcpan $STATICPERL_MODULES
   [ $EXTRA_MODULES ] && instcpan $EXTRA_MODULES

   postinstall || fatal "postinstall hook failed"

   touch "$PREFIX/staticstamp.install"
}

#############################################################################
# install a module from CPAN

instcpan() {
   [ $NOCHECK_INSTALL ] || install

   verblock <<EOF
installing modules from CPAN
$@
EOF

   for mod in "$@"; do
      "$PREFIX"/bin/perl -MCPAN -e 'notest install => "'"$mod"'"' \
         || fatal "$mod: unable to install from CPAN"
   done
   rm -rf "$STATICPERL/build"
}

#############################################################################
# install a module from unpacked sources

instsrc() {
   [ $NOCHECK_INSTALL ] || install

   verblock <<EOF
installing modules from source
$@
EOF

   for mod in "$@"; do
      echo
      echo $mod
      (
         rcd $mod
         make -f Makefile.aperl map_clean >/dev/null 2>&1
         make distclean >/dev/null 2>&1
         "$PREFIX"/bin/perl Makefile.PL || fatal "$mod: error running Makefile.PL"
         make || fatal "$mod: error building module"
         "$PREFIX"/bin/cpan-make-install || fatal "$mod: error installing module"
         make distclean >/dev/null 2>&1
         exit 0
      ) || exit $?
   done
}

#############################################################################
# do schmorpy stuff

MODSRCDIR=~/src

instmodsrc() {
   for mod in "$@"; do
      instmod_src "$MODSRCDIR/$mod"
   done
}

install_schmorp() {
   install

   instcpan Data::Dump Term::ReadLine::Perl Term::ANSIColor Term::ReadKey
   instcpan Digest::SHA Digest::MD6 Digest::SHA256 Digest::MD4 Digest::HMAC_MD5 Digest::HMAC_MD6 Digest::FNV
   #instcpan Net::SSLeay # requires static -ldl

   instmodsrc common-sense Crypt-Twofish2 Array-Heap Convert-Scalar Compress-LZF JSON-XS
   instmodsrc EV Guard Async-Interrupt IO-AIO
   instmodsrc AnyEvent AnyEvent-AIO Coro AnyEvent-HTTP
   instmodsrc Linux-Inotify2 EV-Loop-Async

   instcpan AnyEvent::HTTPD
}

#############################################################################
# main

podusage() {
   echo
   if [ -e "$PREFIX/bin/perl" ]; then
      "$PREFIX/bin/perl" -MPod::Usage -e \
         'pod2usage -input => *STDIN, -output => *STDOUT, -verbose => '$1', -exitval => 0, -noperldoc => 1' <"$0" \
         2>/dev/null && exit
   fi
   # try whatever perl we can find
   perl -MPod::Usage -e \
      'pod2usage -input => *STDIN, -output => *STDOUT, -verbose => '$1', -exitval => 0, -noperldoc => 1' <"$0" \
      2>/dev/null && exit

   fatal "displaying documentation requires a working perl - try '$0 install' first"
}

usage() {
   podusage 0
}

catmkbundle() {
   {
      read dummy
      echo "#!$PREFIX/bin/perl"
      cat
   } <<'MKBUNDLE'
#CAT mkbundle
MKBUNDLE
}

bundle() {
   catmkbundle >"$MKBUNDLE~" || fatal "$MKBUNDLE~: cannot create"
   chmod 755 "$MKBUNDLE~" && mv "$MKBUNDLE~" "$MKBUNDLE"
   "$PREFIX/bin/perl" -- "$MKBUNDLE" "$@"
}

if [ $# -gt 0 ]; then
   while [ $# -gt 0 ]; do
      mkdir -p "$STATICPERL" || fatal "$STATICPERL: cannot create"
      mkdir -p "$PREFIX" || fatal "$PREFIX: cannot create"

      command="${1#--}"; shift
      case "$command" in
         fetch | configure | build | install | clean | distclean)
            verblock <<EOF
$command
EOF
            "$command"
            ;;
         instsrc )
            instsrc "$@"
            exit
            ;;
         instcpan )
            instcpan "$@"
            exit
            ;;
         instschmorp )
            install_schmorp
            ;;
         cpan )
            install
            "$PREFIX/bin/cpan" "$@"
            exit
            ;;
         mkbundle )
            install
            bundle "$@"
            exit
            ;;
         mkperl )
            install
            bundle --perl "$@"
            exit
            ;;
         help )
            podusage 2
            ;;
         * )
            exec 1>&2
            echo
            echo "Unknown command: $command"
            podusage 0
            ;;
      esac
   done
else
   usage
fi

exit 0

#CAT staticperl.pod

