From: Nils Kneuper Date: Sun, 6 Mar 2011 01:05:59 +0000 (+0100) Subject: new pnd_make.sh with many changes (but still compatible when usign the X-Git-Tag: sz_beta3~58 X-Git-Url: http://git.openpandora.org/cgi-bin/gitweb.cgi?p=pandora-libraries.git;a=commitdiff_plain;h=b6ed6054c59a2498c74287d038a527e2fd34f522 new pnd_make.sh with many changes (but still compatible when usign the old commands). Changelog: * Add decent argument parsing. * Add support for schema validation. * Add support for additional switches: --genpxml define the script to be used to generate a pxml file --schema define the schema file to be used * Add colored output. * General cleanup. --- diff --git a/testdata/scripts/pnd_make.sh b/testdata/scripts/pnd_make.sh index 3e8ad8b..15248c2 100644 --- a/testdata/scripts/pnd_make.sh +++ b/testdata/scripts/pnd_make.sh @@ -1,65 +1,273 @@ #!/bin/bash - + ######adjust path of genpxml.sh if you want to use that "feture"##### - -TEMP=`getopt -o p:d:x:i:c -- "$@"` - -if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi - -eval set -- "$TEMP" -while true ; do - case "$1" in - -p) echo "PNDNAME set to $2" ;PNDNAME=$2;shift 2;; - -d) echo "FOLDER set to $2" ;FOLDER=$2;shift 2 ;; - -x) echo "PXML set to $2" ;PXML=$2;shift 2 ;; - -i) echo "ICON set to $2" ;ICON=$2;shift 2 ;; - -c) echo "-c set, will create compressed squasfs image instead of iso $2" ;SQUASH=1;shift 1 ;; - --) shift ; break ;; - *) echo "Error while parsing arguments! $2" ; exit 1 ;; - esac + +PXML_schema="PXML_schema.xsd" +GENPXML_PATH="genpxml.sh" + +# useful functions ... +black='\E[30m' +red='\E[31m' +green='\E[32m' +yellow='\E[33m' +blue='\E[34m' +magenta='\E[35m' +cyan='\E[36m' +white='\E[37m' + + +cecho () # Color-echo. Argument $1 = message, Argument $2 = color +{ + local default_msg="No message passed." # Doesn't really need to be a local variable. + message=${1:-$default_msg} # Defaults to default message. + color=${2:-$black} # Defaults to black, if not specified. + echo -e "$color$message" + tput sgr0 # Reset to normal. + return +} + + +print_help() +{ + cat << EOSTREAM +pnd_make.sh - A script to package "something" into a PND. + +Usage: + $(basename ${0}) {--directory|-d} {--pndname|-p} [{--compress-squashfs|-c}] + [{--genpxml} ] [{--icon|-i} ] [{--pxml|-x} ] + [{--schema|-s} ] [{--help|-h}] + + +Switches: + --compress-squashfs / -c Define wether or not the pnd should be compressed using + squashfs. If this parameter is selected, a compressed pnd + will be created. + + --directory / -d Sets the folder that is to be used for the resulting pnd + to . This option is mandatory for the script to + function correctly. + + --genpxml + + --help / -h Displays this help text. + + --icon / -i Sets the icon that will be appended in the pnd to . + + --pndname / -p Sets the output filename of the resulting pnd to . + This option is mandatory for the script to function + correctly. + + --pxml / -x Sets the PXML file that is to be used to . If you + neither provide a PXML file or set this entry to 'guess', + an existing 'PXML.xml' in your selected '--directory' + will be used, or the script $GENPXML_PATH will be called + to try to generate a basic PXML file for your. + + --schema / -s Sets the schema file, that is to be used for validation, + to =4.0 of squashfs +is required to be available in your PATH. +EOSTREAM +} + + +# Parse command line parameters +while [ "${1}" != "" ]; do + if [ "${1}" = "--compress-squashfs" ] || [ "${1}" = "-c" ]; + then + SQUASH=1 + shift 1 + elif [ "${1}" = "--directory" ] || [ "${1}" = "-d" ]; + then + FOLDER=$2 + shift 2 + elif [ "${1}" = "--genpxml" ]; + then + GENPXML_PATH=$2 + shift 2 + elif [ "${1}" = "--help" ] || [ "${1}" = "-h" ]; + then + print_help + exit 0 + elif [ "${1}" = "--icon" ] || [ "${1}" = "-i" ]; + then + ICON=$2 + shift 2 + elif [ "${1}" = "--pndname" ] || [ "${1}" = "-p" ]; + then + PNDNAME=$2 + shift 2 + elif [ "${1}" = "--pxml" ] || [ "${1}" = "-x" ]; + then + PXML=$2 + shift 2 + elif [ "${1}" = "--schema" ] || [ "${1}" = "-f" ] + then + PXML_schema=$2 + shift 2 + else + cecho "ERROR: '$1' is not a known argument. Printing --help and aborting." $red + print_help + exit 1; + fi done - -rnd=$RANDOM; # random number for genpxml and index$rnd.xml - -#generate pxml if guess or empty -if [ ! $PXML ] || [ $PXML = "guess" ] && [ $PNDNAME ] && [ $FOLDER ]; then - PXMLtxt=$(/home/user/libpnd/pandora-libraries/testdata/scripts/genpxml.sh $FOLDER $ICON) - PXML=$FOLDER/PXML.xml - echo "$PXMLtxt" > $FOLDER/PXML.xml + + +# Generate a PXML if the param is set to Guess or it is empty. +#TODO: make sure this does still work nicely with the latest genpxml that sebt3 is working on! +if [ ! $PXML ] || [ $PXML = "guess" ] && [ $PNDNAME ] && [ $FOLDER ]; +then + if [ -f $FOLDER/PXML.xml ]; # use the already existing PXML.xml file if there is one... + then + PXML=$FOLDER/PXML.xml + PWML_ALREADY_EXISTING="true" + else + PXMLtxt=$($GENPXML_PATH $FOLDER $ICON) + PXML=$FOLDER/PXML.xml + echo "$PXMLtxt" > $FOLDER/PXML.xml + PXML_GENERATED="true" + fi fi - -#check arguments -if [ ! $PNDNAME ] || [ ! $FOLDER ] || [ ! $PXML ]; then - echo " Usage: pnd_make.sh -p your.pnd -d folder/containing/your/app/ -x - your.pxml (or \"guess\" to try to generate it from the folder) -i icon.png" + + +# Probe if required variables were set +echo -e +cecho "Checking if all required variables were set." $green +if [ ! $PNDNAME ] || [ ! $FOLDER ] || [ ! $PXML ]; +then + echo -e + cecho "ERROR: Not all required options were set! Please see the --help information below." $red + echo -e + print_help exit 1 +else + echo "PNDNAME set to '$PNDNAME'." fi -if [ ! -d $FOLDER ]; then echo "$FOLDER doesnt exist"; exit 1; fi #check if folder actually exists -if [ ! -f $PXML ]; then echo "$PXML doesnt exist"; exit 1; fi #check if pxml actually exists - -#make iso from folder -if [ ! $SQUASH ]; then - mkisofs -o $PNDNAME.iso -R $FOLDER +# Check if the selected folder actually exists +if [ ! -d $FOLDER ]; +then + echo -e + cecho "ERROR: '$FOLDER' doesnt exist or is not a folder." $red + exit 1 +else + echo "FOLDER set to '$FOLDER'." +fi +# Check if the selected PXML file actually exists +if [ ! -f $PXML ]; +then + echo -e + cecho "ERROR: '$PXML' doesnt exist or is not a file." $red + exit 1 +else + if [ $PWML_ALREADY_EXISTING ]; + then + echo "You have not explicitly specified a PXML to use, but an existing file was +fount. Will be using this one." + elif [ $PXML_GENERATED ]; + then + echo "A PXML file was generated for you using '$GENPXML_PATH'. This file will +not be removed at the end of this script because you might want to review it, adjust +single entries and rerun the script to generate a pnd with a PXML file with all the +information you want to have listed." + fi + echo "PXML set to '$PXML'." +fi + +# Print the other variables: +if [ $ICON ]; +then + if [ ! -f $ICON ] + then + cecho "'$ICON' doesn't exist, will not append the selected icon to the pnd." $red + else + echo "ICON set to '$ICON'." + USE_ICON="true" + fi +fi +if [ $SQUASH ]; +then + echo "Will use a squashfs for '$PNDNAME'." +fi + + +# Validate the PXML file (if xmllint is available) +# Errors and problems in this section will be shown but are not fatal. +echo -e +cecho "Trying to validate '$PXML' now. Will be using '$PXML_schema' to do so." $green +which xmllint &> /dev/null +if [ "$?" -ne "0" ]; +then + VALIDATED=false + cecho "WARNING: Could not find 'xmllint'. Validity check of '$PXML' is not possible!" $red +else + if [ ! -f "$PXML_schema" ]; + then + VALIDATED=false + cecho "WARNING: Could not find '$PXML_schema'. If you want to validate your PXML file +please make sure to provide a schema using the --schema option." $red + else + xmllint --noout --schema $PXML_schema $PXML + if [ "$?" -ne "0" ]; then VALIDATED=false; else VALIDATED=true; fi + fi +fi +# Print some message at the end about the validation in case the user missed the output above +if [ $VALIDATED = "false" ] +then + cecho "WARNING: Could not successfully validate '$PXML'. Please check the output +above. This does not mean that your pnd will be broken. Either you are not following the strict +syntax required for validation or you don't have all files/programs required for validating." $red else - if [ $(mksquashfs -version | awk 'BEGIN{r=0} $3>=4{r=1} END{print r}') = 0 ]; then - echo "your squashfs version is older then version 4, pleas upgrade to 4.0 or later" - exit 1 - fi - mksquashfs $FOLDER $PNDNAME.iso -nopad -no-recovery + cecho "Your file '$PXML' was validated successfully. The resulting pnd should +work nicely with libpnd." $green fi -#append pxml to iso -cat $PNDNAME.iso $PXML > $PNDNAME + + +# Make iso from folder +echo -e +cecho "Creating an iso file based on '$FOLDER'." $green +if [ $SQUASH ]; +then + if [ $(mksquashfs -version | awk 'BEGIN{r=0} $3>=4{r=1} END{print r}') -eq 0 ]; + then + cecho "ERROR: Your squashfs version is older then version 4, pleas upgrade to 4.0 or later" $red + exit 1 + fi + mksquashfs $FOLDER $PNDNAME.iso -nopad -no-recovery +else + mkisofs -o $PNDNAME.iso -R $FOLDER +fi + + +# Append pxml to iso +echo -e +cecho "Appending '$PXML' to the created iso file." $green +cat $PNDNAME.iso $PXML > $PNDNAME rm $PNDNAME.iso #cleanup -#append icon if specified -if [ $ICON ]; then # check if we want to add an icon - if [ ! -f $ICON ]; then #does the icon actually exist? - echo "$ICON doesnt exist" - else # yes + +# Append icon if specified and available +if [ $USE_ICON ]; +then + echo -e + cecho "Appending the icon '$ICON' to the pnd." $green mv $PNDNAME $PNDNAME.tmp cat $PNDNAME.tmp $ICON > $PNDNAME # append icon rm $PNDNAME.tmp #cleanup - fi fi - -if [ $PXML = "guess" ];then rm $FOLDER/PXML.xml; fi #cleanup + + +# Final message +echo -e +if [ -f $PNDNAME ]; +then + cecho "Finished creating the pnd '$PNDNAME'." $green +else + cecho "There seems to have been a problem and '$PNDNAME' was not created. Please check +the output above for any error messages." $red +fi + + +#if [ $PXML = "guess" ];then rm $FOLDER/PXML.xml; fi #cleanup