# $Id: actar.sh 638 2008-06-11 07:23:49Z aakkaya $
# Autor:                      Juergen Pabel / Akkaya Consulting GmbH / www.akkaya.de
# Create Date:                2002
# Type:                       Shell Skript
# Description:                creates a selfextracting file
# Changed:                    2008 by Adem Akkaya

if [ ! $# -gt 0 -o $# -lt 4 ]; then
   echo "Usage: "
   echo " $0 <p1> <p2> <p3> <p4>"
   echo " "
   echo "  p1 = Name of the script collection directory"
   echo "  p2 = Name of the tar file which will be created (without extention)."
   echo "  p3 = Name of the main script which sould be started"
   echo "  p4 = Name of the docu file which sould be displayed"
   echo "  p5 = optional! Name of the script collection"
   exit 99
fi

(
echo "#!/usr/bin/ksh"
echo "# Copyright: (c) 2008 Akkaya Consulting GmbH"
echo "#                     http://www.akkaya.de"
echo "#                     http://www.ac-stb.de"
echo "# "
echo "# actar.sh creates a selfextracting file"
echo "#"
echo "# This program is free software; you can redistribute it and/or modify"
echo "# it under the terms of the GNU General Public License as published by"
echo "# the Free Software Foundation; either version 3 of the License, or"
echo "# (at your option) any later version."
echo "#"
echo "# This program is distributed in the hope that it will be useful,"
echo "# but WITHOUT ANY WARRANTY; without even the implied warranty of"
echo "# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"
echo "# GNU General Public License for more details."
echo "# You should have received a copy of the GNU General Public License"
echo "# along with this program.  If not, see <http://www.gnu.org/licenses/>."
echo " "
echo "EXTRACT_()"
echo "{ uudecode <<'@!#_EnTe__'"
tar -c $1 | uuencode ./$2.tar
echo "@!#_EnTe__"
echo "( tar -xf ./$2.tar && rm -f ./$2.tar ) | tee \${Outfile_}"
echo "}"
echo ""
echo "DOCU_()"
echo -n "{"
echo " ("
echo "echo \"`cat $4`\""
echo ") | more"
echo "  exit 1"
echo "}"
echo ""
echo "LICENSE_()"
echo "{ clear; echo \"Copyright: (c) 2008 Akkaya Consulting GmbH / www.akkaya.de\""
echo "  echo \" \""
echo "  echo \"actar.sh creates a selfextracting file \""
echo "  echo \" \""
echo "  echo \" This program is free software; you can redistribute it and/or modify\""
echo "  echo \" it under the terms of the GNU General Public License as published by\""
echo "  echo \" the Free Software Foundation; either version 3 of the License, or\""
echo "  echo \" (at your option) any later version.\""
echo "  echo \" \""
echo "  echo \" This program is distributed in the hope that it will be useful,\""
echo "  echo \" but WITHOUT ANY WARRANTY; without even the implied warranty of\""
echo "  echo \" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\""
echo "  echo \" GNU General Public License for more details.\""
echo "  echo \" \""
echo "  echo \" You should have received a copy of the GNU General Public License\""
echo "  echo \" along with this program.  If not, see <http://www.gnu.org/licenses/>.\""
echo "  exit 2"
echo "}"
echo ""
echo "USAGE_()"
echo "{ clear; echo \"Copyright: (c) 2008 Akkaya Consulting GmbH / www.akkaya.de\""
echo "  echo \" \""
echo "  echo \"./actar.$2.sh  self-extracting a \\\"uuencoded\\\" tar file\""
echo "  echo \" \""
echo "  echo \"Usage:\""
echo "  echo \"     actar.$2.sh [OPTION] [-o OUTFILE]\""
echo "  echo \" \""
echo "  echo \"Description:\""
echo "  echo \"     -h      show this usage message\""
echo "  echo \"     -l      show license information\""
echo "  echo \"     -d      docu of the archived script collection $5\""
echo "  echo \"     -e      only extract archived script collection $5\""
echo "  echo \"     -n      backup existing main skript directory $1 then extract with overriting existing files \""
echo "  echo \"     -s      extract the archived script collection $5 and start the main script $3\""
echo "  echo \"     -o      redirect output from main script to outputfile\""
echo "  echo \"             default outputfile is actar.<hostname>.out\""
echo "  echo \"     -r      remove the extracted script collection $5\""
echo "  echo \" \""
echo "  echo \"     actar.$2.sh uses a \\\"uuencoded\\\" tar-file\""
echo "  exit 3"
echo "}"
echo ""
echo "Help_=Doku_=Lizenz_=Remove_=Start_=Usage_=\"NiX\""
echo "Dir_=\`pwd\`"
echo ""
echo "[ \$# -lt 1 ] && USAGE_"
echo "while getopts denhlo:rp:s op 2>/dev/null; do"
echo "   case \$op in"
echo "     h) USAGE_;;"
echo "     d) DOCU_;;"
echo "     l) LICENSE_;;"
echo "     e) Extract_=\"Ja\";;"
echo "     n) Copy_=\"Ja\""
echo "        Extract_=\"Ja\""
echo "        Start_=\"Ja\";;"
echo "     s) Extract_=\"Ja\""
echo "        Start_=\"Ja\";;"
echo "     o) Outfile_=\$OPTARG;;"
echo "     p) Parameter_=\$OPTARG;;"
echo "     r) Remove_=\"Ja\";;"
echo "     ?) USAGE_;;"
echo "   esac"
echo "done"
echo ""
echo "if [ -z \"\${Outfile_}\" ]; then"
echo "   Outfile_=\${Dir_}/actar.\`hostname\`.out"
echo "fi"
echo ""
echo "if [ -f \"\${Outfile_}\" ]; then"
echo "   echo \"File [\${Outfile_}] exist\""
echo "   echo \"please remove file [\${Outfile_}] and restart\""
echo "   exit 4"
echo "fi"
echo ""
echo "if [ -d \"\${Outfile_}\" ]; then"
echo "   echo \"a Directory [\${Outfile_}] exist\""
echo "   echo \"please use another Outfilename\""
echo "   exit 5"
echo "fi"
echo ""
echo "touch \${Outfile_} 1>/dev/null 2>&1"
echo "if [ ! \$? -eq 0 ]; then"
echo "   echo \"can not create [\${Outfile_}]\""
echo "   exit 6"
echo "else"
echo "   rm \${Outfile_} 1>/dev/null 2>&1"
echo "   if [ ! \$? -eq 0 ]; then"
echo "      echo \"can not access [\${Outfile_}]\""
echo "      exit 7"
echo "   fi"
echo "fi"
echo ""
echo "      echo \"Copyright: (c) 2008 Akkaya Consulting GmbH / http://www.akkaya.de\""
echo ""
echo "if [ \"\${Copy_}\" = \"Ja\" ]; then"
echo "   if [ -d ./$2 ]; then"
echo "      date_=\`date "+%Y%m%d%H%M%S"\`"
echo "      echo \"Archiving ./$2 as actar.$2.\$date_.tar\""
echo "      ( tar -cvf actar.$2.\$date_.tar  ./$2 "
echo "      echo \"Compressing actar.$2.\$date_.tar\""
echo "      compress actar.$2.\$date_.tar; sleep 3"
echo "      ) > \${Outfile_}"
echo "   fi"
echo "fi"
echo ""
echo "if [ \"\${Extract_}\" = \"Ja\" ]; then"
echo "   if [ ! \"\${Copy_}\" = \"Ja\" ]; then"
echo "      if [ -d ./$2 ]; then"
echo "         echo \"Directory ./$2 exist\""
echo "         echo \"please remove or rename ./$2 than start again.\""
echo "         exit 8"
echo "      fi"
echo "   fi"
echo " "
echo "   echo \"extracting script collection\"; sleep 3"
echo "   EXTRACT_"
echo "fi"
echo ""
echo "if [ \"\${Start_}\" = \"Ja\" ]; then"
echo "   if [ ! -d ./$2 ]; then"
echo "      echo \"Directory ./$2 does not exist\""
echo "      exit 9"
echo "   fi"
echo "   xx=\`pwd\`"
echo "   cd $2"
echo "   echo \"starting main script $3\""
echo "   sh ./$3 \"\${Parameter_}\" | tee -a \${Outfile_}"
echo "   if [ ! \$? -eq 0 ]; then"
echo "      echo \"Fehler in $3\""
echo "      exit 99"
echo "   fi"
echo "   chmod 644 \${Outfile_}"
echo "   cd \$xx "
echo "fi"
echo ""
echo "if [ \"\${Remove_}\" = \"Ja\" ]; then"
echo "   if [ -d ./$2 ]; then"
echo "      clear"
echo "      Input_=NO"
echo "      echo \"Copyright: (c) 2008 Akkaya Consulting GmbH / http://www.akkaya.de\""
echo "      echo \"removing script collection: ./$2\""
echo "      echo \" \""
echo "      echo \"Are you certain hat you want to remove the entire directory structure \""
echo "      echo \"./$2\""
echo "      echo \"including all contents ?\""
echo "      printf  \"If you are sure type in 'yes' \""
echo "      read Input_"
echo "      if [ ! \"\${Input_}\" = \"yes\" ]; then"
echo "         exit 10"
echo "      else"
echo "         Input_=NO"
echo "         printf \"To continue type 'cont' \""
echo "         read Input_"
echo "         if [ ! \"\${Input_}\" = \"cont\" ]; then"
echo "            exit 11"
echo "         fi"
echo "         rm -rf ./$2 1>\${Outfile_} 2>&1"
echo "         if [ ! \$? -eq 0 ]; then"
echo "            echo \"problems with removing ./$2\""
echo "            echo \"Output of rm command in: ${Outfile_}\""
echo "            exit 12"
echo "         fi"
echo "      fi"
echo "   else"
echo "      echo \"directory structure  ./$2 does not exist!\""
echo "      exit 13"
echo "   fi"
echo "fi"
echo "exit 0"

) > actar.$2.sh

