#!/bin/sh
#the binaries from woofQ quirky/woof-arch/<target arch>/target/boot/easyinit/bin
#were merged by script 'merg2out' into easyinit/bin
#these need to have heaps of symlinks created.
#this cannot be done on a cross-build.
#binaries from these pkgs are here: busybox, coreutils, e2fsprogs, xdelta3, squashfs-tools, ntfs-3g
#this script called from 3builddistro
#161210 allow to run when host-arch and target-arch do not match. 161212 fixes.

EASYINIT='easyinit-populated'

[ ! -d ${EASYINIT}/bin ] && exit
[ ! -f ../WOOFMERGEVARS ] && exit

. ../WOOFMERGEVARS
#[ "$WOOF_HOSTARCH" != "$WOOF_TARGETARCH" ] && exit

#only run this once...
[ -h ${EASYINIT}/bin/ash ] && exit

echo "populating symlinks in ${EASYINIT}/bin..."

if [ -f ${EASYINIT}/bin/ntfs-3g ];then
 ln -s ntfs-3g ${EASYINIT}/bin/mount.ntfs-3g
fi

if [ -f ${EASYINIT}/bin/e2fsck ];then
 ln -s e2fsck ${EASYINIT}/bin/fsck.ext2
 ln -s e2fsck ${EASYINIT}/bin/fsck.ext3
 ln -s e2fsck ${EASYINIT}/bin/fsck.ext4
fi

if [ -f ${EASYINIT}/bin/coreutils ];then
 APPLETS=""
 if [ "$WOOF_HOSTARCH" == "$WOOF_TARGETARCH" ];then
  APPLETS="$(./${EASYINIT}/bin/coreutils --help | grep basename | tr '[' ' ')"
 else
  if which coreutils;then
   APPLETS="$(coreutils --help | grep basename | tr '[' ' ')"
  fi
 fi
 #ex: base32 base64 basename cat chcon chgrp chmod chown chroot cksum comm cp csplit cut date dd df dir dircolors dirname du echo env expand expr factor false fmt fold ginstall groups head hostid id join kill link ln logname ls md5sum mkdir mkfifo mknod mktemp mv nice nl nohup nproc numfmt od paste pathchk pinky pr printenv printf ptx pwd readlink realpath rm rmdir runcon seq sha1sum sha224sum sha256sum sha384sum sha512sum shred shuf sleep sort split stat stty sum sync tac tail tee test timeout touch tr true truncate tsort tty uname unexpand uniq unlink uptime users vdir wc who whoami yes
 for ONEAPPLET in $APPLETS
 do
  [ -e ${EASYINIT}/bin/${ONEAPPLET} ] && continue
  echo "#!/bin/coreutils --coreutils-prog-shebang=${ONEAPPLET}" > ${EASYINIT}/bin/${ONEAPPLET}
  chmod 755 ${EASYINIT}/bin/${ONEAPPLET}
 done
fi

if [ -f ${EASYINIT}/bin/busybox ];then
 if [ "$WOOF_HOSTARCH" == "$WOOF_TARGETARCH" ];then
  APPLETS="$(./${EASYINIT}/bin/busybox --list | tr '\n' ' ' | tr '[' ' ')"
 else
  APPLETS="$(busybox --list | grep -v -E 'chroot|getty|init|poweroff|reboot' | tr '\n' ' ' | tr '[' ' ')"
 fi
 #ex: acpid addgroup adduser ash base64 basename bbconfig beep blkdiscard blkid blockdev bunzip2 bzcat bzip2 cal cat chgrp chmod chown chpasswd chroot chvt cksum clear cmp cp cpio crond crontab cryptpw cut date dc dd deallocvt delgroup deluser depmod devmem df dhcprelay diff dirname dmesg dnsdomainname dos2unix dpkg dpkg-deb du dumpkmap dumpleases echo ed egrep eject env expand expr false fatattr fbset fbsplash fdflush fdformat fdisk fgconsole fgrep find findfs flash_eraseall flash_lock flash_unlock flashcp fold free freeramdisk fsck fstrim fsync ftpget ftpput fuser getopt getty grep groups guess_fstype gunzip gzip halt hd hdparm head hexdump hostid hostname httpd hwclock i2cdetect i2cdump i2cget i2cset id ifconfig ifdown ifplugd ifup init inotifyd insmod ionice iostat ip ipaddr ipcalc iplink iproute iprule iptunnel kbd_mode kill killall klogd less linux32 linux64 linuxrc ln loadfont loadkmap logger login logname logread losetup ls lsmod lsof lspci lsusb lzcat lzma lzop lzopcat makedevs man md5sum mdev mesg microcom mkdir mkdosfs mke2fs mkfifo mkfs.ext2 mkfs.reiser mkfs.vfat mknod mkpasswd mkswap mktemp modinfo modprobe more mount mountpoint mpstat mv nameif nc netstat nice nohup nsenter nslookup od openvt passwd patch pgrep pidof ping ping6 pipe_progress pivot_root pkill pmap poweroff powertop printenv printf ps pscan pstree pwd pwdx rdate rdev readahead readlink realpath reboot renice reset resize rev rfkill rm rmdir rmmod route rpm rpm2cpio rtcwake run-parts sed seq setarch setconsole setfont setkeycodes setlogcons setserial sh sha1sum sha256sum sha3sum sha512sum showkey shuf sleep sort split start-stop-daemon stat strings stty su sulogin sum swapoff swapon switch_root sync sysctl syslogd tac tail tar tee telnet test tftp time timeout top touch tr traceroute traceroute6 true truncate tty ttysize tune2fs ubirename udhcpc udhcpd uevent umount uname uncompress unexpand uniq unix2dos unlink unlzma unlzop unshare unxz unzip uptime usleep uudecode uuencode vi vlock volname watch wc wget which whoami xargs xz xzcat yes zcat
 for ONEAPPLET in $APPLETS
 do
  [ -e ${EASYINIT}/bin/${ONEAPPLET} ] && continue
  ln -s busybox ${EASYINIT}/bin/${ONEAPPLET}
 done
 ln -s busybox "${EASYINIT}/bin/["
 ln -s busybox "${EASYINIT}/bin/[["
 ln -s ../bin/busybox ${EASYINIT}/sbin/chroot #must match host!
 ln -s ../bin/busybox ${EASYINIT}/sbin/getty
 ln -s ../bin/busybox ${EASYINIT}/sbin/init
 ln -s ../bin/busybox ${EASYINIT}/sbin/poweroff
 ln -s ../bin/busybox ${EASYINIT}/sbin/reboot
fi

sync
echo '...done'
###end###

