#!/bin/sh -

perl=/usr/bin/perl; test -f $perl || perl=`wh perl`
cpan=/usr/bin/cpan; test -f $cpan || cpan=`wh cpan`
libs=

#se Digest::MD5;	# for pa
#se IPC::Open2;
#se Text::VimColor;	# for text-vimcolor(1)

# ckperl - rough test that all Perl modules are present for HOME
# Steve Kinzler, steve@kinzler.com, May 04/May 08/Nov 16
# https://kinzler.com/me/home.html#homedir

cpanforce=; shforce=
while :
do
	case $# in
	0)	break;;
	*)	case "$1" in
		-u)	libs="$libs -I$HOME/libp/perl";;
		-f)	cpanforce='force '; shforce=' -f';;

		--)	shift; break;;
		-h)	bad=t; break;;
		-*)	bad=t; echo "$0: unknown option ($1)" 1>&2;;
		*)	break;;
		esac
		shift;;
	esac
done

case "$bad" in
?*)	cat <<EOF 1>&2
usage: $0 [ -u ] [ -f ] [ cpan | sh ]
	-u	include perl lib in home directory
	-f	include force option to install commands
	cpan	output just cpan install commands for missing modules
	sh	output just single cpan shell command for missing modules
To attempt an install of all thoroughly missing modules at once, run:
	$0 -u sh | sh -x
EOF
	exit 1;;
esac

greph '[u#]se.*;' |
$perl -ne "s/[';\s].*//, print
		if s/^.*:\s*(eval\s+'\s*)?[u#]se\s+(.+)('\s*)?;.*/\$2\\n/" |
grep . | sort -u |
$perl -pe "s,^,$perl $libs -e 'use ,; s/$/'/" |
case "$1" in
cpan)	sh 2>&1 | sed -n 's/.*Can.t locate \([^ ]*\).*/\1/p' |
		  sed 's/\.p.$//; s/\//::/g; s/^/'"${cpanforce}install /";;
sh)	sh 2>&1 | sed -n 's/.*Can.t locate \([^ ]*\).*/\1/p' |
		  sed 's/\.p.$//; s/\//::/g' |
		  perl -e 'chomp(@_ = <>);
			   print "'"$cpan$shforce"' \\\n\t",
				 join(" \\\n\t", @_), "\n"';;
*)	sh -x;;
esac

rm -f smallprof.out
