#!/bin/bash
# Wrapper file for Crack
# written by Javier Fernández-Sanguino Peña for the Debian GNU/Linux distribution
# Heavily rewritten by Paweł Więcek
# 
# Licensed under the GNU GPL v2.
# See /usr/share/doc/common-licenses/GPL

# This is needed due to relative calls to 'dawg' and co.
PATH=/usr/lib/Crack:$PATH
export PATH

if [ "`id -u`" != "0" ]
then
  echo "Crack requires root priviledges to run."
  exit 1
fi

CRACKDIR=/usr/share/Crack

# Prepare parameters list expanding any filenames to their canonical names
unset PARAMS
declare -a PARAMS
n=1

while [ "${!n}" != "" ]
do
  if [ -f "${!n}" ]
  then
    PARAMS[$n]="`readlink -f "${!n}"`"
    if [ "${PARAMS[$n]}" = "/etc/passwd" -a -f /etc/shadow ]
    then
      # Need to merge passwd with shadow...
      MERGED=`tempfile -d $CRACKDIR/run`
      >$MERGED
      chmod 600 $MERGED
      sh $CRACKDIR/scripts/shadmrg.sv >$MERGED
      PARAMS[$n]=$MERGED
    fi
  else
    PARAMS[$n]="${!n}"
  fi
  ((n++))
done

cd $CRACKDIR
./Crack "${PARAMS[@]}"
